Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 8 months ago.
serial communication in lpc1768
Hi, I am trying to do the serial communication between two microcontrollers (LPC1768 and K64F). I successfully trasmit the data between the two mbed boards. I came across a very strange issue. If I try to receive a serial data on a web server based microcontroller it receives upto 16 characters, it just ignores the other characters even length of string is coming upto 16, though i am sending 19 characters.
If I try to receive these characters in normal application(without web server) it works fine.
Can any one tell me what kind of issue it is. following is the piece of code i am using to receive serially data:
if (rfid.readable()){ while (rfid.readable()){ char c=rfid.getc(); msgbuffer+=c; printf("%s \n",msgbuffer); } msgbuffer.clear();
2 Answers
10 years, 8 months ago.
The LPC1768 has a 16-byte hardware FIFO, so it might be related to that.
One thing is already that for every byte you receive, you at the same time send a whole bunch of characters to your PC. Unless that one has a much higher baudrate it will have to wait until that one is done sending before it can process a char again.
10 years, 8 months ago.
Best guess is that you aren't reading the serial port fast enough.
The serial port hardware can internally buffer up to 16 bytes. If you send it 16 bytes or less it doesn't matter when you read the data from the port, you'll get it all. If you send it 17 bytes then the serial port won't have anywhere to store the last byte and it'll get lost. The only way to avoid this is to read the first byte from the port before byte 17 arrives.
If all your software is doing is sitting waiting for data to arrive this is easy. If it's also running a web server then the CPU could be off doing something else when the data arrives and not get around to checking the serial data in time.
The solution is to use the receive interrupt on the serial port to tell you that you need to do something now. All the interrupt needs to do is read the data from the serial port, copy it into a buffer in memory and set a count of how much data is waiting. Avoid doing anything complex in the interrupt if at all possible, certainly no printf's, they are slow..
Your main loop code then checks the count of bytes waiting and if there is enough for a full message it does something about it.
There are a couple of things to watch out for in doing this:
Make sure any variables set in the interrupt and checked in the main loop (e.g. the data in count) are declared as volatile.
Make sure that the interrupt reads all waiting data in the serial port, not just the first byte.
Make sure that you can cope with data arriving just when the main code is looking at the buffer otherwise you can get some very hard to track down bugs. The simple solution to this is if there is data waiting the main loop to disables interrupts, makes a copy of the buffer, sets the buffer count back to zero and then re-enables interrupts. It then works with the copy it made. This way interrupts are disabled for the minimum length of time but the data you are processing never changes while you are working on it. The down side is that you now have two buffers rather than one.