与Virtual PC的COM端口通信(第2部分) [英] COM port communication with Virtual PC (part 2)

查看:101
本文介绍了与Virtual PC的COM端口通信(第2部分)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题与我的先前的问题有关.

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

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

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

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也没有进行任何更改.

PipeOptions.WriteThrough didn't changed anything, too.

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

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();

更新:

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

The code I am running on the guest os:

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

port.WriteLine("Hallo");

在命令提示符中使用'echo',就像telewin建议的一样. 回显和使用上面的代码有什么区别?

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

推荐答案

很抱歉收到答复,希望它仍然有用...

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

在我的测试中,"echo hello> com1"仅在之前起作用,然后才能在VPC中运行程序(启动新的SerialPort).运行它之后,主机程序将不再显示"echo hello> com1",直到重新启动来宾.

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.

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

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");

这篇关于与Virtual PC的COM端口通信(第2部分)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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