同时访问具有不同线程的COM端口。 [英] Access to COM port with different threads at the same time.

查看:81
本文介绍了同时访问具有不同线程的COM端口。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello All,



在我的应用程序中,我使用SerialPort类来访问COM端口。这个COM端口分别由两个不同的threds访问,用于读写。



是否可以同时通过不同的线程并行访问同一个COM端口?



我是否需要使用SerialPort对象实现访问COM端口的关键部分?



谢谢提前:)

解决方案

为什么不使用 DataReceived-Event [ ^ ]?



或者您可以访问BaseStream [


我只知道Win32的读/写线程方法。


我是使用 SerialPort    .net框架,也是我am 使用收到的数据 event 

假设某些情况下,线程t1 写入字节 COM端口&同时,线程t2 尝试 读取 byte 来自同一COM端口的数据 已收到 event

然后 可能 访问权限COM端口(相同的串行端口 对象)同时 for 阅读&写

我需要 使用 SerialPort对象实现关键部分 访问COM端口


嗯,我不是100%肯定,但我认为使用Com端口是不可能的,因为你需要设备试图同时使用相同的IC时间和UART会因此而混淆(或损坏)。

我认为你需要的是PC接口板外面的PC接收数据并发送数据。这样你就可以在一个块中写入数据发送它,电路板可以根据需要切断数据并发送它。

一旦发送,你可以听取回复。使用上面提到的Data-Recieved事件我在过去做了类似的事情,但是没有遇到使用串口类访问COM的实现关键部分是不是让基于COM的应用程序与NET通信框架工作(如果它是我看到麻烦来你的方式!)



格伦

(哦,为什么代码标签?)

Hello All,

In my application, I am using SerialPort class to access a COM port. This COM port is accessed by two different threds for reading & writing respectively.

Is it possible to access same COM port parallely through different threads at the same time?
Or
Do I need to implement critical section to access COM port using SerialPort object?

Thanks in advance :)

解决方案

Why don''t you use the DataReceived-Event[^]?

Or you could access the BaseStream [^] and use the asynchronous Read/Write-Methods (BeginRead/EndRead(BeginWrite/EndWrite).

I only know the Read/Write-thread approach from Win32.


I am using SerialPort class of .net framework , also I am using data received event.

Suppose, in certain scenario, thread t1 is writing bytes to COM port & at the same time, thread t2 is trying to read byte data from the same COM port as it already received event,

Then is it possible to access the COM port (same Serial Port class object) simulataneously for reading & writing or

Do I need to implement critical section to access COM port using SerialPort object?


Umm, I not 100% sure, but I don''t think that is possible using the Com port as you would have to devices trying to use the same IC at the same time and the UART would either get confused (or damaged) by that.
I think what you need is an SPI interface board out side of the PC to take the data in and send the data out. That way you could write the data in a block send it, the board could chop the data up as needed and send it.
Once sent you could listen for reply. Using the Data-Recieved event suggest above I have done similar things in the past, but have not come across the "implement critical section to access COM using the serial port class" Is it a hack to get COM based applications to talk to the NET frame work (if it is I see trouble coming your way!)

Glenn
(oh yeah why the code tags?)


这篇关于同时访问具有不同线程的COM端口。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆