写入串行端口(C#)的奇怪问题...运行另一个程序后有效

发布于 2024-07-29 08:08:52 字数 6968 浏览 7 评论 0原文

我正在尝试使用 C# 中的 System.IO.Ports 和 SerialPort 类写入 LCD 显示器。

首先,它有效! 我的程序确实成功写入它并且显示良好......但只有在我运行了先前的商业程序(LCDC)之后。

如果我在运行 LCDC(一个完整的 LCD 显示 GUI 程序)之前运行程序 - LCD 显示屏一片空白,甚至没有闪烁。

如果我在 LCDC 上“开始显示”(即使其显示 LCDC 目录中的单个屏幕),然后“停止显示”并完全关闭 LCDC; 然后运行我的程序,它工作得很好。

因此,LCDC 似乎正在激活或初始化 COM/串行端口或其他端口,然后该端口会在程序关闭后记住该端口。 有人有什么主意吗?

我已经尝试了无数使用 SerialPort 的代码示例(来自各种在线资源),它们都有相同的问题 - 在 LCDC 之前没有任何问题,但之后具有完整的功能。

如果您需要,我可以发布代码,但这是标准: 声明 SerialPort 端口

port.Open( x, yz); 端口.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 完成它的业务之后!

I'm trying to write to an LCD Display using System.IO.Ports in C#, with the SerialPort class.

Firstly, it works! My program does write to it successfully and it displays fine... but only after I've run a prior commerical program (LCDC).

If I run by program before running LCDC (a full LCD-display GUI program) - the LCD display sits there blankly, not even a flicker.

If I 'Start Display' on LCDC (i.e. make it display even a single screen from LCDC's catalogue) and then 'Stop Display' and shut down LCDC completely; and THEN run my program it works absolutely fine.

So it seems LCDC is activating or initialising the COM/serial port or something, that the port then remembers after the program has shut down. Does anyone have any idea?

I have tried countless code samples using SerialPort (from various online resources) and they all have the same problem - nothing before LCDC, but full functionality afterwards.

I can post code if you need but it's the standard:
declare SerialPort port

port.Open( x, y z);
port.Write("string");

etc. and it works fine after LCDC is run!

As an update, I'm still stuck! I used PortMon to analyse the port, firstly for my program running alone:

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    

Then for 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: ...   

Any ideas?

I noticed the LCDC did an IOCTL_SERIAL_PURGE right before writing, but my program does this on disconnection - and then it still doesn't work after reconnecting (so I'm assuming that it's not to do with this purge).

If I run my program again after LCDC, it reads exactly the same except for:

26 0.00294954 MatOrbOfficialT IOCTL_SERIAL_SET_HANDFLOW VCP0 SUCCESS Shake:0 Replace:0 XonLimit:1024 XoffLimit:1024

Where is says Replace:0 used to read Replace:40, is this significant?

As far as I can tell... all other values set by LCDC are reset by my program after I run it, so surely it should make no difference that some values are different? Given that my program does write successfully... Only after LCDC has done it's business!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故事↓在人 2024-08-05 08:08:52

我首先认为这不是在计算机上的串行端口上进行配置的内容,而是在开始发送数据之前 LCD 需要进行的一些初始化。

我建议您在串行端口上运行跟踪程序,以查看其他应用程序在发送要显示的第一行文本之前向 LCD 发送的内容。

您可以尝试 Sysinternals(现已成为 Microsoft 的一部分)的 Portmon

I would think first that is not something that is getting configured on the serial port on your computer, but rather some initialization that the LCD requires before you start sending data.

I would recommend you run a tracer on the serial port to see what the other application is sending to the LCD before it sends the first line of text to display.

You could try Portmon from Sysinternals (now part of Microsoft).

盗心人 2024-08-05 08:08:52

确保您的握手符合另一端的期望。 尝试将 DTR 和 RTS 设置为 true。

sPort.Dtr = true;
sPort.Rts = true;

Make sure your handshake matches what the other end is expecting. Try setting DTR and RTS to true.

sPort.Dtr = true;
sPort.Rts = true;
烟柳画桥 2024-08-05 08:08:52

我最近在串行端口通信方面遇到了几乎相同的问题。 我尝试连接到我的设备,但通信无法正常工作。 然后我会打开一个串行端口监控程序,看看我是否确实收到任何数据传输,一切看起来都很好。 最后,我再次尝试连接,就可以正常通信了。

我的情况是,我没有在程序中设置波特率、读/写超时和数据特征(数据位、停止位、奇偶校验位)。 调试程序会自动执行此操作,并且这些更改似乎“粘在”串行端口上,这就是为什么我的程序在运行调试程序一次后就能工作。

确保您已在您的程序中正确设置所有这些设置。

I went through an almost identical issue recently with serial port communications. I would try connecting to my device, and no communications would work properly. Then I would open up a serial port monitoring program, to see if I was indeed getting any data transfer, and everything would look okay. Finally, I would try connecting again and I would be able to communicate properly.

What was happening in my case was that I was not setting up the baud rate, read/write timeouts, and data characteristics (data bits, stop bits, parity bits) in my program. The debug program did that automatically, and those changes seemed to "stick with" the serial port, which was why my program would work after running the debug program once.

Make sure you've set all of those settings properly in your program.

国产ˉ祖宗 2024-08-05 08:08:52

我在连接电机时遇到了类似的问题,首先运行另一个应用程序导致我的应用程序工作。

问题最终是写入和读取时串行端口上的噪音。 发送测试/垃圾消息并清除输入和输出缓冲区最终解决了问题。 就我而言,另一个应用程序只是清除缓冲区中的垃圾。

I've had similar issues with connecting to a motor, where running another application first caused my application to work.

The issue ended up being noise on the serial port, both when writing and reading. Sending test/junk messages, and clearing the Input and Output buffers eventually cleared the issue up. In my case, the other application was just clearing the junk out of the buffers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文