private static ITransport CreateRS232Transport(string host, int e, int timeout)
        {
            string key = "RS232://{0}:{1}".F(host, e);
            var    sp  = new SerialTransport
            {
                PortName = host,
                BaudRate = e
            };

            try
            {
                sp.Open();
                sp.Serial.ReadTimeout = timeout;
                sp.Disconnected      += (s, de) =>
                {
                    if (TransportsCache.ContainsKey(key))
                    {
                        lock (TransportsCache)
                        {
                            if (TransportsCache.ContainsKey(key))
                            {
                                TransportsCache.Remove(key);
                            }
                        }
                    }
                };
            }
            catch (Exception ex)
            {
                XTrace.WriteException(ex);
            }
            return(sp);
        }
 public void SerialCodec_RunAsync_ReceiveNotNull()
 {
     using (var serial = new SerialTransport("COM4", 9600))
     {
         serial.Open();
         var data = serial.IncomingMessages.Receive();
         Assert.That(data, Is.Not.Null);
     }
 }
 public void SerialCodec_Send()
 {
     using (var serial = new SerialTransport("COM4", 9600))
     {
         serial.Open();
         var b = serial.OutgoingMessages.Post(Encoding.Default.GetBytes("Привет! Hello!"));
         Assert.That(b);
     }
 }
        public void BuildStack()
        {
            _serial         = new SerialTransport("COM7", 115200);
            _nibusDataCodec = new NibusDataCodec();
            _nmsCodec       = new NmsCodec();
            _releaser       = new List <IDisposable> {
                _nmsCodec, _nibusDataCodec, _serial
            };

            _nmsCodec.ConnectTo(_nibusDataCodec);
            _nibusDataCodec.ConnectTo(_serial);
            _serial.Open();
        }
Exemple #5
0
        public Boolean Connect(string deviceName, Reader r)
        {
            bool isConnected = false;

            try
            {
                Console.WriteLine("Waiting for streaming...");
                baseTime = DateTime.MinValue;
                while (!isConnected)
                {
                    string portNames = Regex.Replace(deviceName, @"[^0-9_\\]", "").ToUpperInvariant();

                    _serialPort.PortName     = "COM" + portNames;
                    _serialPort.WriteTimeout = 1000;
                    _serialPort.ReadTimeout  = 1000;
                    int[] bps = { 115200, 9600, 921600, 19200, 38400, 57600, 230400, 460800 };
                    foreach (int baudRate in bps)
                    {
                        try
                        {
                            _serialPort.BaudRate = baudRate;
                            _serialPort.Open();
                            byte[]       response     = new byte[256];
                            SerialReader serialReader = r as SerialReader;
                            serialReader.receiveResponse(_serialPort, "");
                        }
                        catch (Exception ex)
                        {
                            //_serialPort.Close();
                            continue;
                        }
                        isConnected = true;
                        break;
                    }
                }
                Console.WriteLine("Connection to the module is successful");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(isConnected);
        }