【问题标题】:Read from COM port in C# [duplicate]从 C# 中的 COM 端口读取 [重复]
【发布时间】:2011-08-28 19:29:54
【问题描述】:

可能重复:
How to READ and Write from the Serial Port in C#

如何在 C# 中从 COM 端口读取数据?更具体地说,我需要读取可变长度的数据。如果可能,请提供一个例子。提前致谢

【问题讨论】:

  • 您能否详细说明您正在尝试做什么?数据如何发送给您等...
  • 你搜索过这个网站吗?这个问题已经出现很多次了。见here

标签: c# serial-port


【解决方案1】:

您需要使用System.IO.Ports.SerialPort 类或System.IO.Ports 命名空间。

page 有一些示例和更多信息。

这是一个例子:

    // This is a new namespace in .NET 2.0
    // that contains the SerialPort class using System.IO.Ports;

 private static void SendSampleData()

 { 

// Instantiate the communications
// port with some basic settings

 SerialPort port = new SerialPort(
    "COM1", 9600, Parity.None, 8, StopBits.One); 

// Open the port for communications
 port.Open();

 // Write a string 

port.Write("Hello World"); 

// Write a set of bytes 

port.Write(new byte[] {0x0A, 0xE2, 0xFF}, 0, 3); 

// Close the port 

port.Close(); }

【讨论】:

  • 这似乎显示了如何写作,而问题询问如何阅读
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多