private byte[] CommonTestSendCommand(string testCommandString, string testFunCode) { //eg:FF FF 00 0D F2 AA 00 01 00 02 00 03 00 06 01 02 01 0A if (testCommandString.Length < 1) { return(new byte[0]); } byte[] conductionByte = new byte[2 + testCommandString.Length / 2]; var conductArray = (testFunCode + " " + SplitStringByEmpty(testCommandString)).Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); return(ConvertByte.HexStringToByte(conductionByte, conductArray, 0)); }
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); }
/* Check đúng khung truyền*/ public static bool CheckFrame(ConvertByte <byte> byteRes, int startNum, Dictionary <int, KeyBoardCounter> dicCounterKeyboard) { bool isCheck = false; try { var deviceID = byteRes[1]; var command = ((int)byteRes[3] + (int)byteRes[4] * 256); var address = byteRes[2]; switch (deviceID) { case (int)DEVICE_ID.DEVICE_KEYBOARD: if ((startNum == byteRes[0] && dicCounterKeyboard.ContainsKey(address)) && (command == (int)BYTE_COMMAND.CALLSTORE_COMMAND || command == (int)BYTE_COMMAND.DELETE_COMMAND || command == (int)BYTE_COMMAND.DISPLAY_COMMAND || command == (int)BYTE_COMMAND.END_COMMAND || command == (int)BYTE_COMMAND.ERROR_COMMAND || command == (int)BYTE_COMMAND.FINISH_COMMAND || command == (int)BYTE_COMMAND.FORWARD_COMMAND_COUNTER || command == (int)BYTE_COMMAND.FORWARD_COMMAND_SERVICE || command == (int)BYTE_COMMAND.NEXT_COMMAND || command == (int)BYTE_COMMAND.RECALL_COMMAND || command == (int)BYTE_COMMAND.RESTORE_COMMAND || command == (int)BYTE_COMMAND.START_COMMAND || command == (int)BYTE_COMMAND.START_OF_COUNTER_COMMAND || command == (int)BYTE_COMMAND.OK_COMMAND || command == (int)BYTE_COMMAND.RESET_COUNTER || command == (int)BYTE_COMMAND.CALL_LIST_WATTING || command == (int)BYTE_COMMAND.CALL_PRIORITY_COMMAND || command == (int)BYTE_COMMAND.STATUS_COMMAND || command == (int)BYTE_COMMAND.CONNECT_COMMAND)) { string msg = ""; switch (command) { case (int)BYTE_COMMAND.CALLSTORE_COMMAND: msg = "Call restore"; break; case (int)BYTE_COMMAND.DELETE_COMMAND: msg = "DELETE"; break; case (int)BYTE_COMMAND.DISPLAY_COMMAND: msg = "DISPLAY_COMMAND"; break; case (int)BYTE_COMMAND.END_COMMAND: msg = "END_COMMAND"; break; case (int)BYTE_COMMAND.ERROR_COMMAND: msg = "ERROR_COMMAND"; break; case (int)BYTE_COMMAND.FINISH_COMMAND: msg = "FINISH_COMMAND"; break; case (int)BYTE_COMMAND.FORWARD_COMMAND_COUNTER: msg = "FORWARD_COMMAND_COUNTER"; break; case (int)BYTE_COMMAND.FORWARD_COMMAND_SERVICE: msg = "FORWARD_COMMAND_SERVICE"; break; case (int)BYTE_COMMAND.NEXT_COMMAND: msg = "NEXT_COMMAND"; break; case (int)BYTE_COMMAND.RECALL_COMMAND: msg = "RECALL_COMMAND"; break; case (int)BYTE_COMMAND.RESTORE_COMMAND: msg = "RESTORE_COMMAND"; break; case (int)BYTE_COMMAND.START_COMMAND: msg = "START_COMMAND"; break; case (int)BYTE_COMMAND.OK_COMMAND: msg = "OK_COMMAND"; break; case (int)BYTE_COMMAND.CALL_LIST_WATTING: msg = "CALL_LIST_WATTING"; break; case (int)BYTE_COMMAND.CALL_PRIORITY_COMMAND: msg = "CALL_PRIORITY_COMMAND"; break; case (int)BYTE_COMMAND.STATUS_COMMAND: msg = "STATUS_COMMAND"; break; case (int)BYTE_COMMAND.CONNECT_COMMAND: msg = "CONNECT_COMMAND"; break; } Console.WriteLine(msg); isCheck = true; } else { Console.WriteLine("Sai khung truyen: "); foreach (var a in byteRes) { Console.WriteLine(a + ", "); } } break; case (int)DEVICE_ID.DEVICE_FEED_BACK: if (startNum == byteRes[0] && dicCounterKeyboard.Values.FirstOrDefault(m => m.AddressFeedBack == address) != null) { isCheck = true; } break; } } catch { return(isCheck); } return(isCheck); }