void comm_DataReceived(object sender, SerialDataReceivedEventArgs e) { int n = comm.BytesToRead; byte[] buff = new byte[n]; comm.Read(buff, 0, n); if (Global.bLogOpen) { Log.WriteLog("CommPort", "INFO", "【comm_DataReceived】 接收数据:" + SystemUnit.ToHexString(buff)); } Buffer.BlockCopy(buff, 0, curBuff, curCount, n); curCount = curCount + n; if (curCount < 5) { return; } if (SystemUnit.getCRC(curBuff, 0, curCount - 2) == (curBuff[curCount - 2] << 8 | curBuff[curCount - 1])) { byte[] proBuff = new byte[curCount]; Buffer.BlockCopy(curBuff, 0, proBuff, 0, curCount); curCount = 0; procData(proBuff); } if (curCount > 256) { curCount = 0; } }
//--------------------------------------------------------------- //FUNCTION: send command to the serial port //IN: // byte[] buf, the buffer for the command // len, the length of command //OUT: true for success, false for failed //--------------------------------------------------------------- public bool SendControlCmd(byte[] buf, int len) { if (comm.IsOpen) { comm.Write(buf, 0, len); if (Global.bLogOpen) { Log.WriteLog("CommPort", "INFO", "【SendControlCmd】 发送数据:" + SystemUnit.ToHexString(buf)); } return(true); } else { return(false); } }