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); }
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 btnImport_Click(object sender, EventArgs e) { DataTable dt = null; OpenFileDialog folder = new OpenFileDialog(); if (folder.ShowDialog() == DialogResult.OK) { String path = folder.FileName; dt = SystemUnit.importData(path); } else { return; } if (null == dt) { showInfo("导入数据失败", true, false); return; } DataTable[] dts = DBHelp.devideTable(dt); DGV1.DataSource = dts[0]; DGV2.DataSource = dts[1]; setDGV(DGV1); setDGV(DGV2); showInfo("导入数据成功", true, true); toolImportFile.Text = "数据来源:" + folder.SafeFileName; toolImportFile.ForeColor = toolbarReadIndex.ForeColor; }
private void procData(byte[] buff) { //if (SystemUnit.getCRC(buff, 0, buff.Length - 2) != (buff[buff.Length - 2] << 8 | buff[buff.Length - 1])) //{ // Log.WriteLog("CommPort", "ERROR", "【procData】校验失败!接收数据:" + SystemUnit.ToHexString(buff)); // return; //} //读数据模式下回复 if (buff[1] == 0x03 && buff[2] == Global.sizeDistance) { if (Global.runMode == RunMode.READSIZE) { for (int i = 0; i < Global.dataCount; i++) { Global.procData[i].size = (buff[3 + 4 * i] << 8 | buff[4 + 4 * i] | buff[5 + 4 * i] << 24 | buff[6 + 4 * i] << 16); } SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READSIZEBACK, (int)ReturnResult.SUCCESS, (int)DataType.SIZE); } else if (Global.runMode == RunMode.READPLAN) { for (int i = 0; i < Global.dataCount; i++) { Global.procData[i].planCount = (buff[3 + 4 * i] << 8 | buff[4 + 4 * i] | buff[5 + 4 * i] << 24 | buff[6 + 4 * i] << 16); } SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READPLANBACK, (int)ReturnResult.SUCCESS, (int)DataType.PLAN); } // if(buff[2] == Global.dataCount*2) // { // for(int i =0;i< Global.dataCount; i++) // { // Global.procData[i].pin = (short)(buff[3 + 2 * i] << 8 | buff[3 + 2 * i + 1]); // } // SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READBACK, (int)ReturnResult.SUCCESS, (int)DataType.PIN); // } } //写入数据时回复 if (Global.runMode == RunMode.WRITESIZE && buff[1] == 0x10) { int offset = buff[2] << 8 | buff[3]; if (offset == Global.sizeOffset) { SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_WRITESIZEBACK, (int)ReturnResult.SUCCESS, (int)DataType.SIZE); } } else if (Global.runMode == RunMode.WRITEPLAN && buff[1] == 0x10) { int offset = buff[2] << 8 | buff[3]; if (offset == Global.planOffset) { SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_WRITEPLANBACK, (int)ReturnResult.SUCCESS, (int)DataType.PLAN); } } }
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); }
//--------------------------------------------------------------- //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); } }
private bool exportData(string path) { try { DataTable[] dts = new DataTable[2]; dts[0] = (DataTable)DGV1.DataSource; dts[1] = (DataTable)DGV2.DataSource; DataTable dt = DBHelp.mergeTable(dts); return(SystemUnit.ExportDataToExcel(dt, path)); } catch (Exception ex) { Log.WriteLog("MainForm", "ERROR", "【exportData】 " + ex.Message); return(false); } }
public void appInit() { getParamFromXML(); setFormStatus(0); DataTable dt = SystemUnit.importData(Global.defaultPath); if (null == dt) { MessageBox.Show("初始化默认数据失败,请通过【导入表格】进行数据初始化!"); return; } DataTable[] dts = DBHelp.devideTable(dt); DGV1.DataSource = dts[0]; DGV2.DataSource = dts[1]; setDGV(DGV1); setDGV(DGV2); toolImportFile.Text = "数据来源:默认表格"; toolImportFile.ForeColor = toolbarReadIndex.ForeColor; }
private void procData(byte[] buff) { //if (SystemUnit.getCRC(buff, 0, buff.Length - 2) != (buff[buff.Length - 2] << 8 | buff[buff.Length - 1])) //{ // Log.WriteLog("CommPort", "ERROR", "【procData】校验失败!接收数据:" + SystemUnit.ToHexString(buff)); // return; //} //读数据模式下回复 if (buff[1] == 0x03 && Global.runMode == RunMode.READ) { if (buff[2] == 8) { Global.procData[Global.readIndex].size = (buff[3] << 8 | buff[4] | buff[5] << 24 | buff[6] << 16); Global.procData[Global.readIndex].planCount = (buff[7] << 8 | buff[8] | buff[9] << 24 | buff[10] << 16); SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READBACK, (int)ReturnResult.SUCCESS, (int)DataType.SIZEANDPLAN); } if (buff[2] == Global.dataCount * 2) { for (int i = 0; i < Global.dataCount; i++) { Global.procData[i].pin = (short)(buff[3 + 2 * i] << 8 | buff[3 + 2 * i + 1]); } SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READBACK, (int)ReturnResult.SUCCESS, (int)DataType.PIN); } } //写入数据时回复 if (Global.runMode == RunMode.WRITE && buff[1] == 0x10) { int offset = buff[2] << 8 | buff[3]; if (Global.writeIndex == Global.dataCount && offset == Global.pinOffset) { SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_WRITEBACK, (int)ReturnResult.SUCCESS, (int)DataType.PIN); } else if (offset == Global.offset + 6 * Global.writeIndex) { SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_WRITEBACK, (int)ReturnResult.SUCCESS, (int)DataType.SIZEANDPLAN); } } }
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); }