private static void serialPortDataReceived(byte[] data) { // nhận data từ bàn phím xử lý gọi lên socket resByte = CheckByteSend.getByteSend(data); if (resByte != null) { string idCounter = ""; if (dicCounterKeyboard.ContainsKey(resByte.AddressKey)) { idCounter = dicCounterKeyboard[resByte.AddressKey].CounterID; string action = ""; switch (resByte.Command) { case (int)CheckByteSend.BYTE_COMMAND.NEXT_COMMAND: action = ActionTicket.ACTION_CALL; break; case (int)CheckByteSend.BYTE_COMMAND.RECALL_COMMAND: action = ActionTicket.ACTION_RECALL; break; case (int)CheckByteSend.BYTE_COMMAND.DELETE_COMMAND: action = ActionTicket.ACTION_CANCEL; break; case (int)CheckByteSend.BYTE_COMMAND.FINISH_COMMAND: action = ActionTicket.ACTION_FINISH; break; case (int)CheckByteSend.BYTE_COMMAND.FORWARD_COMMAND_SERVICE: action = action = ActionTicket.ACTION_MOVE; break; case (int)CheckByteSend.BYTE_COMMAND.FORWARD_COMMAND_COUNTER: action = action = ActionTicket.ACTION_MOVE; break; case (int)CheckByteSend.BYTE_COMMAND.CALLSTORE_COMMAND: action = ActionTicket.ACTION_MOVE; break; case (int)CheckByteSend.BYTE_COMMAND.RESTORE_COMMAND: action = ActionTicket.ACTION_ALL_RESTORE; break; default: //ERRor break; } if (!"".Equals(action) && !"".Equals(idCounter)) { socket.SendFromAction(action, idCounter); } else { //serialPort.SendData();//error } } else { Console.WriteLine("Khong ton tai dia chi nay!"); } } }
public static ResponeByte getByteSend(byte[] resByte) { ResponeByte resByteClass = null; var count = resByte.Length; if (count > 10) { resByteClass = new ResponeByte(); var dataLength = resByte[5] + resByte[6] * 256; var end = resByte[count - 2] + resByte[count - 1] * 256; int startNum = (int)BYTE_COMMAND.START_COMMAND; int endNum = (int)BYTE_COMMAND.END_COMMAND; if (startNum == resByte[0] && (count - 10) - 1 == dataLength && endNum == end) { resByteClass.DeviceId = (int)resByte[1]; resByteClass.AddressKey = (int)resByte[2]; resByteClass.Command = (int)(resByte[3] + resByte[4] * 256); } } return(resByteClass); }
private static void ReciveDataSocket(EventSocketSendProgram eventData) { int command = 0; switch (eventData.Action) { case ActionTicket.INITIAL: if (dicCounter.Count() == 0 && dicService.Count() == 0) { InitKeyBoardSend(eventData.DicCounter, eventData.DicService); } return; case ActionTicket.ACTION_CREATE: // không làm gì break; case ActionTicket.ACTION_CALL: command = (int)CheckByteSend.BYTE_COMMAND.NEXT_COMMAND; break; case ActionTicket.ACTION_RECALL: command = (int)CheckByteSend.BYTE_COMMAND.RECALL_COMMAND; break; case ActionTicket.ACTION_RESTORE: command = (int)CheckByteSend.BYTE_COMMAND.RESTORE_COMMAND; break; case ActionTicket.ACTION_MOVE: command = (int)CheckByteSend.BYTE_COMMAND.FORWARD_COMMAND_COUNTER; command = (int)CheckByteSend.BYTE_COMMAND.FORWARD_COMMAND_SERVICE; break; case ActionTicket.ACTION_CANCEL: command = (int)CheckByteSend.BYTE_COMMAND.DELETE_COMMAND; break; case ActionTicket.ACTION_FINISH: command = (int)CheckByteSend.BYTE_COMMAND.FINISH_COMMAND; break; default: //bug break; } if (eventData.ObjSend != null) { string counterID = eventData.ObjSend.counter_id; int indexService = getIndexService(eventData.ObjSend.service_id, counterID); if (indexService != -1) { string data = eventData.ObjSend.cnum; var address = dicCounterKeyboard.FirstOrDefault(d => d.Value.CounterID == counterID).Key; if (address > 0) { var byteRes = modBus.BuildText(ActionTicket.DEVICE_ID, address, command, data, indexService); serialPort.SendData(byteRes); } else { //bug } } else { //truyền lỗi xuống port } } resByte = null; }
public static List <ResponeByte> getByteSendList(byte[] resByte, Dictionary <int, KeyBoardCounter> dicCounterKeyboard) { List <ResponeByte> lstRes = null; ResponeByte resByteClass = null; var count = resByte.Length; var countMath = 0; if (count > 10) { lstRes = new List <ResponeByte>(); resByteClass = new ResponeByte(); int startNum = (int)BYTE_COMMAND.START_COMMAND; int endNum = (int)BYTE_COMMAND.END_COMMAND; do { ConvertByte <byte> arrayItemDevice = new ConvertByte <byte>(resByte, countMath, 15); if (CheckFrame(arrayItemDevice, startNum, dicCounterKeyboard)) { try { ConvertByte <byte> arrayItemSendSocket = new ConvertByte <byte>(resByte, countMath, 17); resByteClass = new ResponeByte(); var dataLength = arrayItemDevice[5] + arrayItemSendSocket[6] * 256; var end = 0; var lengArr = dataLength + 11; if (lengArr == 15) { countMath += 15; } else if (lengArr <= count) { arrayItemSendSocket = new ConvertByte <byte>(resByte, countMath, lengArr); byte[] byteArr = arrayItemSendSocket.Cast <byte>().ToArray(); var data = new ConvertByte <byte>(byteArr, 11, dataLength - 4); resByteClass.Data = data.Cast <byte>().ToArray(); var lengData = data.Length; byte[] dataByte = null; if (lengData % 4 == 0) { dataByte = new byte[lengData / 4]; var j = 0; for (int i = 1; i < lengData; i = j * 4) { if (i == 1) { dataByte[j] = data[i - 1]; } else { dataByte[j] = data[i]; } j++; } resByteClass.Cnum = Encoding.UTF8.GetString(dataByte, 0, dataByte.Length); } end = arrayItemSendSocket[lengArr - 2] + arrayItemSendSocket[lengArr - 1] * 256; if (endNum == end) { resByteClass.DeviceId = (int)arrayItemSendSocket[1]; resByteClass.AddressKey = (int)arrayItemSendSocket[2]; resByteClass.Command = (int)(arrayItemSendSocket[3] + arrayItemSendSocket[4] * 256); resByteClass.NumServicesOrCounter = (int)(arrayItemSendSocket[11] + arrayItemSendSocket[12]); lstRes.Add(resByteClass); } countMath += lengArr; } else { countMath = ForSearchStart(countMath, resByte, count); } } catch { countMath = ForSearchStart(countMath, resByte, count); } } else { countMath = ForSearchStart(countMath, resByte, count); } } while (countMath < count); } return(lstRes); }