Serial Port Communication on the same Windows machine not working

前端 未结 3 1720
星月不相逢
星月不相逢 2021-01-18 14:12

Excuse me, quick question:

I have this hardware setup:

Same machine: \"Com3\" -> USB -> To Serial -> To USB -> \"Com4\"

3条回答
  •  悲&欢浪女
    2021-01-18 14:51

    I've altered from the example you linked:

    To actually have both ports running to read and write back and forth you will actually need to implement threading for reading and writing for both.

    It can be a good idea to use a timer.

    public static void Main()
    {
        SerialPort SendSerialPort = new SerialPort("Com3", 9600);
        SerialPort ReceiveSerialPort = new SerialPort("Com4", 9600);
    
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);
    
        // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;
    
        SendSerialPort.Open();
        ReceiveSerialPort.Open();
        bool _continue = true;
        readThread.Start();
    
        Console.Write("Name: ");
        name = Console.ReadLine();
    
        Console.WriteLine("Type QUIT to exit");
    
        while (_continue)
        {
            message = Console.ReadLine();
    
            if (stringComparer.Equals("quit", message))
                _continue = false;
            else
                SendSerialPort.WriteLine(String.Format("<{0}>: {1}", name, message));
        }
        readThread.Join();
        SendSerialPort.Close();
    }
    
    public static void Read()
    {
        while (_continue)
        {
            try
            {
                string message = ReceiveSerialPort.ReadLine();
                Console.WriteLine(message);
            }
            catch (TimeoutException) { }
        }
    }
    

    Usually there will be a beginning and end value within the written data to tell the other port that the message is finished and also for the ports to validate that they are reading data they should be, usually with commands of what to do with that data. (out of scope for this question).

    Also lacking and important is the intialisation of your ports.

    I prefer to use the default constructor (preference only)

    SerialPort Constructor ()
    

    And then set any values like so:

    _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
    _serialPort.Parity = SetPortParity(_serialPort.Parity);
    _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
    _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
    _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);
    

    All the constructors will give these values:

    This constructor uses default property values when none are specified. For example, the DataBits property defaults to 8, the Parity property defaults to the None enumeration value, the StopBits property defaults to 1, and a default port name of COM1.

    Even the handshake has a default value. If you look at the source code.

    private const Handshake defaultHandshake = Handshake.None;
    

提交回复
热议问题