コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
            }
        }
コード例 #3
0
        /// <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);
        }
コード例 #4
0
        /// <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);
        }
コード例 #5
0
 /// <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);
 }