同一台 Windows 机器上的串行端口通信不起作用

Serial Port Communication on the same Windows machine not working(同一台 Windows 机器上的串行端口通信不起作用)

本文介绍了同一台 Windows 机器上的串行端口通信不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,问个简单的问题:

Excuse me, quick question:

我有这个硬件设置:

同一台机器:Com3" -> USB -> 转串口 -> 转 USB -> Com4"

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

我关注了 MSDN SerialPort 类 和 MSDN SerialPort.ReadLine() 构建这个例程:

And I followed MSDN SerialPort Class and MSDN SerialPort.ReadLine() to build this routine:

SerialPort SendSerialPort = new SerialPort("Com3", 9600);
SerialPort ReceiveSerialPort = new SerialPort("Com4", 9600);

SendSerialPort.Open();
ReceiveSerialPort.Open();

SendSerialPort.WriteLine("Test");
var message = ReceiveSerialPort.ReadLine(); // control stops here

SendSerialPort.Close();
ReceiveSerialPort.Close();

Console.WriteLine(message);

但是,当我倾向于 ReadLine() 时,我的控件会停止并等待.我原本没想到.

However, when I tend to ReadLine(), my control stops and just waits. I did not expect that.

我希望收到字符串 Test 并将其分配给我的 var message.你能告诉我我在这里做错了什么吗?

I am expecting to receive the string Test and assign it to my var message. Could you please tell me what am I doing wrong here?

编辑:

我使用 Serial Port Utility Application效果很好.

推荐答案

我已经修改了 来自您链接的示例:

要真正让两个端口都运行以来回读写,您实际上需要为两者的读写实现线程.

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:

此构造函数在没有默认属性值时使用 例如,DataBits 属性默认为 8,Parity 属性默认为 None 枚举值,StopBits 属性默认为 1,默认端口名称为 COM1.

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;

这篇关于同一台 Windows 机器上的串行端口通信不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:同一台 Windows 机器上的串行端口通信不起作用