예제 #1
0
        /// <summary>
        ///  Sends Cctalk message to device and waits for answer.
        /// </summary>
        public CctalkMessage Send(CctalkMessage com, ICctalkChecksum chHandler)
        {
            // TODO: handle BUSY message
            lock (_callSyncRoot)
            {
                Trace.TraceInformation("Sending message from {0} to {1}. Header={2}", com.SourceAddr, com.DestAddr, com.Header);

                var msgBytes = com.GetTransferDataNoChecksumm();
                chHandler.CalcAndApply(msgBytes);

                //_respondChecksumChecker = chHandler;

                _port.DiscardInBuffer();

                _port.Write(msgBytes, 0, msgBytes.Length);

                Trace.TraceInformation("Message sent, waiting for respond");

                _port.ReadTimeout = RespondStartTimeout;
                Int32 respondBufPos = 0;
                CctalkMessage respond;

                var echoRemover = 0;
                while (true)
                {
                    try
                    {
                        var b = (Byte)_port.ReadByte();
                        // after first respond package we wait fo rest respond packages with another timout
                        _port.ReadTimeout = RespondDataTimeout;

                        if (_removeEcho && (echoRemover < msgBytes.Length))
                        {
                            echoRemover++;
                            continue;
                        }
                        _respondBuf[respondBufPos] = b;
                        respondBufPos++;

                        var isRespondComplete = GenericCctalkDevice.IsRespondComplete(_respondBuf, respondBufPos);
                        if (isRespondComplete)
                        {
                            if (!chHandler.Check(_respondBuf, 0, respondBufPos))
                            {
                                var copy = new byte[respondBufPos];
                                Array.Copy(_respondBuf, copy, respondBufPos);
                                throw new InvalidRespondFormatException(copy, "Checksumm check fail");
                            }
                            respond = GenericCctalkDevice.ParseRespond(_respondBuf, 0, respondBufPos);
                            Array.Clear(_respondBuf, 0, _respondBuf.Length);
                            break;
                        }

                    }
                    catch (TimeoutException ex)
                    {
                        if (_port.ReadTimeout == RespondStartTimeout)
                            throw new TimeoutException("Device not respondng", ex);

                        var respondBufContents = new StringBuilder();
                        for (int i = 0; i > respondBufPos; i++)
                        {
                            respondBufContents.Append(_respondBuf[i].ToString("X"))
                                              .Append(" ");

                        }
                        Trace.TraceInformation("Pause in reply error. Recive buffer contents {0} bytes: {1}", respondBufPos, respondBufContents);
                        Array.Clear(_respondBuf, 0, _respondBuf.Length);
                        throw new TimeoutException("Pause in reply (should reset all communication vatiables and be ready to recive the next message)", ex);

                    }
                }

                return respond;

                /*
                 * When receiving bytes within a message packet, the communication software should
                 * wait up to 50ms for another byte if it is expected. If a timeout condition occurs, the
                 * software should reset all communication variables and be ready to receive the next
                 * message. No other action should be taken. (cctalk spec part1, 11.1)
                 */

            }
        }
예제 #2
0
        CctalkMessage _doSend(CctalkMessage com, ICctalkChecksum chHandler)
        {
            // TODO: handle BUSY message
            lock (_callSyncRoot)
            {
                var msgBytes = com.GetTransferDataNoChecksumm();
                chHandler.CalcAndApply(msgBytes);

                //_respondChecksumChecker = chHandler;

                //_port.DiscardInBuffer();
                _port.WriteTimeout = WriteTimeout;
                _port.Write(msgBytes, 0, msgBytes.Length);

                Thread.Sleep(100);

                _port.ReadTimeout = RespondStartTimeout;
                Int32         respondBufPos = 0;
                CctalkMessage respond;


                var echoRemover = 0;
                while (true)
                {
                    try
                    {
                        var b = (Byte)_port.ReadByte();
                        if (com.Header == CctalkCommands.TestOutputLines || com.Header == CctalkCommands.TestSolenoids)
                        {
                            _port.ReadTimeout = RespondHeavyCommandsTimeout;
                        }
                        else
                        {
                            _port.ReadTimeout = RespondDataTimeout;
                        }

                        if (_removeEcho && (echoRemover < msgBytes.Length))
                        {
                            echoRemover++;
                            continue;
                        }
                        _respondBuf[respondBufPos] = b;
                        respondBufPos++;

                        var isResponseComplete = GenericCctalkDevice.IsResponseComplete(_respondBuf, respondBufPos);
                        if (isResponseComplete)
                        {
                            if (!chHandler.Check(_respondBuf, 0, respondBufPos))
                            {
                                var copy = new byte[respondBufPos];
                                Array.Copy(_respondBuf, copy, respondBufPos);
                                throw new InvalidRespondFormatException(copy, "Checksumm check fail");
                            }
                            respond = GenericCctalkDevice.ParseResponse(_respondBuf, 0, respondBufPos);
                            Array.Clear(_respondBuf, 0, _respondBuf.Length);
                            break;
                        }
                    }
                    catch (TimeoutException ex)
                    {
                        if (_port.ReadTimeout == RespondStartTimeout)
                        {
                            throw new TimeoutException("Device not respondng", ex);
                        }

                        throw new TimeoutException("Pause in reply (should reset all communication variables and be ready to recive the next message)", ex);
                    }
                }

                Thread.Sleep(100);

                return(respond);


                /*
                 * When receiving bytes within a message packet, the communication software should
                 * wait up to 50ms for another byte if it is expected. If a timeout condition occurs, the
                 * software should reset all communication variables and be ready to receive the next
                 * message. No other action should be taken. (cctalk spec part1, 11.1)
                 */
            }
        }
예제 #3
0
        /// <summary>
        ///  Sends Cctalk message to device and waits for answer.
        /// </summary>
        public CctalkMessage Send(CctalkMessage com, ICctalkChecksum chHandler)
        {
            // TODO: handle BUSY message
            lock (_callSyncRoot)
            {
                Trace.TraceInformation("Sending message from {0} to {1}. Header={2}", com.SourceAddr, com.DestAddr, com.Header);


                var msgBytes = com.GetTransferDataNoChecksumm();
                chHandler.CalcAndApply(msgBytes);

                //_respondChecksumChecker = chHandler;

                _port.DiscardInBuffer();

                _port.Write(msgBytes, 0, msgBytes.Length);

                Trace.TraceInformation("Message sent, waiting for respond");

                _port.ReadTimeout = RespondStartTimeout;
                Int32         respondBufPos = 0;
                CctalkMessage respond;


                var echoRemover = 0;
                while (true)
                {
                    try
                    {
                        var b = (Byte)_port.ReadByte();
                        // after first respond package we wait fo rest respond packages with another timout
                        _port.ReadTimeout = RespondDataTimeout;

                        if (_removeEcho && (echoRemover < msgBytes.Length))
                        {
                            echoRemover++;
                            continue;
                        }
                        _respondBuf[respondBufPos] = b;
                        respondBufPos++;

                        var isRespondComplete = GenericCctalkDevice.IsRespondComplete(_respondBuf, respondBufPos);
                        if (isRespondComplete)
                        {
                            if (!chHandler.Check(_respondBuf, 0, respondBufPos))
                            {
                                var copy = new byte[respondBufPos];
                                Array.Copy(_respondBuf, copy, respondBufPos);
                                throw new InvalidRespondFormatException(copy, "Checksumm check fail");
                            }
                            respond = GenericCctalkDevice.ParseRespond(_respondBuf, 0, respondBufPos);
                            //var temp = new byte[respondBufPos];
                            //Array.Copy(_respondBuf, 0, temp, 0, temp.Length);
                            //DllLog.In(temp.ToStr());
                            Array.Clear(_respondBuf, 0, _respondBuf.Length);
                            break;
                        }
                    }
                    catch (TimeoutException ex)
                    {
                        if (_port.ReadTimeout == RespondStartTimeout)
                        {
                            Log.In("Device not respondng");
                            //throw new TimeoutException("Device not respondng", ex);
                            return(null);
                        }

                        var respondBufContents = new StringBuilder();
                        for (int i = 0; i > respondBufPos; i++)
                        {
                            respondBufContents.Append(_respondBuf[i].ToString("X"))
                            .Append(" ");
                        }
                        Trace.TraceInformation("Pause in reply error. Recive buffer contents {0} bytes: {1}", respondBufPos, respondBufContents);
                        Array.Clear(_respondBuf, 0, _respondBuf.Length);
                        throw new TimeoutException("Pause in reply (should reset all communication vatiables and be ready to recive the next message)", ex);
                    }
                }

                return(respond);


                /*
                 * When receiving bytes within a message packet, the communication software should
                 * wait up to 50ms for another byte if it is expected. If a timeout condition occurs, the
                 * software should reset all communication variables and be ready to receive the next
                 * message. No other action should be taken. (cctalk spec part1, 11.1)
                 */
            }
        }
        /// <summary>
        ///  Sends Cctalk message to device and waits for answer.
        /// </summary>
        public CctalkMessage Send(CctalkMessage com, ICctalkChecksum chHandler)
        {
            // TODO: handle BUSY message
            lock (_callSyncRoot)
            {

                var msgBytes = com.GetTransferDataNoChecksumm();
                chHandler.CalcAndApply(msgBytes);

                //_respondChecksumChecker = chHandler;

                _port.DiscardInBuffer();

                _port.Write(msgBytes, 0, msgBytes.Length);

                _port.ReadTimeout = RespondStartTimeout;
                Int32 respondBufPos = 0;
                CctalkMessage respond;

                var echoRemover = 0;
                while (true)
                {
                    try
                    {
                        var b = (Byte)_port.ReadByte();
                        _port.ReadTimeout = RespondDataTimeout;

                        if (_removeEcho && (echoRemover < msgBytes.Length))
                        {
                            echoRemover++;
                            continue;
                        }
                        _respondBuf[respondBufPos] = b;
                        respondBufPos++;

                        var isRespondComplete = GenericCctalkDevice.IsRespondComplete(_respondBuf, respondBufPos);
                        if (isRespondComplete)
                        {
                            if (!chHandler.Check(_respondBuf, 0, respondBufPos))
                            {
                                var copy = new byte[respondBufPos];
                                Array.Copy(_respondBuf, copy, respondBufPos);
                                throw new InvalidRespondFormatException(copy, "Checksumm check fail");
                            }
                            respond = GenericCctalkDevice.ParseRespond(_respondBuf, 0, respondBufPos);
                            Array.Clear(_respondBuf, 0, _respondBuf.Length);
                            break;
                        }

                    }
                    catch (TimeoutException ex)
                    {
                        if (_port.ReadTimeout == RespondStartTimeout)
                            throw new TimeoutException("Device not respondng", ex);

                        throw new TimeoutException("Pause in reply (should reset all communication vatiables and be ready to recive the next message)", ex);

                    }
                }

                return respond;

                /*
                 * When receiving bytes within a message packet, the communication software should
                 * wait up to 50ms for another byte if it is expected. If a timeout condition occurs, the
                 * software should reset all communication variables and be ready to receive the next
                 * message. No other action should be taken. (cctalk spec part1, 11.1)
                 */

            }
        }