<bdo id='2ikv2'></bdo><ul id='2ikv2'></ul>
<legend id='2ikv2'><style id='2ikv2'><dir id='2ikv2'><q id='2ikv2'></q></dir></style></legend>

<tfoot id='2ikv2'></tfoot>

    <small id='2ikv2'></small><noframes id='2ikv2'>

  1. <i id='2ikv2'><tr id='2ikv2'><dt id='2ikv2'><q id='2ikv2'><span id='2ikv2'><b id='2ikv2'><form id='2ikv2'><ins id='2ikv2'></ins><ul id='2ikv2'></ul><sub id='2ikv2'></sub></form><legend id='2ikv2'></legend><bdo id='2ikv2'><pre id='2ikv2'><center id='2ikv2'></center></pre></bdo></b><th id='2ikv2'></th></span></q></dt></tr></i><div id='2ikv2'><tfoot id='2ikv2'></tfoot><dl id='2ikv2'><fieldset id='2ikv2'></fieldset></dl></div>

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

      时间:2023-11-10
      <tfoot id='uQOQ9'></tfoot>

    2. <i id='uQOQ9'><tr id='uQOQ9'><dt id='uQOQ9'><q id='uQOQ9'><span id='uQOQ9'><b id='uQOQ9'><form id='uQOQ9'><ins id='uQOQ9'></ins><ul id='uQOQ9'></ul><sub id='uQOQ9'></sub></form><legend id='uQOQ9'></legend><bdo id='uQOQ9'><pre id='uQOQ9'><center id='uQOQ9'></center></pre></bdo></b><th id='uQOQ9'></th></span></q></dt></tr></i><div id='uQOQ9'><tfoot id='uQOQ9'></tfoot><dl id='uQOQ9'><fieldset id='uQOQ9'></fieldset></dl></div>

      <small id='uQOQ9'></small><noframes id='uQOQ9'>

      <legend id='uQOQ9'><style id='uQOQ9'><dir id='uQOQ9'><q id='uQOQ9'></q></dir></style></legend>
        <bdo id='uQOQ9'></bdo><ul id='uQOQ9'></ul>
            <tbody id='uQOQ9'></tbody>

                本文介绍了同一台 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 机器上的串行端口通信不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:获取 USB 适配器的 com 端口号 下一篇:Windows XP 嵌入式版本上 System.IO.IOException 的原因是什么?

                相关文章

                <i id='HSwpb'><tr id='HSwpb'><dt id='HSwpb'><q id='HSwpb'><span id='HSwpb'><b id='HSwpb'><form id='HSwpb'><ins id='HSwpb'></ins><ul id='HSwpb'></ul><sub id='HSwpb'></sub></form><legend id='HSwpb'></legend><bdo id='HSwpb'><pre id='HSwpb'><center id='HSwpb'></center></pre></bdo></b><th id='HSwpb'></th></span></q></dt></tr></i><div id='HSwpb'><tfoot id='HSwpb'></tfoot><dl id='HSwpb'><fieldset id='HSwpb'></fieldset></dl></div>

                <tfoot id='HSwpb'></tfoot>

                  <bdo id='HSwpb'></bdo><ul id='HSwpb'></ul>

                <small id='HSwpb'></small><noframes id='HSwpb'>

                1. <legend id='HSwpb'><style id='HSwpb'><dir id='HSwpb'><q id='HSwpb'></q></dir></style></legend>