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; } }
private void writeSingleData(int Index) { byte[] sendbuf = new byte[17]; sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.writeCmd; sendbuf[2] = (byte)((Global.offset + Global.sizeDistance * Index) / 256); sendbuf[3] = (byte)((Global.offset + Global.sizeDistance * Index) % 256); sendbuf[4] = 0x00; sendbuf[5] = 0x04; sendbuf[6] = 0x08; //双字,字间是低位在前高位在后,字内是高位在前低位在后 3412顺序 sendbuf[7] = (byte)((Global.procData[Index].size << 16) >> 24); sendbuf[8] = (byte)((Global.procData[Index].size << 24) >> 24); sendbuf[9] = (byte)(Global.procData[Index].size >> 24); sendbuf[10] = (byte)((Global.procData[Index].size << 8) >> 24); sendbuf[11] = (byte)((Global.procData[Index].planCount << 16) >> 24); sendbuf[12] = (byte)((Global.procData[Index].planCount << 24) >> 24); sendbuf[13] = (byte)(Global.procData[Index].planCount >> 24); sendbuf[14] = (byte)((Global.procData[Index].planCount << 8) >> 24); uint crc = SystemUnit.getCRC(sendbuf, 0, 15); sendbuf[15] = (byte)(crc / 256); sendbuf[16] = (byte)(crc % 256); comm.SendControlCmd(sendbuf, sendbuf.Length); }
private void readPinData() { byte[] sendbuf = new byte[8]; sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.readCmd; sendbuf[2] = (byte)((Global.pinOffset) / 256); sendbuf[3] = (byte)((Global.pinOffset) % 256); sendbuf[4] = 0x00; sendbuf[5] = (byte)Global.dataCount; uint crc = SystemUnit.getCRC(sendbuf, 0, 6); sendbuf[6] = (byte)(crc / 256); sendbuf[7] = (byte)(crc % 256); comm.SendControlCmd(sendbuf, 8); }
private void readSingleData(int Index) { byte[] sendbuf = new byte[8]; sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.readCmd; sendbuf[2] = (byte)((Global.offset + Global.sizeDistance * Index) / 256); sendbuf[3] = (byte)((Global.offset + Global.sizeDistance * Index) % 256); sendbuf[4] = 0x00; sendbuf[5] = 4; uint crc = SystemUnit.getCRC(sendbuf, 0, 6); sendbuf[6] = (byte)(crc / 256); sendbuf[7] = (byte)(crc % 256); comm.SendControlCmd(sendbuf, 8); }
private void writePinData() { byte[] sendbuf = new byte[9 + Global.dataCount * 2]; sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.writeCmd; sendbuf[2] = (byte)(Global.pinOffset / 256); sendbuf[3] = (byte)(Global.pinOffset % 256); sendbuf[4] = (byte)(Global.dataCount / 256); sendbuf[5] = (byte)(Global.dataCount % 256); sendbuf[6] = (byte)(Global.dataCount * 2); for (int i = 0; i < Global.dataCount; i++) { sendbuf[7 + i * 2] = (byte)(Global.procData[i].pin / 256); sendbuf[7 + i * 2 + 1] = (byte)(Global.procData[i].pin % 256); } uint crc = SystemUnit.getCRC(sendbuf, 0, 7 + Global.dataCount * 2); sendbuf[7 + Global.dataCount * 2] = (byte)(crc / 256); sendbuf[8 + Global.dataCount * 2] = (byte)(crc % 256); comm.SendControlCmd(sendbuf, 9 + Global.dataCount * 2); }
private void WriteAllPlanData() { byte[] sendbuf = new byte[4 * Global.dataCount + 9]; sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.writeCmd; sendbuf[2] = (byte)((Global.planOffset) / 256); sendbuf[3] = (byte)((Global.planOffset) % 256); sendbuf[4] = 0x00; sendbuf[5] = (byte)(Global.dataCount * 2); sendbuf[6] = (byte)(Global.dataCount * 4); //双字,字间是低位在前高位在后,字内是高位在前低位在后 3412顺序 for (int i = 0; i < Global.dataCount; i++) { sendbuf[7 + i * 4] = (byte)((Global.procData[i].planCount << 16) >> 24); sendbuf[8 + i * 4] = (byte)((Global.procData[i].planCount << 24) >> 24); sendbuf[9 + i * 4] = (byte)(Global.procData[i].planCount >> 24); sendbuf[10 + i * 4] = (byte)((Global.procData[i].planCount << 8) >> 24); } uint crc = SystemUnit.getCRC(sendbuf, 0, 4 * Global.dataCount + 7); sendbuf[4 * Global.dataCount + 7] = (byte)(crc / 256); sendbuf[4 * Global.dataCount + 8] = (byte)(crc % 256); comm.SendControlCmd(sendbuf, sendbuf.Length); }