同一台Windows计算机上的串口通信无法正常工作 [英] Serial Port Communication on the same Windows machine not working

查看:229
本文介绍了同一台Windows计算机上的串口通信无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,快速的问题:

我有这样的硬件设置:

同一台机器上: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.

我期待收到字符串测试并将其分配给我的 VAR消息。你能告诉我我在做什么错在这里。

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?

修改

我测试使用 串行端口实用应用程序 和的 它只是正常工作

I tested my hardware using the Serial Port Utility Application and it worked just fine.

推荐答案

我已经修改的从您链接的例子:

要实际上有两个端口上运行的读取和写入,并回第四,你实际上需要实现阅读和写作两个线程。

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).

同样缺乏,重要的是你的端口intialisation。

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:

此构造用途当没有指定默认属性值。的例如,数据位​​属性默认为8,奇偶校验属性默认为无枚举值时,停止位属性默认为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计算机上的串口通信无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆