I’m having issues with the Netduino Serialport Datarecieved threads not completing execution. I have 3 serial ports set up to communicate with 3 devices. Basically when data is received it execute a method in its native class called a method in a different class associated with the port I want to relay data to then calls a universal sender method in a third class to send to the ladder device. What I’m finding is that while debugging the code seems to execute fine but no data is sent via the port while stepping though. Below is a much siplified version of the code (which may be my issue). I may try containing the final code to send the data in the same class, the issue is that ACK needs to happen from the port in the other class and relaying that information become problematic. Has anyone seen this issue?
private static void DataReceieve(object sender, SerialDataReceivedEventArgs e)
readbytes = new byte[port.BytesToRead];
port.Read(readbytes, 0, readbytes.Length);
case 0xB7: F340.CaptureImage(); break;
public static void CaptureImage()
public static void CaptureImage()
public static void CaptureImage(int numberOfPackages)
public static void senderService(SerialPort port, byte writebytes)
port.Write(writebytes, 0, writebytes.Length);
I had the same issues you are having. I solved the issues using timers and polling the port. Example:
Private Shared PollStatusThread As Thread = New Thread(AddressOf PLM.PollStatus
Then use a while loop in PollStatatus with a small delay (Thread.Sleep(500)) to get the data .
That is great idea. After knocking it around in my head i found a solution that seems to be working. As you can see i basically set up a code system for the first byte that is send which tells the program what method to run. I set up the while loop for all the ports and codes in the main method which is spinning in a while loop set to true to keep the program running forever. When the port gets the byte it sets a byte in the class holding the main method. This byte is the argument of the while loop looking for the codes. This lets the new thread after data received do one quick thing then close and free up resources. It then lets the main thread handle all the hand work. This takes away simultaneous execution of multiple commands are received but in my case that shouldn’t be a big issue. I think I’ll give your option a try as well.
Here is a link to some serial code that I think is very well written it uses three timers.
What is it polling for exactly, data received event or data in the input buffer?
When I use “polling” I just mean that I check for some change to occur in a while loop. In the example below I am polling the input port for data , ACK or NAK. If it takes more that 2 seconds I leave the loop (must be something wrong).
If Port.BytesToRead > 0 Then
Port.Read(buffer, 0, buffer.Length)
str += BytesToHexString(buffer)
If CM.InString(str, ACK) Then Exit While
If CM.InString(str, NAK) Then Exit While
i += 1
If i > 20 Then Exit While
That’s pretty much what i did. I run the while loop in the main method and “poll” a byte that is updated by the data received events from the other classes. In my cause if they do not change byte then i just miss the command and it must be sent again. I may implement a more immediate ACK to make sure they send again if needed. Thanks for the help!