用 C# 与科勒仪表通信
我正在尝试与科勒有源仪表进行通信。它是半双工的。我已阅读所有协议。科勒有自己的软件来查看配置文件。当它阅读配置文件时,我试图查看它是如何发生的。发送和接收数据。然后我尝试用C#编写一个程序。第一步通信波特率选择 300,然后应该是 4800。
这是我的问题:
如果我尝试使用 F5 调试我的程序,它无法通信。但如果我使用单步进入模式 F11,它可以工作吗?我想应该要等一段时间。有什么想法吗?
当我尝试将波特率从 300 更改为 4800 时,进入模式 F11 不起作用。我检查了软件是否有变化,但没问题。知道这是怎么回事吗?
I m trying to communicate with Kohler active meter. It s half duplex. I ve read all protocol. Kohler has own software to see the profiles. While it was reading the profiles, i tried to view how it is happening. Sending and recieving datas. Then i tried to write a programme in C#. To communicate in first steps baud-rate is chosen 300, and then it should be 4800.
Here is my problems:
If i try to debug my programme using F5, it is not communicating. But if i use step into mode F11, it is working? i guess it should wait some amount of time. any idea?
When i try to change baud-rate from 300 to 4800, step into mode F11 is not working. I checked in the software if it is changing or not, but it is fine. any idea what this is about?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是使用串行端口时的常见问题。它们是非常慢的设备,尤其是在 300 波特率下。每 33 毫秒仅显示一个字符,这对于现代机器来说是一个永恒的时间。通过使用调试器,您人为地减慢了程序的速度。很多。为串行端口驱动程序提供时间来接收完整的设备响应。当您在没有单步执行或断点的情况下运行时,SerialPort.Read() 调用一次仅返回一个或几个字符。您可以通过使用带有正确设置的 NewLine 属性的 ReadLine() 或将接收到的字节存储在缓冲区中直到获得完整响应来修复此问题。
您不能只更改波特率,它必须始终与设备的速率匹配。
This is a common mishap when you work with serial ports. They are very slow devices, especially at 300 baud. That's only one character per 33 milliseconds, an eternity on a modern machine. By using the debugger, you artificially slow down your program. A lot. Giving the serial port driver time to receive the full device response. When you run without single-stepping or breakpoints, the SerialPort.Read() call only returns one or a few characters at a time. You fix this by using ReadLine() with a properly set NewLine property or by storing the received bytes in a buffer until you got the full response.
You can't just change the baudrate, it must always match the rate of the device.
在发送您的请求之前,请等待一段时间。当您使用 F5 进行调试时,您无需等待。但使用 F11 时,您不可避免地要等待。这也表明,当您等待时,您的程序会更好地工作:)使用“免费串行端口监视器”等监控程序,首先运行科勒自己的程序来找出这些等待时间。
您可以使用流读取器和流编写器与仪表进行通信。
就像
在用户写入命令后不要忘记使用 writer.Flush() 命令。
有时要在向仪表发送命令之前等待,请使用 System.Threading.Thread.Sleep(250); code 。 250 的单位是毫秒。
我不建议您在发送请求后等待。在发送新请求、ACK 消息等之前先等待。
如果科勒自己的程序在命令设置串行端口超时值之间等待太多......
Before sending your requests wait for some time . When you debug with F5 you don't wait. But with F11 you wait unavoidablely. This also shows that your program works better when you wait :) Use a monitoring program like 'Free Serial Port Monitor' to find out these waiting times by first running Kohler's own program.
You can use a streamreader and streamwriter to communicate with the meter.
Like
Don't forget to use writer.Flush() command just after you user write command.
To wait sometimes before you send your commands to the meter use
System.Threading.Thread.Sleep(250);
code . 250 is in msecs.I don't recommend you to wait after sending the request. Wait before you send the new request, ACK message, etc.
If kohler's own program waits too much between commands set serialports timeout values ...
“免费串口监视器”这个程序确实打开了我的思路。非常感谢@ferda。我解决了这个问题。这是关于等待时间的问题。首先我看到了使用该程序的请求时间和答案。然后我还使用了 Flush 命令。我再次使用 FreeSerial Port Monitor 程序观看了我的程序输出。最后我可以以 4800 波特率发送数据和接收数据。对于科勒来说,只需更改 C# 中的波特率即可。它不需要任何其他东西。
'Free Serial Port Monitor' this programme has really opened my mind. Thank you so much @ferda. and i solved the problem. it was about waiting times. First i saw the times for request and the answers using that programme. Then i also used Flush command. i watched my programme outputs using again FreeSerial Port Monitor programme. At the end i could send the datas and received datas at 4800 baud-rate. Just changing baud-rate in C# worked for Kohler. It doesn't need anything else.