我正在尝试用C#替换C程序。 C程序通过COM1串行端口连接到较旧的计算机。我没有关于C代码如何连接到其他机器的任何信息,但它确实正确连接,因此我必须猜测它是如何做到的。我一直在使用Portmon来试图找出C程序如何成功编写和阅读。当我的新C#应用程序运行时,我也一直在运行Portmon。在我的C#程序中,我使用的是SerialPort类。
我试图通过在工作程序上使用Portmon的输出来确定SerialPort类中的不同项应该具有的值。我的想法是,如果我能够为类获得正确的值,它将正确地写入和读取。程序(如下)无法正常工作。程序成功打开COM1端口,但在尝试写入句点(然后是另一个句点)时失败。这些会超时。
查看在旧C程序后面运行的Portmon的输出,工作程序打开COM1端口,然后在从COM1端口读取成功值(& OK)之前写入一个句点然后再写入另一个句点。旧程序设置了几个我希望在C#程序中复制的值。
我已尝试过以下各种SerialPort选项的组合,但我显然没有碰到任何东西。
BTW,我在Stack Overflow问题中提出了一些初步问题 Reading and writing from a serial port in C#, part 2
我在运行程序的计算机上使用Windows XP。 COM1端口另一侧的机器是一台非常旧的PC。
有更好的方法吗?使用Portmon以外的东西?对于旧程序的Portmon输出,Process列是ntvdm.exe。那是问题吗?我不应该使用C#SerialPort类吗? 可能最重要的问题是:我应该使用什么值来使Serialport类与旧程序匹配?
我的C#程序:
SerialPort comport = new SerialPort();
comport.BaudRate = 9600;
comport.DataBits = 7;
comport.StopBits = StopBits.One;
comport.Parity = Parity.Odd;
comport.RtsEnable = true;
comport.DtrEnable = true;
comport.Handshake = Handshake.RequestToSend;
comport.ReadBufferSize = 8192;
comport.WriteBufferSize = 100;
comport.WriteTimeout = 30000; // 30 sec
comport.ReadTimeout = 30000; // 30 sec
comport.PortName = "COM1";
string tempFbuffer;
byte[] Fbuffer = new byte[200];
string alldata5;
tempFbuffer = "..";
for (int cnt = 0; cnt < tempFbuffer.Length; cnt++)
{
Fbuffer[cnt] = Convert.ToByte(tempFbuffer[cnt]);
}
comport.Open();
comport.Write(Fbuffer, 0, 1);
comport.Write(Fbuffer, 1, 1);
for (i = 0; i < 4; i++)
{
alldata5 = alldata5 + comport.ReadChar();
}
comport.Close();
我想模仿的应用程序的Portmon输出如下。此应用程序连接到其他计算机并正常工作。我手动将标题放在顶部。
如您所见,在第33和34行有成功的写入,然后在第35和36行,有成功的读取。我在后台运行了Portmon程序。
# Time Process Reuest Port Result Other
0 0.00004407 ntvdm.exe IRP_MJ_CREATE Serial0 SUCCESS Options: Open
1 0.00000198 ntvdm.exe IOCTL_SERIAL_GET_BAUD_RATE Serial0 SUCCESS
2 0.00000115 ntvdm.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
3 0.00000104 ntvdm.exe IOCTL_SERIAL_GET_CHARS Serial0 SUCCESS
4 0.00000106 ntvdm.exe IOCTL_SERIAL_GET_HANDFLOW Serial0 SUCCESS
5 0.0000008 ntvdm.exe IOCTL_SERIAL_GET_BAUD_RATE Serial0 SUCCESS
6 0.00000082 ntvdm.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
7 0.00000085 ntvdm.exe IOCTL_SERIAL_GET_CHARS Serial0 SUCCESS
8 0.00000081 ntvdm.exe IOCTL_SERIAL_GET_HANDFLOW Serial0 SUCCESS
9 0.00000712 ntvdm.exe IOCTL_SERIAL_SET_BAUD_RATE Serial0 SUCCESS Rate: 1200
10 0.00000349 ntvdm.exe IOCTL_SERIAL_CLR_RTS Serial0 SUCCESS
11 0.00000366 ntvdm.exe IOCTL_SERIAL_CLR_DTR Serial0 SUCCESS
12 0.00000225 ntvdm.exe IOCTL_SERIAL_SET_LINE_CONTROL Serial0 SUCCESS StopBits: 1 Parity: NONE WordLength: 5
13 0.00000111 ntvdm.exe IOCTL_SERIAL_SET_CHAR Serial0 SUCCESS EOF:0 ERR:0 BRK:0 EVT:0 XON:11 XOFF:13
14 0.00000735 ntvdm.exe IOCTL_SERIAL_SET_HANDFLOW Serial0 SUCCESS Shake:0 Replace:0 XonLimit:2048 XoffLimit:512
15 0.00000133 ntvdm.exe IOCTL_SERIAL_LSRMST_INSERT Serial0 SUCCESS Char: ffffffff
16 0.00000338 ntvdm.exe IOCTL_SERIAL_SET_QUEUE_SIZE Serial0 SUCCESS InSize: 8192 OutSize: 100
17 0.00000083 ntvdm.exe IOCTL_SERIAL_GET_TIMEOUTS Serial0 SUCCESS
18 0.00000092 ntvdm.exe IOCTL_SERIAL_SET_TIMEOUTS Serial0 SUCCESS RI:-1 RM:0 RC:0 WM:0 WC:0
19 0.00000349 ntvdm.exe IOCTL_SERIAL_SET_DTR Serial0 SUCCESS
20 0.00000342 ntvdm.exe IOCTL_SERIAL_SET_RTS Serial0 SUCCESS
21 0.00001121 ntvdm.exe IOCTL_SERIAL_SET_BAUD_RATE Serial0 SUCCESS Rate: 1200
22 0.00000262 ntvdm.exe IOCTL_SERIAL_SET_WAIT_MASK Serial0 SUCCESS Mask: RXCHAR CTS DSR RLSD ERR RING
23 36.94054111 ntvdm.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0 SUCCESS
24 0.00000403 ntvdm.exe IOCTL_SERIAL_CLR_DTR Serial0 SUCCESS
25 0.00000356 ntvdm.exe IOCTL_SERIAL_CLR_RTS Serial0 SUCCESS
26 0.00000351 ntvdm.exe IOCTL_SERIAL_SET_DTR Serial0 SUCCESS
27 0.00000348 ntvdm.exe IOCTL_SERIAL_SET_RTS Serial0 SUCCESS
28 0.00000717 ntvdm.exe IOCTL_SERIAL_SET_BAUD_RATE Serial0 SUCCESS Rate: 9600
29 0.00000145 ntvdm.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
30 0.00000246 ntvdm.exe IOCTL_SERIAL_SET_LINE_CONTROL Serial0 SUCCESS StopBits: 1 Parity: ODD WordLength: 5
31 0.00000086 ntvdm.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
32 0.00000226 ntvdm.exe IOCTL_SERIAL_SET_LINE_CONTROL Serial0 SUCCESS StopBits: 1 Parity: ODD WordLength: 7
33 0.00002222 ntvdm.exe IRP_MJ_WRITE Serial0 SUCCESS Length 1: .
34 0.00002142 ntvdm.exe IRP_MJ_WRITE Serial0 SUCCESS Length 1: .
35 0.00000562 ntvdm.exe IRP_MJ_READ Serial0 SUCCESS Length 4: &OK.
36 0.00000239 ntvdm.exe IRP_MJ_READ Serial0 SUCCESS Length 0:
37 0.00000533 ntvdm.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0 SUCCESS
38 0.0000023 ntvdm.exe IRP_MJ_READ Serial0 SUCCESS Length 0:
39 95.8854497 ntvdm.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0 SUCCESS
40 0.00002486 ntvdm.exe IRP_MJ_WRITE Serial0 SUCCESS Length 1: D
我正在创建的C#应用程序的Portmon输出如下。上面的C#代码,当在后台运行Portmon时会创建此输出。我把标题放进去了。
如您所见,写入期间第69行存在超时错误。我需要将它运行得足够接近C,以便写入和读取工作。
显然波特率,字长,奇偶校验和其他几个都设置正确。
# Time Process Reuest Port Result Other
0 0.00004362 fancom.exe IRP_MJ_CREATE Serial0 SUCCESS Options: Open
1 0.0000019 fancom.exe IOCTL_SERIAL_GET_PROPERTIES Serial0 SUCCESS
2 0.00000263 fancom.exe IOCTL_SERIAL_GET_MODEMSTATUS Serial0 SUCCESS
3 0.00000096 fancom.exe IOCTL_SERIAL_GET_BAUD_RATE Serial0 SUCCESS
4 0.00000097 fancom.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
5 0.00000084 fancom.exe IOCTL_SERIAL_GET_CHARS Serial0 SUCCESS
6 0.00000097 fancom.exe IOCTL_SERIAL_GET_HANDFLOW Serial0 SUCCESS
7 0.00000081 fancom.exe IOCTL_SERIAL_GET_BAUD_RATE Serial0 SUCCESS
8 0.00000088 fancom.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
9 0.0000008 fancom.exe IOCTL_SERIAL_GET_CHARS Serial0 SUCCESS
10 0.00000079 fancom.exe IOCTL_SERIAL_GET_HANDFLOW Serial0 SUCCESS
11 0.00000715 fancom.exe IOCTL_SERIAL_SET_BAUD_RATE Serial0 SUCCESS Rate: 9600
12 0.00000355 fancom.exe IOCTL_SERIAL_CLR_DTR Serial0 SUCCESS
13 0.0000024 fancom.exe IOCTL_SERIAL_SET_LINE_CONTROL Serial0 SUCCESS StopBits: 1 Parity: ODD WordLength: 7
14 0.00000107 fancom.exe IOCTL_SERIAL_SET_CHAR Serial0 SUCCESS EOF:1a ERR:3f BRK:3f EVT:1a XON:11 XOFF:13
15 0.00000779 fancom.exe IOCTL_SERIAL_SET_HANDFLOW Serial0 SUCCESS Shake:8 Replace:84 XonLimit:1024 XoffLimit:1024
16 0.0000008 fancom.exe IOCTL_SERIAL_GET_BAUD_RATE Serial0 SUCCESS
17 0.00000081 fancom.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
18 0.00000081 fancom.exe IOCTL_SERIAL_GET_CHARS Serial0 SUCCESS
19 0.00000081 fancom.exe IOCTL_SERIAL_GET_HANDFLOW Serial0 SUCCESS
20 0.00000705 fancom.exe IOCTL_SERIAL_SET_BAUD_RATE Serial0 SUCCESS Rate: 9600
21 0.00000349 fancom.exe IOCTL_SERIAL_SET_DTR Serial0 SUCCESS
22 0.0000022 fancom.exe IOCTL_SERIAL_SET_LINE_CONTROL Serial0 SUCCESS StopBits: 1 Parity: ODD WordLength: 7
23 0.00000098 fancom.exe IOCTL_SERIAL_SET_CHAR Serial0 SUCCESS EOF:1a ERR:3f BRK:3f EVT:1a XON:11 XOFF:13
24 0.00000493 fancom.exe IOCTL_SERIAL_SET_HANDFLOW Serial0 SUCCESS Shake:9 Replace:84 XonLimit:1024 XoffLimit:1024
25 0.00000684 fancom.exe IOCTL_SERIAL_SET_DTR Serial0 SUCCESS
26 0.00000108 fancom.exe IOCTL_SERIAL_SET_TIMEOUTS Serial0 SUCCESS RI:-1 RM:-1 RC:30000 WM:0 WC:30000
27 0.00000227 fancom.exe IOCTL_SERIAL_SET_WAIT_MASK Serial0 SUCCESS Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING
28 35.62327662 fancom.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0 SUCCESS
29 0.00000399 fancom.exe IOCTL_SERIAL_SET_QUEUE_SIZE Serial0 SUCCESS InSize: 8192 OutSize: 100
30 30.00157726 fancom.exe IRP_MJ_WRITE Serial0 TIMEOUT Length 1: .
31 0.00000767 fancom.exe IRP_MJ_FLUSH_BUFFERS Serial0 SUCCESS
32 0.00001012 fancom.exe IOCTL_SERIAL_SET_WAIT_MASK Serial0 SUCCESS Mask:
33 0.00000402 fancom.exe IOCTL_SERIAL_CLR_DTR Serial0 SUCCESS
34 0.00000116 fancom.exe IRP_MJ_FLUSH_BUFFERS Serial0 SUCCESS
35 0.0000023 fancom.exe IOCTL_SERIAL_PURGE Serial0 SUCCESS Purge: RXABORT RXCLEAR
36 0.00000163 fancom.exe IOCTL_SERIAL_PURGE Serial0 SUCCESS Purge: TXABORT TXCLEAR
37 0.00000404 fancom.exe IRP_MJ_CLEANUP Serial0 SUCCESS
38 0.00322359 fancom.exe IRP_MJ_CLOSE Serial0 SUCCESS
39 0.00004607 fancom.exe IRP_MJ_CREATE Serial0 SUCCESS Options: Open
40 0.00000188 fancom.exe IOCTL_SERIAL_GET_PROPERTIES Serial0 SUCCESS
41 0.00000277 fancom.exe IOCTL_SERIAL_GET_MODEMSTATUS Serial0 SUCCESS
42 0.00000092 fancom.exe IOCTL_SERIAL_GET_BAUD_RATE Serial0 SUCCESS
43 0.00000112 fancom.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
44 0.0000008 fancom.exe IOCTL_SERIAL_GET_CHARS Serial0 SUCCESS
45 0.00000093 fancom.exe IOCTL_SERIAL_GET_HANDFLOW Serial0 SUCCESS
46 0.00000079 fancom.exe IOCTL_SERIAL_GET_BAUD_RATE Serial0 SUCCESS
47 0.00000085 fancom.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
48 0.00000081 fancom.exe IOCTL_SERIAL_GET_CHARS Serial0 SUCCESS
49 0.00000082 fancom.exe IOCTL_SERIAL_GET_HANDFLOW Serial0 SUCCESS
50 0.00000704 fancom.exe IOCTL_SERIAL_SET_BAUD_RATE Serial0 SUCCESS Rate: 9600
51 0.00000352 fancom.exe IOCTL_SERIAL_CLR_DTR Serial0 SUCCESS
52 0.00000225 fancom.exe IOCTL_SERIAL_SET_LINE_CONTROL Serial0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
53 0.00000113 fancom.exe IOCTL_SERIAL_SET_CHAR Serial0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
54 0.00000489 fancom.exe IOCTL_SERIAL_SET_HANDFLOW Serial0 SUCCESS Shake:8 Replace:80 XonLimit:1024 XoffLimit:1024
55 0.00000084 fancom.exe IOCTL_SERIAL_GET_BAUD_RATE Serial0 SUCCESS
56 0.00000083 fancom.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial0 SUCCESS
57 0.00000081 fancom.exe IOCTL_SERIAL_GET_CHARS Serial0 SUCCESS
58 0.00000081 fancom.exe IOCTL_SERIAL_GET_HANDFLOW Serial0 SUCCESS
59 0.00000696 fancom.exe IOCTL_SERIAL_SET_BAUD_RATE Serial0 SUCCESS Rate: 9600
60 0.00000344 fancom.exe IOCTL_SERIAL_SET_DTR Serial0 SUCCESS
61 0.00000222 fancom.exe IOCTL_SERIAL_SET_LINE_CONTROL Serial0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
62 0.00000102 fancom.exe IOCTL_SERIAL_SET_CHAR Serial0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
63 0.00000474 fancom.exe IOCTL_SERIAL_SET_HANDFLOW Serial0 SUCCESS Shake:9 Replace:80 XonLimit:1024 XoffLimit:1024
64 0.00000345 fancom.exe IOCTL_SERIAL_SET_DTR Serial0 SUCCESS
65 0.00000081 fancom.exe IOCTL_SERIAL_SET_TIMEOUTS Serial0 SUCCESS RI:-1 RM:-1 RC:30000 WM:0 WC:30000
66 0.00000192 fancom.exe IOCTL_SERIAL_SET_WAIT_MASK Serial0 SUCCESS Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING
67 30.00755135 fancom.exe IOCTL_SERIAL_WAIT_ON_MASK Serial0 SUCCESS
68 0.00000353 fancom.exe IOCTL_SERIAL_SET_QUEUE_SIZE Serial0 SUCCESS InSize: 8192 OutSize: 100
69 29.99287343 fancom.exe IRP_MJ_WRITE Serial0 TIMEOUT Length 1: .
70 0.00000349 fancom.exe IRP_MJ_FLUSH_BUFFERS Serial0 SUCCESS
71 0.00000985 fancom.exe IOCTL_SERIAL_SET_WAIT_MASK Serial0 SUCCESS Mask:
答案 0 :(得分:0)
PortMon输出\ n。
的句点