我正在尝试使用 C# 中的 System.IO.Ports 和 SerialPort 类写入 LCD 显示器。
首先,它有效!我的程序确实成功写入它并且它显示正常......但只有在我运行了之前的商业程序(LCDC)之后。
如果我在运行 LCDC(一个完整的 LCD 显示 GUI 程序)之前按程序运行 - LCD 显示屏空白地坐在那里,甚至没有闪烁。
如果我在 LCDC 上“开始显示”(即让它显示 LCDC 目录中的单个屏幕),然后“停止显示”并完全关闭 LCDC;然后运行我的程序,它工作得很好。
因此,LCDC 似乎正在激活或初始化 COM/串行端口或其他东西,该端口在程序关闭后会记住。有人有什么主意吗?
我使用 SerialPort(来自各种在线资源)尝试了无数代码示例,它们都有相同的问题 - LCDC 之前没有,但之后功能完整。
如果您需要,我可以发布代码,但这是标准:声明 SerialPort 端口
端口.打开(x,yz);port.Write("字符串");
等等,LCDC运行后它工作正常!
作为更新,我仍然卡住了!我使用 PortMon 来分析端口,首先是我的程序单独运行:
0 0.04459980 MatOrbOfficialT IRP_MJ_CREATE VCP0 SUCCESS Options: Open
1 0.00000531 MatOrbOfficialT IOCTL_SERIAL_GET_PROPERTIES VCP0 SUCCESS
2 0.00000335 MatOrbOfficialT IOCTL_SERIAL_GET_MODEMSTATUS VCP0 SUCCESS
3 0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS
4 0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS
5 0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS
6 0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS
7 0.00000307 MatOrbOfficialT IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS
8 0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS
9 0.00000196 MatOrbOfficialT IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS
10 0.00000196 MatOrbOfficialT IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS
11 0.00216620 MatOrbOfficialT IOCTL_SERIAL_SET_BAUD_RATE VCP0 SUCCESS Rate: 19200
12 0.00295484 MatOrbOfficialT IOCTL_SERIAL_SET_RTS VCP0 SUCCESS
13 0.00297244 MatOrbOfficialT IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS
14 0.00297496 MatOrbOfficialT IOCTL_SERIAL_SET_LINE_CONTROL VCP0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
15 0.00000251 MatOrbOfficialT IOCTL_SERIAL_SET_CHAR VCP0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
16 0.00501293 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:40 XonLimit:1024 XoffLimit:1024
17 0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS
18 0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS
19 0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS
20 0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS
21 0.00282829 MatOrbOfficialT IOCTL_SERIAL_SET_BAUD_RATE VCP0 SUCCESS Rate: 19200
22 0.00293892 MatOrbOfficialT IOCTL_SERIAL_SET_RTS VCP0 SUCCESS
23 0.00296909 MatOrbOfficialT IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS
24 0.00297719 MatOrbOfficialT IOCTL_SERIAL_SET_LINE_CONTROL VCP0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
25 0.00000279 MatOrbOfficialT IOCTL_SERIAL_SET_CHAR VCP0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
26 0.00294954 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:40 XonLimit:1024 XoffLimit:1024
27 0.00288919 MatOrbOfficialT IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS
28 0.00000447 MatOrbOfficialT IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS
29 0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS
30 0.00000251 MatOrbOfficialT IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS
31 0.00000223 MatOrbOfficialT IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS
32 0.00285763 MatOrbOfficialT IOCTL_SERIAL_SET_BAUD_RATE VCP0 SUCCESS Rate: 19200
33 0.00296406 MatOrbOfficialT IOCTL_SERIAL_CLR_RTS VCP0 SUCCESS
34 0.00298753 MatOrbOfficialT IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS
35 0.00292132 MatOrbOfficialT IOCTL_SERIAL_SET_LINE_CONTROL VCP0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
36 0.00000559 MatOrbOfficialT IOCTL_SERIAL_SET_CHAR VCP0 SUCCESS EOF:1a ERR:0 BRK:0 EVT:1a XON:11 XOFF:13
37 0.00491962 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:0 XonLimit:1024 XoffLimit:1024
38 0.00292942 MatOrbOfficialT IOCTL_SERIAL_CLR_RTS VCP0 SUCCESS
39 0.00000475 MatOrbOfficialT IOCTL_SERIAL_SET_TIMEOUTS VCP0 SUCCESS RI:-1 RM:-1 RC:-2 WM:0 WC:0
40 0.00245674 MatOrbOfficialT IOCTL_SERIAL_SET_WAIT_MASK VCP0 SUCCESS Mask: RXCHAR RXFLAG CTS DSR RLSD BRK ERR RING
41 0.00000670 MatOrbOfficialT IOCTL_SERIAL_SET_QUEUE_SIZE VCP0 SUCCESS InSize: 4096 OutSize: 2048
42 0.00000000 MatOrbOfficialT IOCTL_SERIAL_WAIT_ON_MASK VCP0
43 0.00049811 MatOrbOfficialT IRP_MJ_WRITE VCP0 SUCCESS Length 2: .X
然后对于 LCDC:
0 0.05153979 LCDC.exe IRP_MJ_CREATE VCP0 SUCCESS Options: Open
1 0.00000503 LCDC.exe IOCTL_SERIAL_SET_QUEUE_SIZE VCP0 SUCCESS InSize: 1024 OutSize: 1024
2 0.00000251 LCDC.exe IOCTL_SERIAL_GET_BAUD_RATE VCP0 SUCCESS
3 0.00000279 LCDC.exe IOCTL_SERIAL_GET_LINE_CONTROL VCP0 SUCCESS
4 0.00000223 LCDC.exe IOCTL_SERIAL_GET_CHARS VCP0 SUCCESS
5 0.00000251 LCDC.exe IOCTL_SERIAL_GET_HANDFLOW VCP0 SUCCESS
6 0.00274029 LCDC.exe IOCTL_SERIAL_SET_BAUD_RATE VCP0 SUCCESS Rate: 19200
7 0.00299563 LCDC.exe IOCTL_SERIAL_CLR_RTS VCP0 SUCCESS
8 0.00295205 LCDC.exe IOCTL_SERIAL_CLR_DTR VCP0 SUCCESS
9 0.00289757 LCDC.exe IOCTL_SERIAL_SET_LINE_CONTROL VCP0 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
10 0.00000531 LCDC.exe IOCTL_SERIAL_SET_CHAR VCP0 SUCCESS EOF:b0 ERR:1 BRK:1 EVT:0 XON:79 XOFF:13
11 0.00296155 LCDC.exe IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:0 XonLimit:256 XoffLimit:256
12 0.00000307 LCDC.exe IOCTL_SERIAL_SET_TIMEOUTS VCP0 SUCCESS RI:-1 RM:0 RC:0 WM:100 WC:1000
13 0.00251401 LCDC.exe IOCTL_SERIAL_SET_WAIT_MASK VCP0 SUCCESS Mask: RXCHAR ERR
14 0.00000335 LCDC.exe IOCTL_SERIAL_GET_MODEMSTATUS VCP0 SUCCESS
15 0.00000251 LCDC.exe IOCTL_SERIAL_GET_MODEMSTATUS VCP0 SUCCESS
16 0.00000251 LCDC.exe IOCTL_SERIAL_GET_MODEMSTATUS VCP0 SUCCESS
17 0.00000391 LCDC.exe IOCTL_SERIAL_PURGE VCP0 SUCCESS Purge: TXCLEAR RXCLEAR
18 0.00066545 LCDC.exe IRP_MJ_WRITE VCP0 SUCCESS Length 3: .P.
19 3.94750981 LCDC.exe IOCTL_SERIAL_WAIT_ON_MASK VCP0 SUCCESS
20 0.00091632 LCDC.exe IRP_MJ_WRITE VCP0 SUCCESS Length 3: ...
有任何想法吗?
我注意到 LCDC 在写入之前执行了 IOCTL_SERIAL_PURGE,但是我的程序在断开连接时执行此操作 - 然后在重新连接后它仍然无法工作(所以我假设它与此清除无关)。
如果我在 LCDC 之后再次运行我的程序,它的读数完全相同,除了:
26 0.00294954 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:0 XonLimit:1024 XoffLimit:1024
Replace:0 用于读取 Replace:40 的位置在哪里,这很重要吗?
据我所知...... LCDC 设置的所有其他值在我运行它后都由我的程序重置,所以肯定有些值不同应该没有区别吗?鉴于我的程序确实编写成功......只有在 LCDC 完成它的业务之后!