COM 端口与 Virtual PC 通信(第 2 部分)

发布于 2024-10-15 18:35:41 字数 913 浏览 4 评论 0原文

这个问题与我的之前的问题相关。

现在连接到管道已成功,但我仍然无法从端口读取(或写入)任何数据。

我的第一个猜测是,数据被缓冲了。但即使当我(在客户端站点上)写入 5000 字节(NamedPipeClientStream 中的缓冲区为 512 字节大)时,我也没有收到任何 数据。

PipeOptions.WriteThrough 也没有改变任何东西。

当我不使用管道而是使用文本文件(在虚拟 PC 设置中)来重定向写入 COM 端口的数据时,数据将按预期写入文本文件。因此,在 Virtual-PC 中运行的客户端测试程序运行良好。问题可能出在我下面的代码中。

var pipe = new NamedPipeClientStream(".", "mypipe", PipeDirection.InOut, PipeOptions.WriteThrough);

pipe.Connect();

// this is blocking
int i = pipe.ReadByte();

var reader = new StreamReader(pipe);
// this is blocking, too
var s = reader.ReadLine();

更新:

我在来宾操作系统上运行的代码:

var port = new SerialPort("COM1");
port.Open();

port.WriteLine("Hallo");

按照 telewin 的建议在命令提示符中使用“echo”效果很好。 回显和使用上面的代码有什么区别?

This question is related to my earlier question.

Connecting to the pipe is now successful, but I still cannot read (or write) any data from the port.

My first guess was, that the data are buffered. But even when I write (on the client site) 5000 bytes (the buffer in NamedPipeClientStream is 512 byte large), I do not receive any
data.

PipeOptions.WriteThrough didn't changed anything, too.

When I do not use a pipe, but a textfile (in the Virtual-PC settings) to redirect the data written to the COM-Port, the data are written as expected to the textfile. So the client test programm, running in Virtual-PC, is doing fine. The problem is likely in my code below.

var pipe = new NamedPipeClientStream(".", "mypipe", PipeDirection.InOut, PipeOptions.WriteThrough);

pipe.Connect();

// this is blocking
int i = pipe.ReadByte();

var reader = new StreamReader(pipe);
// this is blocking, too
var s = reader.ReadLine();

Update:

The code I am running on the guest os:

var port = new SerialPort("COM1");
port.Open();

port.WriteLine("Hallo");

Using 'echo' in an command prompt as telewin suggested works fine.
What is the difference between echoing and using the above code?

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

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

发布评论

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

评论(1

秋千易 2024-10-22 18:35:41

抱歉回复晚了,希望它仍然相关...

在我的测试中,“echo hello > com1”仅在您在 VPC 内运行程序(启动新的串行端口)之前才有效。运行它后,主机程序将不再看到“echo hello > com1”,直到客户机重新启动。

这表明串行端口本身的初始化做了一些永久性的事情。使用 Reflector,我们发现 SerialPort 的 ctor 没有执行任何操作,但它的 Open 方法调用了 SerialStream 的 ctor。这个构造函数做了很多事情:它设置读/写缓冲区、Rts/Dtr 和握手模式。经过一些试验,似乎 Rts/Dtr 搞砸了“echo hello > com1”。您能否在 VPC 内尝试一下修改后的代码:

var port = new SerialPort("com1");
port.DtrEnable = true;
port.RtsEnable = true;
port.Open();
port.WriteLine("Hallo");

Sorry for the late reply, hope it's still relevant...

In my tests, "echo hello > com1" only works before you run your program (which initiates a new SerialPort) inside VPC. After you run it, "echo hello > com1" will no longer be seen by the host program, until the guest is rebooted.

This suggests that the initialization of the SerialPort itself does something permanent. Using Reflector we find that SerialPort's ctor does nothing of consequence, but its Open method calls the ctor for SerialStream. This ctor does quite a bit: it sets read/write buffers, Rts/Dtr, and handshake mode. After some trials, it seems that the Rts/Dtr screw up the "echo hello > com1". Can you please try this modified code inside VPC:

var port = new SerialPort("com1");
port.DtrEnable = true;
port.RtsEnable = true;
port.Open();
port.WriteLine("Hallo");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文