/// <summary> /// split the messages /// </summary> /// <param name="messages"></param> /// <returns></returns> private ArrayList getMessages(byte[] messages) { ArrayList mesList = new ArrayList(); bool beginMessage = false; string sMessage = string.Empty; for (int i = 0; i < messages.Length - 2; i++) { if (beginMessage) { sMessage = sMessage + CommunicationTools.byteToString(messages[i]); } if (messages[i] == _cc_STX) { beginMessage = true; sMessage = string.Empty; sMessage = sMessage + CommunicationTools.byteToString(messages[i]) + CommunicationTools.byteToString(messages[i + 1]); i++; } if (messages[i] == _cc_ETX && beginMessage) { beginMessage = false; sMessage = sMessage + CommunicationTools.byteToString(messages[i + 1]) + CommunicationTools.byteToString(messages[i + 2]); mesList.Add(CommunicationTools.stringToByteArray(sMessage)); sMessage = string.Empty; i += 2; } } return(mesList); }
/// <summary> /// decompress a dataarray - 0x00 and 0xff values are compress after crc-calculation /// -before computing data, the dataarray musst decompress /// 0x10 in data comes twice it is one /// </summary> /// <param name="data">dataarray wich will be decompress</param> /// <returns>decompress data array</returns> private byte[] decompressDataStream(byte[] data) { if (data.Length > 6) { int length = data.Length; string sDecomp = ""; int posLastDLE = int.MinValue; //DLE ETX sDecomp = sDecomp + CommunicationTools.byteToString(data[0]); sDecomp = sDecomp + CommunicationTools.byteToString(data[1]); //Header and Data decompress for (int i = 2; i < length - 4; i++) { if (data[i] == 0x10) { if ((posLastDLE + 1) == i) { i++; } else { posLastDLE = i; } } if ((data[i] == 0x00 || data[i] == 0xff) && (i + 1 != length)) { int count = (int)data[i + 1]; for (int j = 0; j < count; j++) { sDecomp = sDecomp + CommunicationTools.byteToString(data[i]); } i++; } else { sDecomp = sDecomp + CommunicationTools.byteToString(data[i]); } } //DLE STX and CRC for (int i = length - 4; i < length; i++) { sDecomp = sDecomp + CommunicationTools.byteToString(data[i]); } return(CommunicationTools.stringToByteArray(sDecomp)); } else { return(data); } }
/// <summary> /// sending a string over the serial port /// </summary> /// <param name="text">sending text</param> /// <param name="waitForAnswer">is set false the program will not wait for an answer</param> public void sendText(string text, bool waitForAnswer) { if (!_sPort.IsOpen) { openSerialPort(); } if (waitForAnswer) { startReadThread(); } #if DEBUG writeSerialLog(CommunicationTools.stringToByteArray(text), "SEND TO DEVICE:"); #endif _sPort.WriteLine(text); }
private void writeSerialLog(byte[] values, string title) { StreamWriter sw = new StreamWriter(this.logFilename, true, System.Text.Encoding.Default); try { sw.WriteLine(title); DateTime now = DateTime.Now; sw.WriteLine(now.ToLongDateString() + " " + now.ToLongTimeString() + "," + now.Millisecond.ToString("000")); sw.WriteLine(CommunicationTools.byteArrayToString(values)); sw.WriteLine("HEX:"); sw.WriteLine(CommunicationTools.ByteArrayToHexString(values)); sw.WriteLine("LENGTH: " + values.Length.ToString()); sw.WriteLine(); } catch { } finally { sw.Close(); } }
/// <summary> /// compress a data array - 0x00 and 0xff values will be compress /// 0x10 must send in twice in the data set /// </summary> /// <param name="data">uncompress data-array</param> /// <returns>compress data-array</returns> private byte[] compressDataStream(byte[] data) { int length = data.Length; string sComp = ""; byte compZeroCounter = 0; byte compFFCounter = 0; for (int i = 0; i < length; i++) { if (data[i] == 0x10) { sComp = sComp + CommunicationTools.byteToString(0x10) + CommunicationTools.byteToString(0x10); } if (data[i] == 0x00 && compFFCounter == 0) { compZeroCounter++; } else if (data[i] == 0xff && compZeroCounter == 0) { compFFCounter++; } else { if (compFFCounter != 0) { sComp = sComp + CommunicationTools.byteToString(0xff) + CommunicationTools.byteToString(compFFCounter); compFFCounter = 0; if (data[i] == 0x00) { compZeroCounter++; } } else if (compZeroCounter != 0) { sComp = sComp + CommunicationTools.byteToString(0x00) + CommunicationTools.byteToString(compZeroCounter); compZeroCounter = 0; if (data[i] == 0xff) { compFFCounter++; } } if (compZeroCounter == 0 && compFFCounter == 0) { if (data[i] == 0x10) { sComp = sComp + CommunicationTools.byteToString(data[i]) + CommunicationTools.byteToString(data[i]); } else { sComp = sComp + CommunicationTools.byteToString(data[i]); } } else if (i == length - 1) { if (compFFCounter != 0) { sComp = sComp + CommunicationTools.byteToString(0xff) + CommunicationTools.byteToString(compFFCounter); } else if (compZeroCounter != 0) { sComp = sComp + CommunicationTools.byteToString(0x00) + CommunicationTools.byteToString(compZeroCounter); } } } } return(CommunicationTools.stringToByteArray(sComp)); //return CommunicationTools.unicodeStringToASCIIByteArray(sComp, codePage); }
/// <summary> /// send a string over serial port /// </summary> /// <param name="command">command as a string</param> /// <param name="waitForAnswer">is set false the program will not wait for an answer</param> private void sendOverSerial(string command, bool waitForAnswer) { byte[] bCommand = CommunicationTools.stringToByteArray(command); byte[] bSendCommand = createSendData(bCommand); _serial.sendData(bSendCommand, waitForAnswer); }
/// <summary> /// trigger by incoming data /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void _serial_DataReceived(object sender, ReceivedDataEventArgs e) { if (e.ReceivedData.Length > 1) { byte[] answer = e.ReceivedData; string sAnswer = string.Empty; byte LRC = 0x00; byte checkLRC = 0x00; bool beginMessage = false; bool endMessage = false; for (int i = 0; i < answer.Length; i++) { if (answer[i] == _cc_STX) { LRC = 0x00; beginMessage = true; endMessage = false; LRC += answer[i]; } else if (beginMessage == true) { if (answer[i] == _cc_ETX) { beginMessage = false; endMessage = true; } else { sAnswer += CommunicationTools.byteToString(answer[i]); } LRC += answer[i]; } else if (endMessage) { checkLRC = answer[i]; break; } } LRC = (byte)((0xff - LRC) + 0x01); if (LRC != checkLRC) { if (OnCRCError != null) { //OnCRCError(this, new EventArgs()); ThreadSafe.Invoke(this.OnCRCError, new object[] { this, new QCommunicationCRCEventArgs(sAnswer, answer, checkLRC, LRC) }); } } if (OnIncomingData != null) // { //OnIncomingData(this, new QCommunicationEventArgs(answer, decompressData)); ThreadSafe.Invoke(this.OnIncomingData, new object[] { this, new QCommunicationEventArgs(sAnswer, answer) }); } } else { if (e.ReceivedData[0] == _cc_ACK) { if (OnACKMessage != null) { ThreadSafe.Invoke(this.OnACKMessage, new object[] { this, EventArgs.Empty }); } } else if (e.ReceivedData[0] == _cc_NAK) { if (OnNAKMessage != null) { ThreadSafe.Invoke(this.OnNAKMessage, new object[] { this, EventArgs.Empty }); } } else if (e.ReceivedData[0] == _cc_SYN) { if (OnSYNMessage != null) { ThreadSafe.Invoke(this.OnSYNMessage, new object[] { this, EventArgs.Empty }); } } } }