Exemplo n.º 1
0
        protected override async Task <byte[]> SendRequest(byte[] request, int reqLen)
        {
            //double slient_interval = 1000 * 5 * ((double)1 / (double)config.BaudRate);
            byte[] response = null;

            m_semaphore_connection.Wait();

            if (m_serialPort != null && m_serialPort.IsOpen())
            {
                try
                {
                    m_serialPort.DiscardInBuffer();
                    m_serialPort.DiscardOutBuffer();
                    Task.Delay(m_silent).Wait();
                    m_serialPort.Write(request, 0, reqLen);
                    response = ReadResponse();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Something wrong with the connection, disposing...");
                    Console.WriteLine(e.Message);
                    m_serialPort.Close();
                    m_serialPort.Dispose();
                    m_serialPort = null;
                    Console.WriteLine("Connection lost, reconnecting...");
                    await ConnectSlave();
                }
            }
            else
            {
                Console.WriteLine("Connection lost, reconnecting...");
                await ConnectSlave();
            }

            m_semaphore_connection.Release();

            return(response);
        }