private void SendRestData() { StringBuilder sb = new StringBuilder(); sb.Append("update workorder set status = 2 where id in ("); DataTable dt = Global.mysqlHelper.GetDataTable("select * from workorder where status = 1 and thickness = " + comboThickness.Text + " order by id asc"); byte[] sendbuf = new byte[400]; int count = dt.Rows.Count; if (count > (30 - Global.dataCount)) { count = 30 - Global.dataCount; } sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.writeCmd; sendbuf[2] = (byte)((Global.offset + Global.dataCount * 6) / 256); sendbuf[3] = (byte)((Global.offset + Global.dataCount * 6) % 256); sendbuf[4] = (byte)(count * 6 / 256); sendbuf[5] = (byte)(count * 6 % 256); sendbuf[6] = (byte)(count * 12); for (int i = 0; i < count; i++) { WorkOrder order = new WorkOrder(dt.Rows[i]); sendbuf[7 + 12 * i] = (byte)((order.Length << 16) >> 24); sendbuf[7 + 12 * i + 1] = (byte)((order.Length << 24) >> 24); sendbuf[7 + 12 * i + 2] = (byte)(order.Length >> 24); sendbuf[7 + 12 * i + 3] = (byte)((order.Length << 8) >> 24); sendbuf[7 + 12 * i + 4] = (byte)((order.GrossWidth << 16) >> 24); sendbuf[7 + 12 * i + 5] = (byte)((order.GrossWidth << 24) >> 24); sendbuf[7 + 12 * i + 6] = (byte)(order.GrossWidth >> 24); sendbuf[7 + 12 * i + 7] = (byte)((order.GrossWidth << 8) >> 24); sendbuf[7 + 12 * i + 8] = (byte)((order.Done << 16) >> 24); sendbuf[7 + 12 * i + 9] = (byte)((order.Done << 24) >> 24); sendbuf[7 + 12 * i + 10] = (byte)(order.Done >> 24); sendbuf[7 + 12 * i + 11] = (byte)((order.Done << 8) >> 24); sb.Append(order.id.ToString() + ","); Global.procData[i + Global.dataCount].id = order.id; Global.procData[i + Global.dataCount].Lenth = order.Length; Global.procData[i + Global.dataCount].Thickness = order.Thickness; Global.procData[i + Global.dataCount].GrossWidth = order.GrossWidth; Global.procData[i + Global.dataCount].status = (int)OrderStatus.WORKING; } uint crc = SystemUnit.getCRC(sendbuf, 0, count * 12 + 7); sendbuf[count * 12 + 7] = (byte)(crc / 256); sendbuf[count * 12 + 8] = (byte)(crc % 256); Global.commHelper.SendControlCmd(sendbuf, count * 12 + 9); timerBreak.Enabled = true; sb.Append("0)"); Global.mysqlHelper.ExecuteSql(sb.ToString()); }
void comm_DataReceived(object sender, SerialDataReceivedEventArgs e) { int n = comm.BytesToRead; byte[] buff = new byte[n]; comm.Read(buff, 0, n); if (Global.logOpen == 1) { 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 > 250) { curCount = 0; } }
private void SendReadStatusCmd() { byte[] sendbuf = new byte[8]; sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.readCmd; sendbuf[2] = (byte)(Global.runStatusOffset / 256); sendbuf[3] = (byte)(Global.runStatusOffset % 256); sendbuf[4] = 0; sendbuf[5] = 2; uint crc = SystemUnit.getCRC(sendbuf, 0, 6); sendbuf[6] = (byte)(crc / 256); sendbuf[7] = (byte)(crc % 256); Global.commHelper.SendControlCmd(sendbuf, 8); }
private void SendReadDataCmd(int offset, int count) { byte[] sendbuf = new byte[8]; sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.readCmd; sendbuf[2] = (byte)(offset / 256); sendbuf[3] = (byte)(offset % 256); sendbuf[4] = (byte)(count * 6 / 256); sendbuf[5] = (byte)(count * 6 % 256); uint crc = SystemUnit.getCRC(sendbuf, 0, 6); sendbuf[6] = (byte)(crc / 256); sendbuf[7] = (byte)(crc % 256); Global.commHelper.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.logOpen == 1) { Log.WriteLog("CommPort", "INFO", "【SendControlCmd】 发送数据:" + SystemUnit.ToHexString(buf)); } return(true); } else { return(false); } }
private void sendNewOrders(int thickness) { FlushProcData(); Global.runMode = RunMode.WRITE; string sql = "insert into working " + "(id, productorder, type, scheduledate, lenth, width, thickness, gross, done, undone," + "completedate, machinecode, worker, worth, remark, status, receivedate, grosswidth)" + "select* from workorder a where a.status = 1 and a.thickness = " + thickness.ToString(); Global.mysqlHelper.ExecuteSql(sql); sql = "update workorder a, working b set a.status = 2 where a.id = b.id"; Global.mysqlHelper.ExecuteSql(sql); StringBuilder sb = new StringBuilder(); sb.Append("update working set status = 2 where id in ("); DataTable dt = Global.mysqlHelper.GetDataTable("select * from working order by workingid asc"); byte[] sendbuf = new byte[400]; int count = dt.Rows.Count; //判断订单数是否大于设备最大订单数30 if (count > 30) { Global.isDataOver = true; } else { Global.isDataOver = false; } //判断是否可以一次发完 Global.isSendAll = true; if (count > Global.dataCount) { Global.isSendAll = false; count = Global.dataCount; } sendbuf[0] = Global.stationAddr; sendbuf[1] = Global.writeCmd; sendbuf[2] = (byte)(Global.offset / 256); sendbuf[3] = (byte)(Global.offset % 256); sendbuf[4] = (byte)(Global.dataCount * 6 / 256); sendbuf[5] = (byte)(Global.dataCount * 6 % 256); sendbuf[6] = (byte)(Global.dataCount * 12); for (int i = 0; i < count; i++) { WorkOrder order = new WorkOrder(dt.Rows[i]); sendbuf[7 + 12 * i] = (byte)((order.Length << 16) >> 24); sendbuf[7 + 12 * i + 1] = (byte)((order.Length << 24) >> 24); sendbuf[7 + 12 * i + 2] = (byte)(order.Length >> 24); sendbuf[7 + 12 * i + 3] = (byte)((order.Length << 8) >> 24); sendbuf[7 + 12 * i + 4] = (byte)((order.GrossWidth << 16) >> 24); sendbuf[7 + 12 * i + 5] = (byte)((order.GrossWidth << 24) >> 24); sendbuf[7 + 12 * i + 6] = (byte)(order.GrossWidth >> 24); sendbuf[7 + 12 * i + 7] = (byte)((order.GrossWidth << 8) >> 24); sendbuf[7 + 12 * i + 8] = (byte)((order.Done << 16) >> 24); sendbuf[7 + 12 * i + 9] = (byte)((order.Done << 24) >> 24); sendbuf[7 + 12 * i + 10] = (byte)(order.Done >> 24); sendbuf[7 + 12 * i + 11] = (byte)((order.Done << 8) >> 24); sb.Append(order.id.ToString() + ","); Global.procData[i].id = order.id; Global.procData[i].Lenth = order.Length; Global.procData[i].Thickness = order.Thickness; Global.procData[i].GrossWidth = order.GrossWidth; Global.procData[i].status = (int)OrderStatus.WORKING; } uint crc = SystemUnit.getCRC(sendbuf, 0, Global.dataCount * 12 + 7); sendbuf[Global.dataCount * 12 + 7] = (byte)(crc / 256); sendbuf[Global.dataCount * 12 + 8] = (byte)(crc % 256); Global.commHelper.SendControlCmd(sendbuf, Global.dataCount * 12 + 9); timerBreak.Enabled = true; sb.Append("0)"); Global.mysqlHelper.ExecuteSql(sb.ToString()); lblWorkingShowInfo.Text = "订单发送中……"; lblWorkingShowInfo.ForeColor = Color.Green; }
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.READDATA) { //if (buff[2] == Global.dataCount * 12 && !Global.readDataDone ) if (buff[2] == Global.dataCount * 12) { for (int i = 0; i < Global.dataCount; i++) { int lenth = (buff[i * 12 + 3] << 8 | buff[12 * i + 4] | buff[12 * i + 5] << 24 | buff[12 * i + 6] << 16); int grosswidth = (buff[12 * i + 7] << 8 | buff[12 * i + 8] | buff[12 * i + 9] << 24 | buff[12 * i + 10] << 16); int done = (buff[12 * i + 11] << 8 | buff[12 * i + 12] | buff[12 * i + 13] << 24 | buff[12 * i + 14] << 16); if (lenth == Global.procData[i].Lenth && grosswidth == Global.procData[i].GrossWidth && done != Global.procData[i].Done) { int undo = Global.procData[i].GrossWidth - done; string sql = "update working set done = " + done.ToString() + " ,undone = " + undo.ToString() + " where id = " + Global.procData[i].id; Global.mysqlHelper.ExecuteSql(sql); Global.procData[i].Done = done; Global.dataIsChanged = true; Log.WriteLog("CommPort", "Info", sql); } } if (Global.readDataDone) { Log.WriteLog("CommPort", "Info", "PostMessage:READDONE"); SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READBACK, (int)ReturnResult.SUCCESS, (int)DataType.READDONE); } else { Log.WriteLog("CommPort", "Info", "PostMessage:READCONTINUE"); SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READBACK, (int)ReturnResult.SUCCESS, (int)DataType.READCONTINUE); } } else if (buff[2] == (30 - Global.dataCount) * 12 && Global.readDataDone) { int count = 30 - Global.dataCount; for (int i = 0; i < count; i++) { int lenth = (buff[i * 12 + 3] << 8 | buff[12 * i + 4] | buff[12 * i + 5] << 24 | buff[12 * i + 6] << 16); int grosswidth = (buff[12 * i + 7] << 8 | buff[12 * i + 8] | buff[12 * i + 9] << 24 | buff[12 * i + 10] << 16); int done = (buff[12 * i + 11] << 8 | buff[12 * i + 12] | buff[12 * i + 13] << 24 | buff[12 * i + 14] << 16); if (lenth == Global.procData[Global.dataCount + i].Lenth && grosswidth == Global.procData[Global.dataCount + i].GrossWidth && done != Global.procData[Global.dataCount + i].Done) { string sql = "update workorder set done = " + done.ToString() + " where id = " + Global.procData[Global.dataCount + i].id; Global.mysqlHelper.ExecuteSql(sql); Global.procData[Global.dataCount + i].Done = done; Global.dataIsChanged = true; Log.WriteLog("CommPort", "Info", sql); } } SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READBACK, (int)ReturnResult.SUCCESS, (int)DataType.READDONE); } // 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); // } } else if (buff[1] == 0x03 && Global.runMode == RunMode.READSTATUS && buff[2] == 4) { //buff[4] 第8位运行状态 buff[6]第3位故障状态 第7位 运行模式 int status = (buff[3] << 8 | buff[6]); SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_READBACK, status, (int)DataType.STATUS); } else if (Global.runMode == RunMode.WRITE && buff[1] == 0x10) { int offset = buff[2] << 8 | buff[3]; if (offset == Global.pinOffset) { SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_WRITEBACK, (int)ReturnResult.SUCCESS, (int)DataType.PIN); } else if (offset == Global.offset) { if (Global.isSendAll) { SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_WRITEBACK, (int)ReturnResult.SUCCESS, (int)DataType.WRITEDONE); } else { SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_WRITEBACK, (int)ReturnResult.SUCCESS, (int)DataType.WRITECONTINUE); } } else if (offset == Global.offset + Global.dataCount * 6) { SystemUnit.PostMessage(SystemUnit.HWND_BROADCAST, SystemUnit.WM_WRITEBACK, (int)ReturnResult.SUCCESS, (int)DataType.WRITEDONE); } } }