通过串行端口发送和接收;不起作用

本文关键字:不起作用 串行端口 | 更新日期: 2023-09-27 18:19:44

我是编程新手。

我正在制作一个终端程序进行一些测试,该程序必须通过串行零调制解调器发送和接收数据。我在MSDN上找到了一个例子:http://msdn.microsoft.com/en-us/library/y2sxhat8.aspx?cs-保存lang=1&cs lang=csharp#code-snippet-2

但是我不能让它工作。这是我现在拥有的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.Threading;
namespace Terminal_0._0._0._2
{
    class Program
    {
        public static void Main()
        {
            string name;
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            Thread readThread = new Thread(Read);
            // Create a new SerialPort object with default settings.
            SerialPort _serialPort = new SerialPort();
            // Allow the user to set the appropriate properties.
            _serialPort.PortName = "COM8";
            _serialPort.BaudRate = 115200;
            _serialPort.Parity = Parity.None;
            _serialPort.DataBits = 8;
            _serialPort.StopBits = StopBits.One;
            _serialPort.Handshake = Handshake.None;
            // Set the read/write timeouts
            _serialPort.ReadTimeout = 500;
            _serialPort.WriteTimeout = 500;
            _serialPort.Open();
            var _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
                {
                    _serialPort.WriteLine(
                        String.Format("<{0}>: {1}", name, message));
                }
            }
            readThread.Join();
            _serialPort.Close();
        }

    }
    public static void Read()
        {
            while (_continue)
            {
                try
                {
                    string message = _serialPort.ReadLine();
                    Console.WriteLine(message);
                }
                catch (TimeoutException) { }
            }
        }
}

我收到以下错误:应为类、委托、枚举、接口或结构在线:66列:19

提前谢谢。

通过串行端口发送和接收;不起作用

据我所知,可能有几件事导致它无法工作。首先,Read方法在Program范围之外,导致它无法工作。其次,在您还创建了"_continue"answers"_serialPort"字段(外部方法)之前,将其移入内部也不会起作用。

返工代码(删除多余的"使用"语句):

using System;
using System.IO.Ports;
using System.Threading;
namespace Terminal_0._0._0._2
{
    class Program
    {
        private static bool _continue;
        private static SerialPort _serialPort;
        public static void Main()
        {
            string name;
            string message;
            StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
            var readThread = new Thread(Read);
            // Create a new SerialPort object with default settings.
            _serialPort = new SerialPort
                {
                    PortName = "COM8",
                    BaudRate = 115200,
                    Parity = Parity.None,
                    DataBits = 8,
                    StopBits = StopBits.One,
                    Handshake = Handshake.None,
                    ReadTimeout = 500,
                    WriteTimeout = 500
                };
            // Allow the user to set the appropriate properties.
            // Set the read/write timeouts
            _serialPort.Open();
            _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
                {
                    _serialPort.WriteLine(
                        String.Format("<{0}>: {1}", name, message));
                }
            }
            readThread.Join();
            _serialPort.Close();
        }
        public static void Read()
        {
            while (_continue)
            {
                try
                {
                    string message = _serialPort.ReadLine();
                    Console.WriteLine(message);
                }
                catch (TimeoutException) { }
            }
        }
    }
}

由于我没有任何串行设备,我无法测试它,但编译器编译它时没有出错。

谢谢Bjarke