110,536
社区成员
发帖
与我相关
我的任务
分享
System.Threading.Timer TimerItem;
private void button2_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName = SearchExcelCom();
serialPort1.Open();
System.Threading.TimerCallback TimerDelegate = new System.Threading.TimerCallback(timerRead);
TimerItem = new System.Threading.Timer(TimerDelegate, null, 0, 1500);
}
catch (Exception ex)
{
MessageBox.Show("C+" + ex.Message);
}
}
private void timerRead(object obj)
{
try
{
//Invoke(new Action(() =>
// {
byte[] byteBuffer = new byte[] { 0x02, 0x30, 0x30, 0x31, 0x30, 0x30, 0x30, 0x31, 0x03, 0x35, 0x35 };
serialPort1.Write(byteBuffer, 0, byteBuffer.Length);
// }));
if (serialPort1.BytesToRead > 0)
{
try
{
if (dataGridView1.Rows.Count - 1 > 0)
{
DateTime dtime = Convert.ToDateTime(dataGridView1.Rows[dataGridView1.Rows.Count - 2].Cells[3].Value.ToString());
TimeSpan ts = DateTime.Now.Subtract(dtime);
if (ts.Seconds <= 5)
{
return;
}
}
}
catch (Exception)
{
Invoke(new Action(() =>
{
DataTable dt = (DataTable)dataGridView1.DataSource;
dt.Rows.RemoveAt(dt.Rows.Count - 1);
dataGridView1.DataSource = dt;
label8.Text = dt.Rows.Count.ToString();
timerRead(null);
}));
}
byte[] rs = new byte[serialPort1.BytesToRead];
serialPort1.Read(rs, 0, rs.Length);
string qsss = "";
for (int i = 0; i < rs.Length; i++)
{
qsss += rs[i].ToString("X2") + " ";
}
rs = null;
//serialPort1.Close();
if (qsss.Equals("02 30 31 03 36 34 "))//!string.IsNullOrEmpty(rs[0].ToString())
{
string Count = "0";
if (label9.Text.Equals("晚班"))
{
Count = "1";
}
Invoke(new Action(() =>
{
DataTable dt = (DataTable)dataGridView1.DataSource;
DataRow dr = dt.NewRow();
dr["编号"] = dataGridView1.Rows.Count.ToString();
dr["品号"] = comboBox3.Text;
dr["名称"] = comboBox2.Text;
dr["打印时间"] = DateTime.Now.ToString();
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
this.dataGridView1.FirstDisplayedScrollingRowIndex = dt.Rows.Count - 1;
label8.Text = (dataGridView1.Rows.Count - 1).ToString();
}));
ENPOT.Bartender.Print.BartenderPrinter b = new ENPOT.Bartender.Print.BartenderPrinter();
b.LabelPrinted += new EventHandler<EventArgs>(b_LabelPrinted);
string printString = "\"品号\",\"灯位\",\"条码\"";
printString += "\r\n\"" + list[comboBox2.SelectedIndex] + "\",";
printString += "\"" + PCode[comboBox2.SelectedIndex] + "\",";
printString += "\"" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day.ToString() + Count + Convert.ToInt32(label8.Text).ToString("000") + "\"";
b.Print(printString, url + @"\大茂软件\" + listView1.SelectedItems[0].Text.Split('.')[0] + ".btw", new System.Drawing.Printing.PrintDocument().PrinterSettings.PrinterName, false);
}
}
}
catch (Exception mes)
{
MessageBox.Show("B+" + mes.Message);
return;
}
}