我正在尝试用 C# 替换 C 程序。C 程序通过 COM1 串行端口连接到旧计算机。我没有任何关于 C 代码如何连接到另一台机器的信息,但它确实连接正确,所以我必须猜测它是如何做到的。我一直在使用Portmon试图弄清楚 C 程序是如何成功读写的。当我的新 C# 应用程序运行时,我也一直在运行 Portmon。在我的 C# 程序中,我使用 SerialPort 类。
我试图通过在工作程序上使用 Portmon 的输出来确定 SerialPort 类中的不同项目应该具有什么值。我的想法是,如果我能得到正确的类的值,它就会正确地写入和读取。程序(如下)无法正常工作。程序成功打开 COM1 端口,但在尝试写入句点(然后是另一个句点)时失败。这些给出了超时。
查看在旧 C 程序后面运行的 Portmon 的输出,工作程序打开 COM1 端口,然后写一个句点,然后再写一个句点,然后从 COM1 端口读取成功值(&OK)。旧程序设置了几个我想在我的 C# 程序中复制的值。
我已经尝试了以下 SerialPort 选项的各种组合,但显然我没有找到任何东西。
顺便说一句,我在 Stack Overflow 问题Read 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: