实例介绍
【实例截图】
【核心代码】
using Beetle.TestServer; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO.Ports; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace RS232 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int addr1 = 0; int addr2 = 0; bool state = false; byte[] meterid = new byte[2]; #region 接收处理 //以下是串口操作 [DllImport("kernel32.dll ")] //调用API 第一个参数是指频率的高低,越大越高,第二个参数是指响的时间多长 private static extern int Beep(int dwFreq, int dwDuration); int cow = 0; private void OutputLog(string text) { try { cow ; richTextBox1.AppendText(DateTime.Now.ToString("HH:mm:ss") ":" text); richTextBox1.AppendText("\r\n"); richTextBox1.Select(this.richTextBox1.Text.Length, 1); //设置滚动到当前位置 richTextBox1.ScrollToCaret(); if (cow > 200) { cow = 0; richTextBox1.Clear(); } } catch (Exception ex) { } } // 2、计算两个时间差值的函数,返回时间差的绝对值: private string DateDiff(DateTime DateTime1, DateTime DateTime2) { string dateDiff = null; try { TimeSpan ts1 = new TimeSpan(DateTime1.Ticks); TimeSpan ts2 = new TimeSpan(DateTime2.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); dateDiff = ts.Days.ToString() "天" ts.Hours.ToString() "小时" ts.Minutes.ToString() "分钟" ts.Seconds.ToString() "秒"; } catch { } return dateDiff; } private void Log(string text) { base.Invoke(new Action<string>(this.OutputLog), new object[] { text }); } bool stateVer = false; int state11 = 0; int rowsCount = 0; private StringBuilder builder = new StringBuilder();//避免在事件处理方法中反复的创建,定义到外面。 private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { Beep(0X7FF, 500); try { int i = 0; int n = 0; int n1 = 0; System.Threading.Thread.Sleep(1500); //for (i = 0; i < 500; i ) //{ // //if (state == false) // //{ // // return; // //} // System.Threading.Thread.Sleep(100); //读取速度太慢,加Sleep延长读取时间, 不可缺少 // //delayTime(1); // n = serialPort1.BytesToRead;//先记录下来,避免某种原因,人为的原因,操作几次之间时间长,缓存不一致 // //Log(n.ToString()); // if (n != 0) // { // if (n == n1) break; // else n1 = n; // } //} //n = n1; //if (n == 0) { state11 = 1; return; } //else state11 = 2; byte[] buf = new byte[n];//声明一个临时数组存储当前来的串口数据 serialPort1.Read(buf, 0, n); // string txt = Encoding.ASCII.GetString(buf); //ASCII卡码 // string txt = ByteToHexStr(buffer); //用到函数,作用:转换16进制 //this.Invoke(textChanged, new string[] { txt }); //通过Invoke调用事件 this.Invoke((EventHandler)(delegate { try { #region builder.Append("接收命令:"); Log("接收:" strHex.byteToHexStr(buf)); //终端地址 68 4A 00 4A 00 68 A8 01 41 FF FF 01 0A 60 00 00 01 0B 01 41 6F 00 0A 00 1A 16 if (buf.Length > 6) { if (buf[12] == 0x0A) { string straddr1 = buf[19].ToString("X2") buf[18].ToString("X2"); string straddr2 = buf[21].ToString("X2") buf[20].ToString("X2"); addr1 = Convert.ToInt32(straddr1); addr2 = Convert.ToInt32(straddr2, 16); txtaddr1.Text = straddr1; txtaddr2.Text = addr2.ToString(); Log("结果--区划码:" straddr1 "终端地址:" addr2.ToString()); } else if (buf[14] == 0x0A) { string straddr1 = buf[21].ToString("X2") buf[20].ToString("X2"); string straddr2 = buf[23].ToString("X2") buf[22].ToString("X2"); addr1 = Convert.ToInt32(straddr1); addr2 = Convert.ToInt32(straddr2, 16); txtaddr1.Text = straddr1; txtaddr2.Text = addr2.ToString(); Log("结果--区划码:" straddr1 "终端地址:" addr2.ToString()); } else if (buf[12] == 0x00) { if (buf[16] == 0x01) { Log("结果--确认帧"); if (rowsCount == 7) { // serialPort1.Close(); return; } } else { Log("结果--否认帧"); } } //<= 68 4A 00 4A 00 68 88 01 41 13 00 02 0C 60 00 00 02 00 48 11 09 16 23 15 FD 16 else if (buf[12] == 0x0C) { string Year = buf[23].ToString("X2"); string Month = buf[21].ToString("X2"); string Day = Convert.ToString(Convert.ToInt32((buf[22] & 0x1f).ToString("X2"))); string Hour = buf[20].ToString("X2"); string Minute = buf[19].ToString("X2"); string Second = buf[18].ToString("X2"); DateTime dt1; string strdt1 = Year "/" Day "/" Month " " Hour ":" Minute ":" Second; dt1 = Convert.ToDateTime(strdt1); DateTime dt = DateTime.Now; TimeSpan ts1 = new TimeSpan(dt1.Ticks); TimeSpan ts2 = new TimeSpan(dt.Ticks); TimeSpan ts = ts1.Subtract(ts2).Duration(); string dateDiff = ts.Days.ToString() "天" ts.Hours.ToString() "小时" ts.Minutes.ToString() "分钟" ts.Seconds.ToString() "秒"; // string strtime= DateDiff(DateTime.Now, dt1); Log("结果--时钟为:" dateDiff); Log("结果--时钟为:" Year "-" Day "-" Month " " Hour ":" Minute ":" Second ""); if (ts.Seconds >= 5) { MessageBox.Show("时钟相差大于5秒!!"); state = false; button4.Enabled = true; return; } } //68 3A 02 3A 02 68 88 01 41 07 00 02 AA 60 00 00 01 1E 80 06 //00 01 00 00 00 00 00 00 00 00 00 00 //01 02 00 00 90 01 00 00 07 01 00 00 12 02 00 00 02 02 00 00 19 01 00 00 01 02 00 00 01 02 00 00 01 02 00 00 10 01 00 00 10 01 00 00 00 00 00 00 10 01 00 00 10 01 00 00 03 02 00 00 03 02 00 00 01 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AA 16 else if (buf[12] == 0xAA) { double intRTC = Convert.ToDouble(buf[21].ToString("X2") buf[20].ToString("X2")); double intMonitor = Convert.ToDouble(buf[33].ToString("X2") buf[32].ToString("X2")); double intLcd = Convert.ToDouble(buf[37].ToString("X2") buf[36].ToString("X2")); double intKey = Convert.ToDouble(buf[41].ToString("X2") buf[40].ToString("X2")); double intJmain = Convert.ToDouble(buf[45].ToString("X2") buf[44].ToString("X2")); double intCuDP = Convert.ToDouble(buf[49].ToString("X2") buf[48].ToString("X2")); double intCNTLP = Convert.ToDouble(buf[53].ToString("X2") buf[52].ToString("X2")); double intFREEZE = Convert.ToDouble(buf[57].ToString("X2") buf[56].ToString("X2")); double intCURVE = Convert.ToDouble(buf[61].ToString("X2") buf[60].ToString("X2")); double intEventRec = Convert.ToDouble(buf[65].ToString("X2") buf[64].ToString("X2")); double intRS232 = Convert.ToDouble(buf[69].ToString("X2") buf[68].ToString("X2")); double intGPRS1 = Convert.ToDouble(buf[73].ToString("X2") buf[72].ToString("X2")); double intINFRARED = Convert.ToDouble(buf[77].ToString("X2") buf[76].ToString("X2")); double intRS485 = Convert.ToDouble(buf[81].ToString("X2") buf[80].ToString("X2")); double intNET = Convert.ToDouble(buf[85].ToString("X2") buf[84].ToString("X2")); double intREAD = Convert.ToDouble(buf[89].ToString("X2") buf[88].ToString("X2")); double intREAD2 = Convert.ToDouble(buf[93].ToString("X2") buf[92].ToString("X2")); double intudisk = Convert.ToDouble(buf[97].ToString("X2") buf[96].ToString("X2")); //double intGPRS1 = Convert.ToDouble(buf[37].ToString("X2") buf[36].ToString("X2")); //double intINFRARED = Convert.ToDouble(buf[21].ToString("X2") buf[20].ToString("X2")); //double intRS485 = Convert.ToDouble(buf[33].ToString("X2") buf[32].ToString("X2")); //double intNET = Convert.ToDouble(buf[37].ToString("X2") buf[36].ToString("X2")); if (intRTC / 100 == 1) { Log("RTC :" (intRTC / 100).ToString("0.00") " OK"); } else { Log("RTC :" (intRTC / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intMonitor / 100 == 2.01) { Log("MONITOR :" (intMonitor / 100).ToString("0.00") " OK"); } else { Log("MONITOR :" (intMonitor / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intLcd / 100 == 1.91) { Log("LCD :" (intLcd / 100).ToString("0.00") " OK"); } else { Log("LCD :" (intLcd / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intKey / 100 == 1.08) { Log("Key :" (intKey / 100).ToString("0.00") " OK"); } else { Log("Key :" (intKey / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intJmain / 100 == 2.12) { Log("JMAIN :" (intJmain / 100).ToString("0.00") " OK"); } else { Log("JMAIN :" (intJmain / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intCuDP / 100 == 2.02) { Log("CURDP :" (intCuDP / 100).ToString("0.00") " OK"); } else { Log("CURDP :" (intCuDP / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intCNTLP / 100 == 1.19) { Log("CNTLP :" (intCNTLP / 100).ToString("0.00") " OK"); } else { Log("CNTLP :" (intCNTLP / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intFREEZE / 100 == 2.01) { Log("FREEZE :" (intFREEZE / 100).ToString("0.00") " OK"); } else { Log("FREEZE :" (intFREEZE / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intCURVE / 100 == 2.01) { Log("CURVE :" (intCURVE / 100).ToString("0.00") " OK"); } else { Log("CURVE :" (intCURVE / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intEventRec / 100 == 2.01) { Log("EventRec :" (intEventRec / 100).ToString("0.00") " OK"); } else { Log("EventRec :" (intEventRec / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intRS232 / 100 == 1.12) { Log("RS232 :" (intRS232 / 100).ToString("0.00") " OK"); } else { Log("RS232 :" (intRS232 / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intGPRS1 / 100 == 1.12) { Log("GPRS1 :" (intGPRS1 / 100).ToString("0.00") " OK"); } else { Log("GPRS1 :" (intGPRS1 / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intINFRARED / 100 == 0) { Log("INFRARED :" (intINFRARED / 100).ToString("0.00") " OK"); } else { Log("INFRARED :" (intINFRARED / 100).ToString("0.00") " 进程版本不正确"); } if (intRS485 / 100 == 1.12) { Log("RS485 :" (intRS485 / 100).ToString("0.00") " OK"); } else { Log("RS485 :" (intRS485 / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intNET / 100 == 1.12) { Log("NET :" (intNET / 100).ToString("0.00") " OK"); } else { Log("NET :" (intNET / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intREAD / 100 == 2.03) { Log("READ :" (intREAD / 100).ToString("0.00") " OK"); } else { Log("READ :" (intREAD / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intREAD2 / 100 == 2.03) { Log("READ2 :" (intREAD2 / 100).ToString("0.00") " OK"); } else { Log("READ2 :" (intREAD2 / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } if (intudisk / 100 == 1.01) { Log("udisk :" (intudisk / 100).ToString("0.00") " OK"); } else { Log("udisk :" (intudisk / 100).ToString("0.00") " 进程版本不正确"); stateVer = true; } } else { Log("接收RS232口ERROR"); return; } } #endregion foreach (byte b in buf) { builder.Append(b.ToString("X2") " "); //转换16进制 } } catch (Exception ex) { Log(ex.ToString()); } })); } catch (Exception ex) { Log(ex.ToString()); } //comm.DiscardInBuffer(); } #endregion int sendocount = 0; //数据格式转换 /// <summary> /// 得到ushort类型的高字节 /// </summary> public static byte GetHighByte(int us) { return (byte)(us >> 8); } /// <summary> /// 得到ushort类型的低字节 /// </summary> public static byte GetLowByte(int us) { return (byte)(us); } public void sendCmd(int flag) { try { state11 = 0; string strSend = ""; byte[] bufsend; if (flag == 1) //读地址 { strSend = "68 32 00 32 00 68 4B 01 41 6A 00 02 0A 70 00 00 01 0B 7F 16 "; bufsend = strHex.strToToHexByte(strSend); bufsend[0] = 0x68; bufsend[1] = 0x32; bufsend[2] = 0x00; bufsend[3] = 0x32; bufsend[4] = 0x00; bufsend[5] = 0x68; bufsend[6] = 0x4B; // bufsend[7] = 0x01; // bufsend[8] = 0x41; bufsend[7] = GetLowByte(Convert.ToInt16(txtaddr11.Text, 16)); bufsend[8] = GetHighByte(Convert.ToInt16(txtaddr11.Text, 16)); bufsend[9] = 0xFF; bufsend[10] = 0xFF; bufsend[11] = 0x01; bufsend[12] = 0x0A; bufsend[13] = 0x70; bufsend[14] = 0x00; bufsend[15] = 0x00; bufsend[16] = 0x01; bufsend[17] = 0x0B; //时间 bufsend[18] = 0x00; //bufsend[19] = (byte)(Convert.ToInt32(DateTime.Now.Day.ToString(), 16)); //bufsend[20] = (byte)(Convert.ToInt32(DateTime.Now.Hour.ToString(), 16)); //bufsend[21] = (byte)(Convert.ToInt32(DateTime.Now.Minute.ToString(), 16)); //bufsend[22] = (byte)(Convert.ToInt32(DateTime.Now.Second.ToString(), 16)); //bufsend[23] = 0x00; for (int i = 6; i < 18; i ) //校验和 { bufsend[18] = bufsend[i]; } bufsend[19] = 0x16; Log("发送抄读:" strHex.byteToHexStr(bufsend)); serialPort1.Write(bufsend, 0, bufsend.Length); } //催费告警投入 68 72 00 72 00 68 4A 01 41 6A 00 02 05 72 00 00 02 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 74 16 else if (flag == 2) { strSend = "68 72 00 72 00 68 4A 01 41 6A 00 02 05 72 00 00 02 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 74 16 "; bufsend = strHex.strToToHexByte(strSend); bufsend[0] = 0x68; bufsend[1] = 0x72; bufsend[2] = 0x00; bufsend[3] = 0x72; bufsend[4] = 0x00; bufsend[5] = 0x68; bufsend[6] = 0x4A; bufsend[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[9] = GetLowByte(addr2); bufsend[10] = GetHighByte(addr2); bufsend[11] = 0x02; bufsend[12] = 0x05; bufsend[13] = 0x72; bufsend[14] = 0x00; bufsend[15] = 0x00; bufsend[16] = 0x02; bufsend[17] = 0x03; bufsend[18] = 0x00; bufsend[19] = 0x00; bufsend[20] = 0x00; bufsend[21] = 0x00; bufsend[22] = 0x00; bufsend[23] = 0x00; bufsend[24] = 0x00; bufsend[25] = 0x00; bufsend[26] = 0x00; bufsend[27] = 0x00; bufsend[28] = 0x00; bufsend[29] = 0x00; bufsend[30] = 0x00; bufsend[31] = 0x00; bufsend[32] = 0x00; bufsend[33] = 0x00; bufsend[34] = 0x00; for (int i = 6; i < 34; i ) //校验和 { bufsend[34] = bufsend[i]; } bufsend[35] = 0x16; Log("发送催费告警投入:" strHex.byteToHexStr(bufsend)); serialPort1.Write(bufsend, 0, bufsend.Length); } //催费告警解除 else if (flag == 3) { strSend = "68 72 00 72 00 68 4A 01 41 6A 00 02 05 73 00 00 02 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 76 16 "; bufsend = strHex.strToToHexByte(strSend); bufsend[0] = 0x68; bufsend[1] = 0x72; bufsend[2] = 0x00; bufsend[3] = 0x72; bufsend[4] = 0x00; bufsend[5] = 0x68; bufsend[6] = 0x4A; bufsend[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[9] = GetLowByte(addr2); bufsend[10] = GetHighByte(addr2); bufsend[11] = 0x02; bufsend[12] = 0x05; bufsend[13] = 0x73; bufsend[14] = 0x00; bufsend[15] = 0x00; bufsend[16] = 0x02; bufsend[17] = 0x04; bufsend[18] = 0x00; bufsend[19] = 0x00; bufsend[20] = 0x00; bufsend[21] = 0x00; bufsend[22] = 0x00; bufsend[23] = 0x00; bufsend[24] = 0x00; bufsend[25] = 0x00; bufsend[26] = 0x00; bufsend[27] = 0x00; bufsend[28] = 0x00; bufsend[29] = 0x00; bufsend[30] = 0x00; bufsend[31] = 0x00; bufsend[32] = 0x00; bufsend[33] = 0x00; bufsend[34] = 0x00; for (int i = 6; i < 34; i ) //校验和 { bufsend[34] = bufsend[i]; } bufsend[35] = 0x16; Log("解除催费告警:" strHex.byteToHexStr(bufsend)); serialPort1.Write(bufsend, 0, bufsend.Length); } //读取进程版本 else if (flag == 4) { strSend = "68 3E 00 3E 00 68 4B 01 41 6F 00 02 AA 60 00 00 01 1E 80 06 80 2D 16 "; bufsend = strHex.strToToHexByte(strSend); bufsend[0] = 0x68; bufsend[1] = 0x3E; bufsend[2] = 0x00; bufsend[3] = 0x3E; bufsend[4] = 0x00; bufsend[5] = 0x68; bufsend[6] = 0x4B; bufsend[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[9] = GetLowByte(addr2); bufsend[10] = GetHighByte(addr2); bufsend[11] = 0x02; bufsend[12] = 0xAA; bufsend[13] = 0x60; bufsend[14] = 0x00; bufsend[15] = 0x00; bufsend[16] = 0x01; bufsend[17] = 0x1E; bufsend[18] = 0x80; bufsend[19] = 0x06; bufsend[20] = 0x80; bufsend[21] = 0x00; for (int i = 6; i < 21; i ) //校验和 { bufsend[21] = bufsend[i]; } bufsend[22] = 0x16; Log("读进程版本:" strHex.byteToHexStr(bufsend)); serialPort1.Write(bufsend, 0, bufsend.Length); } else if (flag == 5) //设置告警时段24不告警 { strSend = "68 7E 00 7E 00 68 5A 01 41 1B 00 02 04 60 00 00 40 02 FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5C 16 "; bufsend = strHex.strToToHexByte(strSend); bufsend[0] = 0x68; bufsend[1] = 0x7E; bufsend[2] = 0x00; bufsend[3] = 0x7E; bufsend[4] = 0x00; bufsend[5] = 0x68; bufsend[6] = 0x5A; bufsend[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[9] = GetLowByte(addr2); bufsend[10] = GetHighByte(addr2); bufsend[11] = 0x02; bufsend[12] = 0x04; bufsend[13] = 0x60; bufsend[14] = 0x00; bufsend[15] = 0x00; bufsend[16] = 0x40; bufsend[17] = 0x02; bufsend[18] = 0x00; bufsend[19] = 0x00; bufsend[20] = 0x00; bufsend[21] = 0x00; bufsend[22] = 0x00; bufsend[23] = 0x00; bufsend[24] = 0x00; bufsend[25] = 0x00; bufsend[26] = 0x00; bufsend[27] = 0x00; bufsend[28] = 0x00; bufsend[29] = 0x00; bufsend[30] = 0x00; bufsend[31] = 0x00; bufsend[32] = 0x00; bufsend[33] = 0x00; bufsend[34] = 0x00; bufsend[35] = 0x00; bufsend[36] = 0x00; bufsend[37] = 0x00; for (int i = 6; i < 37; i ) //校验和 { bufsend[37] = bufsend[i]; } bufsend[38] = 0x16; Log("发送24小时不告警:" strHex.byteToHexStr(bufsend)); serialPort1.Write(bufsend, 0, bufsend.Length); } else if (flag == 6) //设置告警24小时告警时段 { strSend = "68 7E 00 7E 00 68 5A 01 41 1B 00 02 04 60 00 00 40 02 FF FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 5C 16 "; bufsend = strHex.strToToHexByte(strSend); bufsend[0] = 0x68; bufsend[1] = 0x7E; bufsend[2] = 0x00; bufsend[3] = 0x7E; bufsend[4] = 0x00; bufsend[5] = 0x68; bufsend[6] = 0x5A; bufsend[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[9] = GetLowByte(addr2); bufsend[10] = GetHighByte(addr2); bufsend[11] = 0x02; bufsend[12] = 0x04; bufsend[13] = 0x60; bufsend[14] = 0x00; bufsend[15] = 0x00; bufsend[16] = 0x40; bufsend[17] = 0x02; bufsend[18] = 0xFF; bufsend[19] = 0xFF; bufsend[20] = 0xFF; bufsend[21] = 0x00; bufsend[22] = 0x00; bufsend[23] = 0x00; bufsend[24] = 0x00; bufsend[25] = 0x00; bufsend[26] = 0x00; bufsend[27] = 0x00; bufsend[28] = 0x00; bufsend[29] = 0x00; bufsend[30] = 0x00; bufsend[31] = 0x00; bufsend[32] = 0x00; bufsend[33] = 0x00; bufsend[34] = 0x00; bufsend[35] = 0x00; bufsend[36] = 0x00; bufsend[37] = 0x00; for (int i = 6; i < 37; i ) //校验和 { bufsend[37] = bufsend[i]; } bufsend[38] = 0x16; Log("发送24小时告警:" strHex.byteToHexStr(bufsend)); serialPort1.Write(bufsend, 0, bufsend.Length); } //68 A2 00 A2 00 68 4A 01 41 6F 00 02 05 F0 00 00 40 03 00 32 08 16 23 15 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 32 08 16 00 0D 16 else if (flag == 7) //校时 { strSend = "68 A2 00 A2 00 68 4A 01 41 6F 00 02 05 F0 00 00 40 03 00 32 08 16 23 15 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 32 08 16 00 0D 16 "; bufsend = strHex.strToToHexByte(strSend); bufsend[0] = 0x68; bufsend[1] = 0xA2; bufsend[2] = 0x00; bufsend[3] = 0xA2; bufsend[4] = 0x00; bufsend[5] = 0x68; bufsend[6] = 0x4A; bufsend[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[9] = GetLowByte(addr2); bufsend[10] = GetHighByte(addr2); bufsend[11] = 0x02; bufsend[12] = 0x05; bufsend[13] = 0xF0; bufsend[14] = 0x00; bufsend[15] = 0x00; bufsend[16] = 0x40; bufsend[17] = 0x03; // string datet = DateTime.Now.ToString(); bufsend[18] = (byte)(Convert.ToInt32(DateTime.Now.Second.ToString(), 16)); bufsend[19] = (byte)(Convert.ToInt32(DateTime.Now.Minute.ToString(), 16)); bufsend[20] = (byte)(Convert.ToInt32(DateTime.Now.Hour.ToString(), 16)); bufsend[21] = (byte)(Convert.ToInt32(DateTime.Now.Day.ToString(), 16)); bufsend[22] = (byte)(Convert.ToInt32(DateTime.Now.Month.ToString(), 16)); bufsend[23] = (byte)(Convert.ToInt32(DateTime.Now.Year.ToString().Substring(2, 2), 16)); bufsend[24] = 0x00; bufsend[25] = 0x00; bufsend[26] = 0x00; bufsend[27] = 0x00; bufsend[28] = 0x00; bufsend[29] = 0x00; bufsend[30] = 0x00; bufsend[31] = 0x00; bufsend[32] = 0x00; bufsend[33] = 0x00; bufsend[34] = 0x00; bufsend[35] = 0x00; bufsend[36] = 0x00; bufsend[37] = 0x00; bufsend[38] = 0x00; bufsend[39] = 0x00; bufsend[40] = 0x00; bufsend[41] = (byte)(Convert.ToInt32(DateTime.Now.Second.ToString(), 16)); bufsend[42] = (byte)(Convert.ToInt32(DateTime.Now.Minute.ToString(), 16)); bufsend[43] = (byte)(Convert.ToInt32(DateTime.Now.Hour.ToString(), 16)); bufsend[44] = (byte)(Convert.ToInt32(DateTime.Now.Day.ToString(), 16)); bufsend[45] = 0x00; bufsend[46] = 0x00; for (int i = 6; i < 46; i ) //校验和 { bufsend[46] = bufsend[i]; } bufsend[47] = 0x16; Log("校时:" strHex.byteToHexStr(bufsend)); serialPort1.Write(bufsend, 0, bufsend.Length); } // => 68 32 00 32 00 68 5B 01 41 13 00 02 0C 60 00 00 02 00 20 16 时钟 else if (flag == 8) { strSend = "68 32 00 32 00 68 5B 01 41 13 00 02 0C 60 00 00 02 00 20 16 "; bufsend = strHex.strToToHexByte(strSend); bufsend[0] = 0x68; bufsend[1] = 0x32; bufsend[2] = 0x00; bufsend[3] = 0x32; bufsend[4] = 0x00; bufsend[5] = 0x68; bufsend[6] = 0x5B; bufsend[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); bufsend[9] = GetLowByte(addr2); bufsend[10] = GetHighByte(addr2); bufsend[11] = 0x02; bufsend[12] = 0x0C; bufsend[13] = 0x60; bufsend[14] = 0x00; bufsend[15] = 0x00; bufsend[16] = 0x02; bufsend[17] = 0x00; bufsend[18] = 0x00; for (int i = 6; i < 18; i ) //校验和 { bufsend[18] = bufsend[i]; } bufsend[19] = 0x16; Log("发送召测时钟:" strHex.byteToHexStr(bufsend)); serialPort1.Write(bufsend, 0, bufsend.Length); } //=> 68 E2 00 E2 00 68 7A 01 41 1B 00 02 04 60 00 00 04 00 DE 28 11 94 06 27 DE 28 11 94 06 27 43 4D 4E 45 54 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 68 16 IP 备用ip else if (flag == 9) { string[] zhuip = txtIP1.Text.Split('.'); string[] beiip = txtip2.Text.Split('.'); int port1 = Convert.ToInt32(txtport1.Text); int port2 = Convert.ToInt32(txtport2.Text); byte[] buf = new byte[30 32 6 2]; buf[0] = 0x68; buf[1] = 0xfa; buf[2] = 0x00; buf[3] = 0xfa; buf[4] = 0x00; buf[5] = 0x68; buf[6] = 0x4a; buf[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); buf[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); buf[9] = GetLowByte(Convert.ToInt32(addr2)); buf[10] = GetHighByte(Convert.ToInt32(addr2)); buf[11] = 0x02; buf[12] = 0x04; buf[13] = 0xf0; buf[14] = 0x00; buf[15] = 0x00; buf[16] = 0x04; buf[17] = 0x00; buf[18] = (byte)Convert.ToInt32(zhuip[0]); buf[19] = (byte)Convert.ToInt32(zhuip[1]); buf[20] = (byte)Convert.ToInt32(zhuip[2]); buf[21] = (byte)Convert.ToInt32(zhuip[3]); buf[22] = GetLowByte(port1); buf[23] = GetHighByte(port1); buf[24] = (byte)Convert.ToInt32(beiip[0]); buf[25] = (byte)Convert.ToInt32(beiip[1]); buf[26] = (byte)Convert.ToInt32(beiip[2]); buf[27] = (byte)Convert.ToInt32(beiip[3]); buf[28] = GetLowByte(port2); buf[29] = GetHighByte(port2); byte[] apn = System.Text.Encoding.ASCII.GetBytes(txtapn1.Text); apn.CopyTo(buf, 30); buf[buf.Length - 8] = 0x00; buf[buf.Length - 7] = (byte)Convert.ToInt32((DateTime.Now.Second).ToString(), 16); buf[buf.Length - 6] = (byte)Convert.ToInt32((DateTime.Now.Minute).ToString(), 16); buf[buf.Length - 5] = (byte)Convert.ToInt32((DateTime.Now.Hour).ToString(), 16); buf[buf.Length - 4] = (byte)Convert.ToInt32((DateTime.Now.Day).ToString(), 16); buf[buf.Length - 3] = 0x00; for (int s = 6; s < buf.Length - 2; s ) //校验和 { buf[buf.Length - 2] = (byte)(buf[buf.Length - 2] buf[s]); } buf[buf.Length - 1] = 0x16; Log("发送设置主站IP:" strHex.byteToHexStr(buf)); serialPort1.Write(buf, 0, buf.Length); } //68 9A 00 9A 00 68 4A 17 02 81 24 02 04 F0 00 00 01 0B 01 41 EA 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 56 39 11 16 00 EF 16 修改终端逻辑地 //68 82 00 82 00 68 4A 17 02 81 24 02 04 70 00 00 01 0B 01 41 EA 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 EF 16 else if (flag == 10) { byte[] buf = new byte[40]; buf[0] = 0x68; buf[1] = 0x82; buf[2] = 0x00; buf[3] = 0x82; buf[4] = 0x00; buf[5] = 0x68; buf[6] = 0x4a; buf[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); buf[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); buf[9] = GetLowByte(Convert.ToInt32(addr2)); buf[10] = GetHighByte(Convert.ToInt32(addr2)); buf[11] = 0x02; buf[12] = 0x04; buf[13] = 0x70; buf[14] = 0x00; buf[15] = 0x00; buf[16] = 0x01; buf[17] = 0x0B; buf[18] = GetLowByte(Convert.ToInt16(txtNewaddr1.Text, 16)); buf[19] = GetHighByte(Convert.ToInt16(txtNewaddr1.Text, 16)); buf[20] = GetLowByte(Convert.ToInt32(txtNewaddr2.Text)); buf[21] = GetHighByte(Convert.ToInt32(txtNewaddr2.Text)); buf[22] = 0x00; buf[23] = 0x00; buf[24] = 0x00; buf[25] = 0x00; buf[26] = 0x00; buf[27] = 0x00; buf[28] = 0x00; buf[29] = 0x00; buf[30] = 0x00; buf[31] = 0x00; buf[32] = 0x00; buf[33] = 0x00; buf[34] = 0x00; buf[35] = 0x00; buf[36] = 0x00; buf[37] = 0x00; //buf[38] = 0x00; //buf[39] = (byte)(Convert.ToInt32(DateTime.Now.Second.ToString(), 16)); //buf[40] = (byte)(Convert.ToInt32(DateTime.Now.Minute.ToString(), 16)); //buf[41] = (byte)(Convert.ToInt32(DateTime.Now.Hour.ToString(), 16)); //buf[42] = (byte)(Convert.ToInt32(DateTime.Now.Day.ToString(), 16)); //buf[43] = 0x00; buf[38] = 0x00; for (int s = 6; s < 38; s ) //校验和 { buf[38] = (byte)(buf[buf.Length - 2] buf[s]); } buf[buf.Length - 1] = 0x16; Log("发送设置终端地址:" strHex.byteToHexStr(buf)); serialPort1.Write(buf, 0, buf.Length); } //厂内初始化 68 72 00 72 00 68 41 01 41 6F 00 02 01 60 00 00 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 8F 16 else if (flag == 11) { byte[] buf = new byte[36]; buf[0] = 0x68; buf[1] = 0x72; buf[2] = 0x00; buf[3] = 0x72; buf[4] = 0x00; buf[5] = 0x68; buf[6] = 0x41; if (txtNewaddr1.Text != "" && txtNewaddr2.Text != "") { buf[7] = GetLowByte(Convert.ToInt16(txtNewaddr1.Text.ToString(), 16)); buf[8] = GetHighByte(Convert.ToInt16(txtNewaddr1.Text.ToString(), 16)); buf[9] = GetLowByte(Convert.ToInt32(txtNewaddr2.Text)); buf[10] = GetHighByte(Convert.ToInt32(txtNewaddr2.Text)); } else { buf[7] = GetLowByte(Convert.ToInt16(addr1.ToString(), 16)); buf[8] = GetHighByte(Convert.ToInt16(addr1.ToString(), 16)); buf[9] = GetLowByte(Convert.ToInt32(addr2)); buf[10] = GetHighByte(Convert.ToInt32(addr2)); } buf[11] = 0x02; buf[12] = 0x01; buf[13] = 0x60; buf[14] = 0x00; buf[15] = 0x00; buf[16] = 0x20; buf[17] = 0x00; buf[18] = 0x00; buf[19] = 0x00; buf[20] = 0x00; buf[21] = 0x00; buf[22] = 0x00; buf[23] = 0x00; buf[24] = 0x00; buf[25] = 0x00; buf[26] = 0x00; buf[27] = 0x00; buf[28] = 0x00; buf[29] = 0x00; buf[30] = 0x00; buf[31] = 0x00; buf[32] = 0x00; buf[33] = 0x00; //buf[34] = 0x00; //buf[35] = (byte)(Convert.ToInt32(DateTime.Now.Second.ToString(), 16)); //buf[36] = (byte)(Convert.ToInt32(DateTime.Now.Minute.ToString(), 16)); //buf[37] = (byte)(Convert.ToInt32(DateTime.Now.Hour.ToString(), 16)); //buf[38] = (byte)(Convert.ToInt32(DateTime.Now.Day.ToString(), 16)); //buf[39] = 0x00; buf[34] = 0x00; for (int s = 6; s < 34; s ) //校验和 { buf[buf.Length - 2] = (byte)(buf[buf.Length - 2] buf[s]); } buf[buf.Length - 1] = 0x16; Log("发送厂内初始化:" strHex.byteToHexStr(buf)); serialPort1.Write(buf, 0, buf.Length); } } catch (Exception ex) { MessageBox.Show("Error" ex, "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void buttonOpen_Click(object sender, EventArgs e) { try { if (buttonOpen.Text == "打开") { serialPort1.PortName = comPN.Text; serialPort1.BaudRate = Convert.ToInt16(Bdrate.Text); serialPort1.Parity = Parity.Even; //效验码 serialPort1.DataBits = 8; //数据位 serialPort1.StopBits = StopBits.One; //停止位 } if (!serialPort1.IsOpen) { serialPort1.Open(); comPN.Enabled = false; Bdrate.Enabled = false; buttonOpen.Text = "关闭"; MessageBox.Show("串口打开成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); ini.WriteString("NetworkConfiguration", "COM", comPN.Text); // toolStripStatusLabel1.Text = "串口号:" COM " 波特率:" BaudRate "状态:已打开"; } else { comPN.Enabled = true; Bdrate.Enabled = true; button4.Enabled = true; serialPort1.Close(); buttonOpen.Text = "打开"; MessageBox.Show("串口关闭成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); //toolStripStatusLabel1.Text = "串口号:" COM " 波特率:" BaudRate "状态:已关闭"; } } catch (Exception ex) { MessageBox.Show("Operation error" ex.ToString(), "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information); } } private void btnRS232_Click(object sender, EventArgs e) { sendCmd(1); } private void richTextBox1_MouseDoubleClick(object sender, MouseEventArgs e) { richTextBox1.Clear(); } IniFiles ini = new IniFiles("config/tcpserver.ini"); private void Form1_Load(object sender, EventArgs e) { txtIP1.Text = this.ini.ReadString("NetworkConfiguration", "ip1", ""); txtip2.Text = this.ini.ReadString("NetworkConfiguration", "ip2", ""); txtport1.Text = this.ini.ReadString("NetworkConfiguration", "port1", "9990"); txtport2.Text = this.ini.ReadString("NetworkConfiguration", "port2", "9990"); txtapn1.Text = this.ini.ReadString("NetworkConfiguration", "apn1", "9990"); Bdrate.SelectedIndex = 2; //串口加载 string[] ports = SerialPort.GetPortNames(); Array.Sort(ports); comPN.Items.AddRange(ports); try { comPN.Text = this.ini.ReadString("NetworkConfiguration", "COM", "COM1"); } catch (Exception ex) { } // txtapn2.Text = this.ini.ReadString("NetworkConfiguration", "apn2", "9990"); //txtPort.Text = this.ini.ReadString("NetworkConfiguration", "Port", "9990"); //modeSql = this.ini.ReadString("NetworkConfiguration", "modeSql", "update"); //DelayOnline = this.ini.ReadString("NetworkConfiguration", "DelayOnline", "2"); //ForwardingPort = this.ini.ReadString("NetworkConfiguration", "ForwardingPort", "8001"); //转发端口 //noexe = this.ini.ReadString("NetworkConfiguration", "NoExe", "1"); } private void btnstart_Click(object sender, EventArgs e) { sendCmd(2); } private void btnstop_Click(object sender, EventArgs e) { sendCmd(3); } private void button4_Click(object sender, EventArgs e) { rowsCount = 0; try { button4.Enabled = true; state = false; if (!serialPort1.IsOpen) { MessageBox.Show("Close success", "Prompt", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (state == false) { sendCmd(1); //if (state11 == false) retrun; //delayTime(5); while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1 || addr2.ToString() == "0") { Log("地址读取失败,请检测RS232设置!"); button4.Enabled = true; // serialPort1.Close(); return; //buttonOpen.Text = "Open"; } state = true; //delayTime(5); } if (state == true) { if (checkBox6.Checked == true) { if (txtIP1.Text != "" && txtport1.Text != "" && txtapn1.Text != "") { sendCmd(9); //delayTime(5); while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } } else { MessageBox.Show("主ip、端口、apn不能为空"); state = false; button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } } if (checkBox1.Checked == true) { //催费告警 sendCmd(2); while (true) if (state11 != 0) break; else delayTime(1); // delayTime(5); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } } //校时 sendCmd(7); while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } //delayTime(5); sendCmd(8); while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } //delayTime(5); if (checkBox4.Checked == true) { sendCmd(6); while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } delayTime(Convert.ToInt32(txtsecond.Text)); } if (checkBox3.Checked == true) { sendCmd(3); while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } // delayTime(5); } if (checkBox2.Checked == true) { sendCmd(4); // MessageBox.Show(stateVer.ToString()); while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } if (stateVer == true) { MessageBox.Show("默认版本不一致,请检查!!"); //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } delayTime(3); } if (checkBox5.Checked == true) { if (txtNewaddr1.Text != "" && txtNewaddr2.Text != "") { sendCmd(10); while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } //delayTime(5); } else { MessageBox.Show("终端区划码和地址不能为空"); state = false; button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } } if (checkBox7.Checked == true) { sendCmd(11); rowsCount = 7; while (true) if (state11 != 0) break; else delayTime(1); if (state11 == 1) { Log("未接收到数据!"); button4.Enabled = true; //serialPort1.Close(); //buttonOpen.Text = "Open"; return; } //delayTime(120); } txtaddr1.Text = ""; txtaddr2.Text = ""; state = false; //System.Threading.Thread.Sleep(2000); //serialPort1.Close(); //System.Threading.Thread.Sleep(2000); // buttonOpen.Text = "Open"; return; } } } catch (Exception ex) { MessageBox.Show("出现错误" ex.ToString()); } } private void delayTime(double secend) { DateTime tempTime = DateTime.Now; while (tempTime.AddSeconds(secend).CompareTo(DateTime.Now) > 0) Application.DoEvents(); } private void button3_Click(object sender, EventArgs e) { sendCmd(4); } private void button1_Click(object sender, EventArgs e) { sendCmd(6); } private void button5_Click(object sender, EventArgs e) { sendCmd(7); } private void button2_Click(object sender, EventArgs e) { sendCmd(8); } private void btnsetIP_Click(object sender, EventArgs e) { sendCmd(9); } private void btnSetaddr_Click(object sender, EventArgs e) { sendCmd(10); } private void button6_Click(object sender, EventArgs e) { sendCmd(11); } private void Form1_KeyPress(object sender, KeyPressEventArgs e) { } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F1) { button4_Click(null, null); } } private void timer1_Tick(object sender, EventArgs e) { } } }
标签: RS232
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论