[英]Reading and writing from a serial port in C#, part 2
I am trying to replace a C program with a C# one. 我正在尝试用C#替换C程序。 The C program connects to an older computer via the COM1 serial port. C程序通过COM1串行端口连接到旧计算机。 I do not have any kind of information on how the C code connects to the other machine, but it does connect correctly, so I have to guess as to how it is doing it. 我没有任何有关C代码如何连接到另一台计算机的信息,但是它确实可以正确连接,因此我不得不猜测它的运行方式。 I have been using Portmon to try to figure out how the C program is writing and reading successfully. 我一直在使用Portmon尝试弄清楚C程序是如何成功编写和读取的。 I have also been running Portmon when my new C# application is running. 当我的新C#应用程序运行时,我也一直在运行Portmon。 In my C# program, I use the SerialPort class. 在我的C#程序中,我使用SerialPort类。
I am trying to determine what values the different items in the SerialPort class should have by using the output of Portmon on the working program. 我试图通过在工作程序上使用Portmon的输出来确定SerialPort类中不同项目应具有的值。 My thought is that if I can get the values correct for the class, it would write and read correctly. 我的想法是,如果我可以为该类获取正确的值,则它将正确写入和读取。 The program (below) is not working correctly. 程序(如下)无法正常工作。 The program opens the COM1 port successfully, but it fails when it tries to write a period (and then another period). 该程序成功打开了COM1端口,但是在尝试写入一个句点(然后是另一个句点)时失败。 These give a timeout. 这些给超时。
Looking at the output of Portmon running behind the old C program, the working program opened the COM1 port and then wrote one period and then another before reading a successful value (&OK) from the COM1 port. 查看运行在旧C程序后面的Portmon的输出,工作程序打开COM1端口,然后先写入一个句点,然后再写入另一个句点,然后再从COM1端口读取成功值(&OK)。 The old program set up several values that I want to duplicate in my C# program. 旧程序设置了几个要在C#程序中复制的值。
I have tried a variety of combinations of the SerialPort options below, but I obviously have not hit on anything. 我在下面尝试了SerialPort选项的各种组合,但显然我没有遇到任何问题。
BTW, I asked some preliminary questions in Stack Overflow question Reading and writing from a serial port in C#, part 2 顺便说一句,我问了一些有关Stack Overflow问题的初步问题,即在C#中从串行端口读取和写入数据,第2部分
I am using Windows XP on the machine that is running the programs. 我在运行程序的计算机上使用Windows XP。 The machine on the other side of the COM1 port is a very old PC. COM1端口另一侧的计算机是一台非常旧的PC。
Is there a better way to do this? 有一个更好的方法吗? Use something other than Portmon? 使用Portmon以外的其他东西? For the Portmon output for the old program the Process column is ntvdm.exe. 对于旧程序的Portmon输出,“处理”列为ntvdm.exe。 Is that a problem? 那是问题吗? Should I not use the C# SerialPort class? 我不应该使用C#SerialPort类吗? Probably the most important question is: What values should I use for the Serialport class to match the old program? 可能最重要的问题是:我应该为Serialport类使用什么值来匹配旧程序?
My C# program: 我的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();
The Portmon output of the application I want to mimic is below. 我要模仿的应用程序的Portmon输出如下。 This application connects to the other machine and works correctly. 该应用程序连接到另一台机器并正常工作。 I manually put the headings in at the top. 我手动将标题放在顶部。
As you can see, on lines 33 and 34 there are successful writes and then on lines 35 and 36, there are successful reads. 如您所见,在第33和34行上成功写入,然后在第35和36行上成功读取。 I ran the program with Portmon in the background. 我在后台运行了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
The Portmon output of the C# application that I am creating is below. 我正在创建的C#应用程序的Portmon输出如下。 The C# code above, when run with Portmon in the background creates this output. 上面的C#代码在后台运行Portmon时创建此输出。 I put in the headers. 我放在标题中。
As you can see, there is a timeout error on line 69 during the write. 如您所见,在写入期间第69行出现超时错误。 I need this to run close enough to the C one so that the writes and reads work. 我需要它足够接近C语言运行,以便进行写和读工作。
Obviously the baud rate, wordlength, parity and several others are set correctly. 显然,正确设置了波特率,字长,奇偶校验等。
# 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:
PortMon输出\\ n的句点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.