private void btn_SerialOpen_Click(object sender, EventArgs e) { if (btn_SerialOpen.Text == "打开端口") { SerialFun.ComPortSend = new SerialPort(); SerialFun.ComPortSend.BaudRate = Convert.ToInt32(comboBox_SerialBaudrate.Text); SerialFun.ComPortSend.PortName = comboBox_SerialPortNum.Text; SerialFun.ComPortSend.DataBits = Convert.ToInt32(comboBox_SerialDatabit.Text); switch (comboBox_SerialStopbit.Text) { case "1": SerialFun.ComPortSend.StopBits = StopBits.One; break; case "1.5": SerialFun.ComPortSend.StopBits = StopBits.OnePointFive; break; case "2": SerialFun.ComPortSend.StopBits = StopBits.Two; break; default: MessageBox.Show("Error:停止位参数设置不正确", "Error"); break; } switch (comboBox_SerialParity.Text) { case "无校验": SerialFun.ComPortSend.Parity = Parity.None; break; case "偶校验": SerialFun.ComPortSend.Parity = Parity.Even; break; case "奇校验": SerialFun.ComPortSend.Parity = Parity.Odd; break; default: MessageBox.Show("Error:校验位参数设置不正确", "Error"); break; } try { SerialFun.ComPortSend.Open(); MyLog.Info("打开串口成功"); //事件注册 SerialFun.ComPortSend.DataReceived += ComPortSend_DataReceived;; SetConfigValue("serialportsend", SerialFun.ComPortSend.PortName); SetConfigValue("baudrate1", SerialFun.ComPortSend.BaudRate.ToString()); SetConfigValue("databit1", SerialFun.ComPortSend.DataBits.ToString()); btn_SerialOpen.Text = "关闭端口"; } catch (Exception ex) { MyLog.Error("打开串口失败:" + ex.Message); } } else { try { btn_SerialOpen.Text = "打开端口"; SerialFun.ComPortSend.Close(); //事件注销 SerialFun.ComPortSend.DataReceived -= ComPortSend_DataReceived; MyLog.Info("关闭串口成功"); } catch (Exception ex) { MyLog.Error("关闭串口失败:" + ex.Message); } } }
private void Fun_Compare() { DeleUpdateGridView myDeleUpdate = new DeleUpdateGridView(UpdateGridView); DeleUpdateGridView2 myDeleUpdate2 = new DeleUpdateGridView2(UpdateGridView2); DeleUpdateTextBox myDeleUpdateTextBox = new DeleUpdateTextBox(UpdateTextBox); String ret1 = null; bool result = true; if (SerialFun.ComPortSend.IsOpen) { try { MyLog.Info("开始升锁测试"); #region 升锁测试 ret1 = SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF051002FF00");//升锁 Thread.Sleep(500); if (Data.ReturnStr == ret1) { dataGridView1.Invoke(myDeleUpdate, 0, "升锁中..."); } Thread.Sleep(5000); SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0420010004");//查询 Thread.Sleep(500); if (Data.ReturnStr.Substring(22, 2) == "55") { dataGridView1.Invoke(myDeleUpdate, 0, "升锁成功"); } else { dataGridView1.Invoke(myDeleUpdate, 0, "升锁失败"); result = false; } #endregion MyLog.Info("开始降锁测试"); #region 降锁测试 ret1 = SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF051003FF00");//降锁 Thread.Sleep(500); if (Data.ReturnStr == ret1) { dataGridView1.Invoke(myDeleUpdate, 1, "降锁中..."); } Thread.Sleep(5000); SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0420010004");//查询 Thread.Sleep(500); if (Data.ReturnStr.Substring(22, 2) == "ff") { dataGridView1.Invoke(myDeleUpdate, 1, "降锁成功"); } else { dataGridView1.Invoke(myDeleUpdate, 1, "降锁失败"); result = false; } #endregion #region 蜂鸣器测试 //ret1 = SerialFun.SendToPort(SerialFun.ComPortSend, "01051004FF00");//报警 //Thread.Sleep(500); //if (Data.ReturnStr == ret1) //{ // dataGridView1.Invoke(myDeleUpdate, 2, "报警中..."); //} //Thread.Sleep(2000); //ret1 = SerialFun.SendToPort(SerialFun.ComPortSend, "01051005FF00");//取消报警 //Thread.Sleep(500); //if (Data.ReturnStr == ret1) //{ // dataGridView1.Invoke(myDeleUpdate, 2, "取消报警..."); //} //Thread.Sleep(500); //UserConfirm frm = new UserConfirm(); //frm.ShowDialog(); //if (Data.AlarmStatus) //{ // dataGridView1.Invoke(myDeleUpdate, 2, "蜂鸣器正常"); //} //else //{ // dataGridView1.Invoke(myDeleUpdate, 2, "蜂鸣器异常"); // result = false; //} #endregion MyLog.Info("开始无车情况下 超声测试以及地磁测试"); #region 无车情况下 超声测试以及地磁测试 ret1 = SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050300");//进入超声测试 Thread.Sleep(500); if (Data.ReturnStr == ret1) { dataGridView1.Invoke(myDeleUpdate, 3, "超声测试中..."); } bool Normal_Ultrasonic_Tag = true; for (int i = 0; i < 10; i++) { SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0420010004");//查询 Thread.Sleep(200); if (Data.ReturnStr.Length > 32) { if (Data.ReturnStr.Substring(30, 2) != "00") { Normal_Ultrasonic_Tag = false; MyLog.Error("无车超声测试时收到异常返回:" + Data.ReturnStr); break; } } } if (Normal_Ultrasonic_Tag) { dataGridView1.Invoke(myDeleUpdate, 3, "无车情况:超声正常"); } else { byte temp_byte = Convert.ToByte(Data.ReturnStr.Substring(30, 2), 16); bool temp_flag1 = false; bool temp_flag2 = false; if ((temp_byte & 0xf0) != 0) { temp_flag1 = true; } if ((temp_byte & 0x0f) != 0) { temp_flag2 = true; } if (temp_flag1 & temp_flag2) { dataGridView1.Invoke(myDeleUpdate, 3, "无车情况:超声正常"); } else if ((!temp_flag1) & temp_flag2) { dataGridView1.Invoke(myDeleUpdate, 3, "无车情况:超声探头1异常"); } else if (temp_flag1 & (!temp_flag2)) { dataGridView1.Invoke(myDeleUpdate, 3, "无车情况:超声探头2异常"); } else { dataGridView1.Invoke(myDeleUpdate, 3, "无车情况:超声探头1,2异常"); } result = false; } SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050000"); //退出超声测试 Thread.Sleep(500); SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050000"); //退出超声测试 Thread.Sleep(500); ret1 = SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050400");//进入地磁测试 Thread.Sleep(2000); if (Data.ReturnStr == ret1) { dataGridView1.Invoke(myDeleUpdate, 4, "地磁测试中..."); } bool Normal_UltraMagnetic_Tag = true; for (int i = 0; i < 10; i++) { SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0420010004");//查询 Thread.Sleep(200); if (Data.ReturnStr.Length > 32) { if (Data.ReturnStr.Substring(24, 2) != "00") { Normal_UltraMagnetic_Tag = false; MyLog.Error("无车地磁测试时收到异常返回:" + Data.ReturnStr); break; } } } if (Normal_UltraMagnetic_Tag) { dataGridView1.Invoke(myDeleUpdate, 4, "无车情况:地磁正常"); } else { dataGridView1.Invoke(myDeleUpdate, 4, "无车情况:地磁异常"); result = false; } SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050000"); //退出地磁测试 Thread.Sleep(500); SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050000"); //退出地磁测试 Thread.Sleep(500); #endregion MyLog.Info("开始有车情况下 超声测试以及地磁测试"); #region 车情况下,超声测试、地磁测试 ret1 = SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050300");//进入有车超声测试 Thread.Sleep(500); if (Data.ReturnStr == ret1) { dataGridView1.Invoke(myDeleUpdate, 5, "超声测试中..."); } User_Ultrasonic_Confirm frm2 = new User_Ultrasonic_Confirm(); frm2.ShowDialog(); switch (Data.UltrasonicStatus) { case 0: dataGridView1.Invoke(myDeleUpdate, 5, "有车情况:超声正常"); break; case 1: dataGridView1.Invoke(myDeleUpdate, 5, "有车情况:超声探头1异常"); result = false; break; case 2: dataGridView1.Invoke(myDeleUpdate, 5, "有车情况:超声探头2异常"); result = false; break; case 3: dataGridView1.Invoke(myDeleUpdate, 5, "有车情况:超声探头1,2异常"); result = false; break; case 4: dataGridView1.Invoke(myDeleUpdate, 5, "485通讯异常"); result = false; break; default: dataGridView1.Invoke(myDeleUpdate, 5, "有车情况:超声探头1,2异常"); result = false; break; } SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050000"); //退出超声测试 Thread.Sleep(500); SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050000"); //退出超声测试 Thread.Sleep(500); ret1 = SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050400");//进入有车地磁测试 Thread.Sleep(500); if (Data.ReturnStr == ret1) { dataGridView1.Invoke(myDeleUpdate, 6, "地磁测试中..."); } User_Magnetic_Confirm frm3 = new User_Magnetic_Confirm(); frm3.ShowDialog(); if (Data.MagneticStatus) { dataGridView1.Invoke(myDeleUpdate, 6, "有车情况:地磁正常"); } else { dataGridView1.Invoke(myDeleUpdate, 6, "有车情况:地磁异常"); result = false; } SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050000"); //退出地磁测试 Thread.Sleep(500); SerialFun.SendToPort(SerialFun.ComPortSend, "FFFFFFFF0601050000"); //退出地磁测试 Thread.Sleep(500); #endregion if (result) { string timestr = string.Format("{0}-{1:D2}-{2:D2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day); PrintMachine.PrintUniqueCode(timestr, Data.UniqueCode); string time = string.Format("{0}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); string detail = ""; foreach (DataRow row in dtLock.Rows) { detail += row[2] + ","; } dataGridView2.Invoke(myDeleUpdate2, Data.UniqueCode, time, "测试通过", detail); Data.sql.InsertValues("table_All", new string[] { Data.UniqueCode, time, "测试通过", detail }); Data.sql.InsertValues("table_Right", new string[] { Data.UniqueCode, time, "测试通过", detail }); } else { MyLog.Error("当前测试失败,请重新测试!"); string time = string.Format("{0}-{1:D2}-{2:D2} {3:D2}:{4:D2}:{5:D2}", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); string detail = ""; foreach (DataRow row in dtLock.Rows) { detail += row[2] + ","; } dataGridView2.Invoke(myDeleUpdate2, Data.UniqueCode, time, "测试失败", detail); Data.sql.InsertValues("table_All", new string[] { Data.UniqueCode, time, "测试失败", detail }); Data.sql.InsertValues("table_Error", new string[] { Data.UniqueCode, time, "测试失败", detail }); } } catch (Exception ex) { MessageBox.Show("串口错误,请重新配置串口!!"); // MyLog.Error(ex.Message); MyLog.Error(ex.ToString()); } } else { MyLog.Error("串口未打开,测试失败,请重新测试!"); } Trace.WriteLine("退出Fun_Compare"); }
public static void PrintUniqueCode(String str_time, String str_code) { //Test code start // open port. int nLen, ret, sw; byte[] pbuf = new byte[128]; string strmsg; IntPtr ver; System.Text.Encoding encAscII = System.Text.Encoding.ASCII; System.Text.Encoding encUnicode = System.Text.Encoding.Unicode; // dll version. ver = PrintMachine.B_Get_DLL_Version(0); // search port. nLen = PrintMachine.B_GetUSBBufferLen() + 1; strmsg = "DLL "; strmsg += Marshal.PtrToStringAnsi(ver); strmsg += "\r\n"; if (nLen > 1) { byte[] buf1, buf2; int len1 = 128, len2 = 128; buf1 = new byte[len1]; buf2 = new byte[len2]; PrintMachine.B_EnumUSB(pbuf); PrintMachine.B_GetUSBDeviceInfo(1, buf1, out len1, buf2, out len2); sw = 1; if (1 == sw) { ret = PrintMachine.B_CreatePrn(12, encAscII.GetString(buf2, 0, len2));// open usb. } else { ret = PrintMachine.B_CreateUSBPort(1);// must call B_GetUSBBufferLen() function fisrt. } if (0 != ret) { // strmsg += "Open USB fail!"; MyLog.Error("Open USB fail!"); } else { strmsg += "Open USB:\r\nDevice name: "; strmsg += encAscII.GetString(buf1, 0, len1); strmsg += "\r\nDevice path: "; strmsg += encAscII.GetString(buf2, 0, len2); //sw = 2; if (2 == sw) { //Immediate Error Report. PrintMachine.B_WriteData(1, encAscII.GetBytes("^ee\r\n"), 5);//^ee ret = PrintMachine.B_ReadData(pbuf, 4, 1000); } MyLog.Info("打码机打开成功,开始打印二维码!"); // sample setting. PrintMachine.B_Set_DebugDialog(1); PrintMachine.B_Set_Originpoint(0, 0); PrintMachine.B_Select_Option(2); PrintMachine.B_Set_Darkness(8); PrintMachine.B_Del_Pcx("*"); // delete all picture. //draw box. //B_Draw_Box(330, 20, 4, 880, 450);//画一个框 PrintMachine.B_Prn_Text_TrueType_W(500, 40, 80, 30, "Times New Roman", 1, 400, 0, 0, 0, "AA", "位位通"); PrintMachine.B_Prn_Text_TrueType_W(350, 140, 40, 20, "Times New Roman", 1, 400, 0, 0, 0, "AB", "合格证 " + str_time); PrintMachine.B_Prn_Barcode(350, 220, 0, "9", 4, 10, 160, 'B', str_code); //印出一個條碼和加上跳號功能 PrintMachine.B_Print_Out(1); // copy 2. PrintMachine.B_ClosePrn(); MyLog.Info("二维码打印成功!"); } } else { MyLog.Error("未检测到打码机,请确认打码机连接正常,指示灯亮!"); //System.IO.Directory.CreateDirectory(szSavePath); // ret = PrintMachine.B_CreatePrn(0, szSaveFile);// open file. } //if (0 != ret) // return; }