c# - 如何在 c sharp 中的单个 com 端口同时接收多个任务

标签 c# serial-port communication

我是 c sharp 的新手,我坚持执行此任务。我的要求是,我创建了许多线程,这些线程(使用 COM1 发送)必须与单个串行端口通信,比如 COM2。所有线程都必须使用单个 COM 端口发送消息(使用 COM2 接收)。

例如,使用 COM1 发送“helloworld1”(使用线程 1)和“helloworld2”(线程 2),并在 super 终端中使用 COM2 接收。所以我需要同时在 super 终端中看到两个 hell 世界。

请帮帮我。

最佳答案

由于您希望从两个不同的线程发送,因此您需要用锁{}包围您的调用 SerialPort.Write(),如下所示:


SerialPort s = new SerialPort();

//configure serial port, etc.

//spin off additional threads

//in each thread, do this:
lock(s)
{
  s.Write("Hello World1");
}

关于c# - 如何在 c sharp 中的单个 com 端口同时接收多个任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3344653/

相关文章:

c# - 密码正则表达式验证

c# - IsOrderedBy 扩展方法

c# - 仅从字符串中取出数字并放入数组中

android - RFID在BluetoothSocket中获得串行连接

c - 将 rs232 编程为 lcd (Linux)

c++ - R 和 C++ 之间的通信

java - 为Android创建一个 Remote

c# - 验证用户输入和类型

c++ - 串口异步IO

.net - 在没有 Web 服务器的情况下使用 SSL 通过 TCP/IP 套接字进行客户端/服务器通信