Communicate with Kohler Meter in C# [closed]
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, an开发者_运维技巧d 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?
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.
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
writer = new StreamWriter(((System.IO.Ports.SerialPort)serialPort).BaseStream);
reader = new StreamReader(((System.IO.Ports.SerialPort)serialPort).BaseStream);
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 ...
(serialport).ReadTimeout = xxx;
(serialport).WriteTimeout = xxx;
'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.
精彩评论