SerialPort DataReceived event
-
I'm working on the 3 motor drivers that has their own ComPort on the computer. (yes 3 Serial Ports on the back of computer). In the past, I've been using the do-while loops to do the Serial.Readline() to monitor the driver's response to the command. That really slows the processing and usually hangs the application very often. I've modified the code to add the DataReceived event for each port. I've been sending lots of drivers' configuration (and gets the response back from the driver as echo as well.) After sending all of the configuration, the other process begins. The problem is that the drivers didn't finish sending back all of the responses before the new process begin. This caused the new process commands to be messup with communication. Question is... how do I check if the configuration responses are completed before running the next process? Your help would be greatly appreciated. Thanks!
-
I'm working on the 3 motor drivers that has their own ComPort on the computer. (yes 3 Serial Ports on the back of computer). In the past, I've been using the do-while loops to do the Serial.Readline() to monitor the driver's response to the command. That really slows the processing and usually hangs the application very often. I've modified the code to add the DataReceived event for each port. I've been sending lots of drivers' configuration (and gets the response back from the driver as echo as well.) After sending all of the configuration, the other process begins. The problem is that the drivers didn't finish sending back all of the responses before the new process begin. This caused the new process commands to be messup with communication. Question is... how do I check if the configuration responses are completed before running the next process? Your help would be greatly appreciated. Thanks!
There are a couple of ways to handle this, but it depends on how you want to work it. The simplest solution is to set up a thread to handle each com port - that way they can use ReadLine all they want, and you don't have to worry about them hanging the UI thread or about data synchronisation. You then have to communicate between the UI and the threads - and I have no idea how complex that is for you because I don't know what you app is actually doing. The other is to use the DataRecieved Event in the UI thread as you seem to be doing - but that has to be handled differently, because the data is not received in the form of lines - it arrives character by character (or a couple of characters at a time if the processor is busy) so you have to buffer and examine the data a bit more carefully. Exactly which way you should go, I don't know - but without seeing any relevant code fragments, I can't tell what your code is doing at the moment, so I can't make any concrete suggestions.
If you get an email telling you that you can catch Swine Flu from tinned pork then just delete it. It's Spam.