D
D
des1roer2015-09-28 13:11:44
C++ / C#
des1roer, 2015-09-28 13:11:44

How to poll the com port?

How can I check if there is an answer? In general, the question on the code - seems to be correct? What is the code for - there is a device connected via a com port. I can’t understand whether I’m clinging to the com port or sending it. If you guessed with the request and the number, then you should receive some kind of response from the port. In this case, no response, no error

using System;
using System.IO.Ports;

namespace Com
{
    class Program
    {
        static void Main(string[] args)
        {
            SerialPort _serialPort = new SerialPort("COM18",
                                      19200,
                                      Parity.None,
                                      8,
                                      StopBits.One);
            _serialPort.Handshake = Handshake.None;

            _serialPort.Open();
            _serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
            _serialPort.Write("#031");

            Console.ReadLine(); //Pause

        }
        private static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            Console.WriteLine("Data Received:");
            Console.Write(indata);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Martyanov, 2015-09-28
@vilgeforce

At you sp_DataReceived is caused when the data comes to port. The data came - here is the answer ...

D
Dmitry Makarov, 2015-09-28
@DmitryITWorksMakarov

There is a proposal to pupate: close the port (Tx <-> Rx) and work out sending / receiving data. With such a stub, if you have chosen the correct port, then the data sent to it will be returned from it to the event handler: DataReceived. Or you can (if possible) close two COM ports to each other (Tx1 <-> Rx2, Tx2 <-> Rx1) and catch what was sent to the first port on the second port and vice versa.
After you have played enough with the ports, already deal with the exchange protocol of your device ....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question