Exemplo n.º 1
0
 // Read the data from the serial port using ReadLine method
 private void PortRead(ref Attributes.ReadData rd)
 {
     try
     {
         // Read the serial port data
         string message = _serialPort.ReadLine();
         rd.read = message;
     }
     catch (TimeoutException) { rd.read = null; }
     catch (ThreadAbortException) { _continue = false; }
     catch (IOException) { _continue = false; }
     catch (Exception) { _continue = false; }
 }
Exemplo n.º 2
0
        // The method reads and writes data on the serial port
        private void PortBegin(ref List <Attributes.PortData> portData)
        {
            byte i   = 0;
            int  len = portData.Count;

            Attributes.ReadData readData =
                new Attributes.ReadData {
                id = portData[i].id, read = null
            };

            while (_continue && _run)
            {
                // Send the data to the serial port
                if (!String.IsNullOrEmpty(portData[i].send))
                {
                    PortSend(portData[i].send);
                }

                PortRead(ref readData);

                // The static method writes the serial port data to SQL database
                if (!String.IsNullOrEmpty(readData.read))
                {
                    SqlData.Write(readData.id, readData.read);
                }

                if (i < len - 1)
                {
                    i++;
                }
                else
                {
                    i = 0;
                }

                readData.id = portData[i].id;
            }
            _readThread.Join(500);
            _serialPort.Close();
            Service.InitConfig();
        }