Serial Port Interrupts

Ok. I’m using Kevin Watson’s serial port code. It has a few simple, easy to use functions. Three main functions for each serial port. I will only be talking about the second serial port (TTL). Theres one function for initilizing the serial port. This comes implemented already. Then theres a function for determining how many bytes are in the serail port’s buffer. Finally theres a function for reading a single byte from that buffer.

I built a large segment of code for communicating with the AVR cam. It reads data off the serial port’s buffer until it hits the finish/escape character or it reads all the data in the buffer. When it recieves a complete packet (It gets the \r or 0xFF char) it will send the packet off to another peice of code I built to process the packet.

I’ve had it explained that the serial port code works by interupts. (Meaning everytime some bytes come through the serial port it stops everything to add that code to a buffer, then returns to w/e it was doing before) Now the problem with that, is whille my buffer is reading, it interrupts to add more information. I didn’t build my system to be able to handle the buffer changing. For example, I built a safe guard in the begining that stops the overflow of my local buffer. If the bytes in the serial port’s buffer surpass the amount of bytes my array can hold, it stops the read process. However if after that it adds a whole bunch to the buffer then theres a chance of my local array/buffer overflowing.

Thats just one of a few dozen different errors that can happen if its updating its buffer as I’m trying to read from it.

I have onle thought of one solution, however since I haven’t looked through the Serial Port code I don’t know if its fesable. I’m thinking if I set a flag at the begining of my codes and unset it at the end, then I’ll add a check for that flag during the interrupt. If that flag is set during the interrupt then it’ll pass it to a different buffer. During the first write which that flag isn’t set it’ll write everything from the temp. buffer to the start of the actual buffer.

What does everyone else think? This problem is very aggravating.

You need to treat the data from/to the serial port as a stream. Construct your code to process it one byte at a time, empty or not. Then you must make sure you read often enough that it is not possible to miss data. If you can’t read it fast enough, slow the port down.

So look at the incoming stream, find the SOM char, buffer everything through the EOM char and finally pass the buffer to a function to handle the data. Check for overflows/underflows and invalid sequences while processing the stream. If you get any errors, toss the data and look for the next SOM char.

Check out Kevin’s camera code for a good example.