Hi.
I´m developing my own robot board control using the ATMEGA32 microcontroler and a software developed in C# to send the commands for the controller via serial (RS232).
I used hyperterminal to test the controller and it´s working OK.
The method that i´m using to sync the data that i have to send to the controller, and then wait the complete move to send the next data is: first i send a command string “start”, the controller sends back “OK”, then i send a position, the controller sends another “OK” and so on until the last command, then the controller do the move and after it is completed it sends back the last “OK”, then i know that i can send the next step of data.
The problem is that when i use C# to send and receive the commands, the event that should happen when the first “OK” is received, only happen when the second “OK” is received, then the remaining data to be sent became unsync.
I checked the SerialPort.ReceivedBytesThreshold Property and set it to 1, so after the first byte received the event should occur, but it´s not working properly.
It seems that i´m having some king of delay in the C# response.
Someone can help me on how to handle the data received in the serial buffer, and what i have to do, to do a loop to wait the right response from the controller before send the next data?
Thanks for now!
Are you using interrupt service routines for your receive? These routines “grab” the characters received, and put them in a buffer for you. Then when the buffer has a response line (or perhaps just a char), it sets a flag that tells the foreground task that a message has been received. the foreground task then acts upon it.
Alan KM6VV
Yes, i think that this lines sould call the event that handle the data received everytime that a byte arrives.
serialPort1.DataReceived += new
System.IO.Ports.SerialDataReceivedEventHandler(Recebe);
private void Recebe(Object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
recebido += serialPort1.ReadExisting();
}
So, this should be a background task that occurs independent of the foreground task.
When i push the “play” button on my program, i have a do-while loop to send all the data of the sequence until it is finished.
So i think that the play button is my foreground task and it depends of the data received in the background task to complete the sequence.
I receive the data, but it´s unsync with the data sent, maybe the background task is not occuring when i need it to occur, i need that it occurs immediately after the data arrives.
two things come to mind.
the while loop sending sequence data faster than the ATMEGA can process or ATMEAGA sending back faster than events can happen.
Serial message sync can be tricky. i would try (in while loop) send then block on read to wait for answer. You can also try test with ATMEA sending periodicly an incrementing count just to prove you recieve them all.
How are you framing the messages? You might be getting the event but by the timethe event code happens both OKs have been recieved.