如何使用c#找到发送MSG后得到响应的串口?

发布于 2024-08-28 19:12:10 字数 93 浏览 5 评论 0原文

目的是找到哪个串行端口物理连接到其他机器。我想搜索我的系统以查找可用的串行端口并使用所有端口发送测试消息并等待响应。并获取得到响应的端口号连接到另一台机器。怎么办??谢谢。

The objectve is to find which Serial port is phyisically connected to other machine. I want to Search my system to find the available serial ports and send a test message using all the ports and wait for the response. And get the port numbers of Which ever ports get a response is connected to the other machine. How to do?? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏至、离别 2024-09-04 19:12:10

SerialPort.GetPortNames() 方法返回所有可用串行端口的 COM 端口名称数组。您可以迭代它并尝试 Open() 它们。预计会失败,该端口可能已被另一个程序打开。

您不想将某些内容发送到不期望的设备。使用 DsrHolding 属性是一种合理的测试,可以查看连接的设备是否已通电。

您的最终敌人将是波特率属性。您无法猜测正确的值。串行端口太原始,无法支持即插即用式的设备发现。避免使用无法可靠工作的东西,允许您的用户使用您需要的设置来配置您的程序。这是正常做法。

The SerialPort.GetPortNames() method returns an array of the COM port names of all available serial ports. You could iterate it and try to Open() them. Expect failure, the port might already be opened by another program.

You don't want to send something to a device that doesn't expect it. Using the DsrHolding property is a reasonable test to see if a device is attached that's powered up.

Your ultimate nemesis is going to be the Baudrate property. You cannot guess the proper value. Serial ports are way too primitive to support plug-and-play style device discovery. Avoid using something that cannot work reliably, allow you user to configure your program with the settings you need. It is the normal practice.

猫卆 2024-09-04 19:12:10

使用系统.IO.端口。

public static void OnSerialDataReceived(object sender, 
                                        SerialDataReceivedEventArgs args)
{
  string data = ComPort.ReadExisting();
  Console.Write(data.Replace("\r", "\n"));
}


private static void InitializeComPort(string port, int baud)
{
  ComPort = new SerialPort(port, baud);
  // ComPort.PortName = port;
  // ComPort.BaudRate = baud;
  ComPort.Parity = Parity.None;
  ComPort.StopBits = StopBits.One;
  ComPort.DataBits = 8;
  ComPort.Handshake = Handshake.None;
  ComPort.DataReceived += OnSerialDataReceived;
  ComPort.Open();
}

有关 Mark Michaelis 博客的更多信息

Use System.IO.Ports.

public static void OnSerialDataReceived(object sender, 
                                        SerialDataReceivedEventArgs args)
{
  string data = ComPort.ReadExisting();
  Console.Write(data.Replace("\r", "\n"));
}


private static void InitializeComPort(string port, int baud)
{
  ComPort = new SerialPort(port, baud);
  // ComPort.PortName = port;
  // ComPort.BaudRate = baud;
  ComPort.Parity = Parity.None;
  ComPort.StopBits = StopBits.One;
  ComPort.DataBits = 8;
  ComPort.Handshake = Handshake.None;
  ComPort.DataReceived += OnSerialDataReceived;
  ComPort.Open();
}

more on Mark Michaelis' blog

永不分离 2024-09-04 19:12:10

SerialPort.GetPortNames() 方法是 SerialPort 的静态方法。您确定它们之间的通信具有流量控制吗?

SerialPort.GetPortNames() method is static methods for SerialPort .Do you sure communication between them have flow control?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文