public Form1() { InitializeComponent(); //CurTempNodID = 0; //CurPumpNodeID = 0; CurTelosbID = 0; ComSet comset = new ComSet(ref this.ComPort1); comset.ShowDialog(); if (ComPort1.IsOpen == true) { ComPort1.DiscardInBuffer(); } ComPort1.ReadTimeout = 50; comboBoxT.Sorted = true; //comboBoxP.Sorted = true; string curdirectory = Directory.GetCurrentDirectory(); string newdirectory; newdirectory = curdirectory + "\\Config"; if (Directory.Exists(newdirectory)) { /* * if (File.Exists(newdirectory + "\\Temperature.config") == true) * { * ReadConfig(newdirectory + "\\Temperature.config", ref Temperatureconfig, 1); * } * if (File.Exists(newdirectory + "\\Flowrate.config") == true) * { * ReadConfig(newdirectory + "\\Flowrate.config", ref Flowrateconfig, 2); * } */ if (File.Exists(newdirectory + "\\Telosb.config") == true) { ReadConfig(newdirectory + "\\Telosb.config", ref Telosbconfig, 3); } } threadCom1RecvEn = true; threadCom1Recv = new Thread(ThreadCom1Recv); threadCom1Recv.Start(); }
private void ThreadCom1Recv() { byte temp; UInt16 len = 0; UInt16 Type; byte[] SerialRxBuf = new byte[64]; int SerialCount = 0; byte RxSum = 0; MethodInvoker mi = new MethodInvoker(Com1InvokeFun); while (threadCom1RecvEn == true && ComPort1.IsOpen == true) { try { temp = (byte)ComPort1.ReadByte(); if (SerialCount == 0) { if (temp == 0xAA) { SerialRxBuf[SerialCount] = temp; SerialCount++; RxSum = 0xAA; } } else if (SerialCount == 1) { if (temp == 0x55) { SerialRxBuf[SerialCount] = temp; SerialCount++; RxSum += temp; } else if (temp != 0xAA) { SerialCount = 0; } } else if (SerialCount == 2) { len = (UInt16)temp; if (len > 60) { SerialCount = 0; } else { SerialRxBuf[SerialCount] = temp; RxSum += temp; SerialCount++; } } else if (SerialCount < len) { SerialRxBuf[SerialCount] = temp; RxSum += temp; SerialCount++; if (SerialCount == len) { SerialCount = 0; if (RxSum == 0x00) { Type = (UInt16)SerialRxBuf[6]; Type += (UInt16)(((UInt16)SerialRxBuf[7]) << 8); //if (Type == 0x0000 || Type == 0x0001 || Type == 0x0100 || Type == 0x0200 || Type == 0x0300 || Type == 0x0400 || Type == 0x0401) if (Type == 0x0400) { for (int i = 4; i < len; i++) { InBuf1[i - 4] = SerialRxBuf[i]; } BeginInvoke(mi); } } } } else { SerialCount = 0; if (temp == 0xAA) { SerialRxBuf[SerialCount] = temp; SerialCount++; } } } catch { } } }