Пример #1
0
        private void appendReceivedDataToString(ref string incomingData)
        {
            byte[] tempBuffer;
            string bufferAsString;
            int    bytesToRead;

            bytesToRead = m_serialPort.BytesToRead;
            if (bytesToRead > 0)
            {
                tempBuffer = new byte[bytesToRead];
                m_serialPort.Read(tempBuffer, 0, bytesToRead);
                bufferAsString = ASCIIEncoding.ASCII.GetString(tempBuffer);
                incomingData  += bufferAsString;

                ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
                                                        Utils.HelperComDriverLogger.GetLoggerChannel(this), tempBuffer);
            }
        }
Пример #2
0
        private void appendReceivedDataToArray(ref byte[] incomingData)
        {
            byte[] tempBuffer;
            byte[] copyOfBuffer;
            int    bytesToRead;

            bytesToRead = m_serialPort.BytesToRead;
            if (bytesToRead > 0)
            {
                tempBuffer = new byte[bytesToRead];
                m_serialPort.Read(tempBuffer, 0, bytesToRead);
                copyOfBuffer = incomingData;
                incomingData = new byte[copyOfBuffer.Length + tempBuffer.Length];
                Array.Copy(copyOfBuffer, 0, incomingData, 0, copyOfBuffer.Length);
                Array.Copy(tempBuffer, 0, incomingData, copyOfBuffer.Length, tempBuffer.Length);

                ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
                                                        Utils.HelperComDriverLogger.GetLoggerChannel(this), tempBuffer);
            }
        }
Пример #3
0
        internal void OnDataReceived(byte[] receivedBytes)
        {
            int cbRead = receivedBytes.Length;

            byte[] tmpBuffer = new byte[receivedBuffer.Length + cbRead];
            Array.Copy(receivedBuffer, 0, tmpBuffer, 0, receivedBuffer.Length);
            Array.Copy(receivedBytes, 0, tmpBuffer, receivedBuffer.Length, cbRead);
            receivedBuffer = tmpBuffer;

            ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
                                                    Utils.HelperComDriverLogger.GetLoggerChannel(this), tmpBuffer);

            if (inputMode == InputMode.ASCII)
            {
                ClientReceiveString();
            }
            else if (inputMode == InputMode.Binary)
            {
                ClientReceiveBytes();
            }
        }
Пример #4
0
        private void socketReceivedBytes(byte[] incomingBytes, int count)
        {
            bool bStxFound = false;

            byte[] tempBuffer;
            string bufferAsString;
            int    index       = 0;
            int    totalLength = 0;

            byte[] tcpHeader;

            const int HEADER_LENGTH = 24;

            tcpHeader = ethernetHeader;

            try
            {
                int cbRead = 0;
                cbRead = count;
                if (cbRead > 0)
                {
                    byte[] temp = new byte[cbRead];
                    Array.Copy(incomingBytes, 0, temp, 0, cbRead);

                    ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
                                                            Utils.HelperComDriverLogger.GetLoggerChannel(this), temp);

                    byte[] tmpBuffer = new byte[incomingData.Length + cbRead];
                    Array.Copy(incomingData, 0, tmpBuffer, 0, incomingData.Length);
                    Array.Copy(temp, 0, tmpBuffer, incomingData.Length, cbRead);
                    incomingData = tmpBuffer;
                }
                else
                {
                    // Socket was closed?
                    m_PLCSocket.Close();
                }
            }
            catch
            {
                // Socket Error?
                m_PLCSocket.Close();
            }


            if (incomingData.Length >= Utils.Lengths.LENGTH_TCP_HEADER)
            {
                if (incomingData.Take(4).SequenceEqual(tcpHeader) == true)
                {
                    tempBuffer = new byte[incomingData.Length - 6];
                    Array.Copy(incomingData, 6, tempBuffer, 0, incomingData.Length - 6);
                    incomingData = tempBuffer;
                }
                else
                {
                    //throw new ComDriveExceptions("Ethernet Transaction ID mismatch", ComDriveExceptions.ComDriveException.TransactionIdMismatch);
                }
            }

            if (incomingData.Length > 0)
            {
                bufferAsString = ASCIIEncoding.ASCII.GetString(incomingData);
                index          = bufferAsString.IndexOf(STX_STRING);
                if (index >= 0)
                {
                    tempBuffer = new byte[incomingData.Length - index];
                    Array.Copy(incomingData, index, tempBuffer, 0, incomingData.Length - index);
                    incomingData = tempBuffer;
                    bStxFound    = true;
                }
                else
                {
                    if (incomingData.Length > 100)
                    {
                        throw new ComDriveExceptions("STX is missing",
                                                     ComDriveExceptions.ComDriveException.CommunicationTimeout);
                    }
                }
            }

            if (!bStxFound)
            {
                return;
            }

            if (incomingData.Length < HEADER_LENGTH)
            {
                return;
            }

            totalLength = BitConverter.ToUInt16(incomingData, 20) +
                          HEADER_LENGTH + 3; // 3 for data checksum + ETX

            if (incomingData.Length < totalLength)
            {
                return;
            }

            tempBuffer = new byte[totalLength];
            Array.Copy(incomingData, 0, tempBuffer, 0, totalLength);
            incomingData = tempBuffer;

            tempBuffer = null;

            if (incomingData[totalLength - 1] != 92) // 92 is '\' which is the ETX
            {
                throw new ComDriveExceptions("ETX is missing", ComDriveExceptions.ComDriveException.ETXMissing);
            }

            messageReceived = true;
        }
Пример #5
0
        private void socketReceivedString(byte[] incomingBytes, int count)
        {
            bool   bStxFound      = false;
            bool   bEtxFound      = false;
            string incomingString = "";

            byte[] tempBuffer;
            byte[] tcpHeader;
            int    index    = 0;
            int    checksum = 0;

            try
            {
                int cbRead = 0;
                cbRead = count;
                if (cbRead > 0)
                {
                    byte[] temp = new byte[cbRead];
                    Array.Copy(incomingBytes, 0, temp, 0, cbRead);

                    ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
                                                            Utils.HelperComDriverLogger.GetLoggerChannel(this), temp);

                    byte[] tmpBuffer = new byte[incomingData.Length + cbRead];
                    Array.Copy(incomingData, 0, tmpBuffer, 0, incomingData.Length);
                    Array.Copy(temp, 0, tmpBuffer, incomingData.Length, cbRead);
                    incomingData = tmpBuffer;
                }
                else
                {
                    // Socket was closed?
                    m_PLCSocket.Close();
                }
            }
            catch
            {
                // Socket Error?
                m_PLCSocket.Close();
            }

            tcpHeader      = ethernetHeader;
            incomingString = ASCIIEncoding.ASCII.GetString(incomingData);
            if (!_suppressEthernetHeader && incomingString.Length >= Utils.Lengths.LENGTH_TCP_HEADER)
            {
                if (incomingData.Take(4).SequenceEqual(tcpHeader) == true)
                {
                    tempBuffer = new byte[incomingString.Length - 6];
                    Array.Copy(incomingData, 6, tempBuffer, 0, incomingData.Length - 6);
                    incomingData   = tempBuffer;
                    incomingString = ASCIIEncoding.ASCII.GetString(incomingData);
                }
                else
                {
                    //throw new ComDriveExceptions("Ethernet Transaction ID mismatch", ComDriveExceptions.ComDriveException.TransactionIdMismatch);
                }
            }

            if (incomingString.Length > 0)
            {
                index = incomingString.IndexOf("/"); // find the STX
                if (index >= 0)
                {
                    incomingString = incomingString.Substring(index, incomingString.Length - index);
                    bStxFound      = true;
                }
                else
                {
                    if (incomingString.Length > 100)
                    {
                        throw new ComDriveExceptions("STX is missing",
                                                     ComDriveExceptions.ComDriveException.CommunicationTimeout);
                    }
                }

                index = incomingString.IndexOf("\r"); // find the ETX
                if (index >= 0)
                {
                    incomingString = incomingString.Substring(0, index + 1);
                    bEtxFound      = true;
                }
            }

            if (!bStxFound || !bEtxFound)
            {
                return;
            }

            for (int i = 2; i < incomingString.Length - 3; i++)
            {
                checksum += incomingData[i];
            }

            string CRC = Utils.DecimalToHex(checksum % 256);

            if (CRC != incomingString.Substring(incomingString.Length - 3, 2))
            {
                throw new ComDriveExceptions("Wrong Data Checksum", ComDriveExceptions.ComDriveException.ChecksumError);
            }

            messageReceived = true;
        }
Пример #6
0
        private void serialPortReceiveBytes(byte[] incomingBytes, int count)
        {
            bool bStxFound = false;

            byte[] tempBuffer;
            string bufferAsString;
            int    index = 0;
            UInt16 pcCheckSum;
            int    totalLength = 0;

            const int HEADER_LENGTH = 24;

            if (count > 0)
            {
                byte[] temp = new byte[count];
                Array.Copy(incomingBytes, 0, temp, 0, count);

                ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
                                                        Utils.HelperComDriverLogger.GetLoggerChannel(this), temp);

                byte[] tmpBuffer = new byte[resultBytes.Length + count];
                Array.Copy(resultBytes, 0, tmpBuffer, 0, resultBytes.Length);
                Array.Copy(temp, 0, tmpBuffer, resultBytes.Length, count);
                resultBytes = tmpBuffer;
            }

            if (resultBytes.Length > 0)
            {
                bufferAsString = ASCIIEncoding.ASCII.GetString(resultBytes);
                index          = bufferAsString.IndexOf(STX_STRING);
                if (index >= 0)
                {
                    tempBuffer = new byte[resultBytes.Length - index];
                    Array.Copy(resultBytes, index, tempBuffer, 0, resultBytes.Length - index);
                    resultBytes = tempBuffer;
                    bStxFound   = true;
                }
                else
                {
                    if (resultBytes.Length > 100)
                    {
                        throw new ComDriveExceptions("STX is missing",
                                                     ComDriveExceptions.ComDriveException.CommunicationTimeout);
                    }
                }
            }
            else
            {
                return;
            }

            if (!bStxFound)
            {
                return;
            }

            if (resultBytes.Length < HEADER_LENGTH)
            {
                return;
            }

            pcCheckSum = Utils.calcCheckSum(ref resultBytes, 0, 21);

            if (pcCheckSum != BitConverter.ToUInt16(resultBytes, 22))
            {
                throw new ComDriveExceptions("Wrong Header Checksum",
                                             ComDriveExceptions.ComDriveException.ChecksumError);
            }

            totalLength = BitConverter.ToUInt16(resultBytes, 20) +
                          HEADER_LENGTH + 3; // 3 for data checksum + ETX


            if (resultBytes.Length < totalLength)
            {
                return;
            }

            tempBuffer = new byte[totalLength];
            Array.Copy(resultBytes, 0, tempBuffer, 0, totalLength);
            resultBytes = tempBuffer;

            tempBuffer = null;

            pcCheckSum = Utils.calcCheckSum(ref resultBytes, 24, totalLength - 4);
            if (pcCheckSum != BitConverter.ToUInt16(resultBytes, totalLength - 3))
            {
                throw new ComDriveExceptions("Wrong Data Checksum", ComDriveExceptions.ComDriveException.ChecksumError);
            }

            if (resultBytes[totalLength - 1] != 92) // 92 is '\' which is the ETX
            {
                throw new ComDriveExceptions("ETX is missing", ComDriveExceptions.ComDriveException.ETXMissing);
            }

            messageReceived = true;
        }
Пример #7
0
        private void serialPortReceiveString(byte[] incomingBytes, int count)
        {
            bool   bStxFound = false;
            bool   bEtxFound = false;
            int    index     = 0;
            int    checksum  = 0;
            string resultString;

            if (count > 0)
            {
                byte[] temp = new byte[count];
                Array.Copy(incomingBytes, 0, temp, 0, count);

                ComDriverLogger.LogReceivedMessageChunk(DateTime.Now,
                                                        Utils.HelperComDriverLogger.GetLoggerChannel(this), temp);

                byte[] tmpBuffer = new byte[resultBytes.Length + count];
                Array.Copy(resultBytes, 0, tmpBuffer, 0, resultBytes.Length);
                Array.Copy(temp, 0, tmpBuffer, resultBytes.Length, count);
                resultBytes = tmpBuffer;
            }

            resultString = ASCIIEncoding.ASCII.GetString(resultBytes);

            if (resultString.Length > 0)
            {
                index = resultString.IndexOf("/"); // find the STX
                if (index >= 0)
                {
                    resultString = resultString.Substring(index, resultString.Length - index);
                    byte[] tempBuffer = new byte[resultBytes.Length - index];
                    Array.Copy(resultBytes, index, tempBuffer, 0, resultBytes.Length - index);
                    resultBytes = tempBuffer;
                    bStxFound   = true;
                }
                else
                {
                    if (resultString.Length > 100)
                    {
                        throw new ComDriveExceptions("STX is missing",
                                                     ComDriveExceptions.ComDriveException.CommunicationTimeout);
                    }
                }

                index = resultString.IndexOf("\r"); // find the ETX
                if (index >= 0)
                {
                    resultString = resultString.Substring(0, index + 1);
                    bEtxFound    = true;
                }
            }
            else
            {
                return;
            }

            if (!bStxFound || !bEtxFound)
            {
                return;
            }

            for (int i = 2; i < resultString.Length - 3; i++)
            {
                checksum += resultBytes[i];
            }

            string CRC = Utils.DecimalToHex(checksum % 256);

            if (CRC != resultString.Substring(resultString.Length - 3, 2))
            {
                throw new ComDriveExceptions("Wrong Data Checksum", ComDriveExceptions.ComDriveException.ChecksumError);
            }

            messageReceived = true;
        }