Go Down

Topic: Problem with serial buffers (Read 936 times) previous topic - next topic

RompiendoHuesos

Hi everyone!

I've got a Arduino Mega 2560 and i'm trying to communicate with it with the usb connector.

I tryed the ASCII recognition example who writes the info about the character sends by the computer. When I try to send bytes by serial monitor everything works ok.

My problem is when i send continuous bytes with a program (in this case i did some tests with robotic developement evironments Aria and Player/Stage) the send and receive bytes seems not working correctly. When i send bytes from the computer to the board, the board stops to send bytes to the computer and the buffer seems to been overwritten.

Any idea how to solve that? I check that the baudrate it's ok and test under different SO (ubuntu 12.04, ubuntu 11.04, windows xp and windows 7) with the same results.

Kind regards.

PaulS

Quote
My problem is when i send continuous bytes with a program (in this case i did some tests with robotic developement evironments Aria and Player/Stage) the send and receive bytes seems not working correctly. When i send bytes from the computer to the board, the board stops to send bytes to the computer and the buffer seems to been overwritten.

Any idea how to solve that?

Handshaking. The sender should send only when the receiver (the Arduino) says that it is ready.

RompiendoHuesos

Thanks for the reply.

How can i do that? I don't control the computer program (Aria or Player are commercial ones) and are sending bytes repeteadly. I tried to manage that bytes with a if (Serial.available) structure and writes the replys in that structure but the program doesn't waits to process all the structure when multiple bytes are coming from the computer and some of the replys from the board to the computer are overwritten. I tried to do Serial.flush after serial.write or serial.print but doesn't seems to correct the problem.

Kind regards.

PeterH


I don't control the computer program (Aria or Player are commercial ones) and are sending bytes repeteadly.


Are they sending messages at the full line speed, or some constant rate? (What is being sent in these messages?) It would be pretty unusual for a data source to simply flood the serial stream without providing any flow control mechanism or rate limiting.

How much data is the Arduino sending back over the serial port in response? I mean, does it send one byte back for every byte it receives, or does it send back more, or less?
I only provide help via the forum - please do not contact me for private consultancy.

RompiendoHuesos

I control the baudrate that uses Aria/Player and the board. I tried at 9600, 4800, 2400 bauds with the same results. The program sends a message (usually about 60-80 bytes) everytime it needs new information (about 100 ms usually).

I try to send only one byte when i read one byte. The flow from the boards seems to stop when Aria/Player sends a new message and restart few bytes later (losing a number of bytes that vary). I check that with a serial monitor provides by linux instead of Arduino one (cutecom) obtain the same results.

Kind Regards.

Nico V

Hmm, 60-80 bytes. The serial buffer is only 64 bytes so I wonder if that has anything to do with it.
You could try changing the SERIAL_BUFFER_SIZE #define in HardwareSerial.cpp.

bperrybap


Hmm, 60-80 bytes. The serial buffer is only 64 bytes so I wonder if that has anything to do with it.
You could try changing the SERIAL_BUFFER_SIZE #define in HardwareSerial.cpp.

But don't go larger than 256 or you will have issues because the current HardwareSerial code will break
if the head/tail pointers are larger than 8 bits because the code doesn't properly deal
with the atomicity issues involved with updating multi-byte values.


--- bill

RompiendoHuesos

Thanks for the replys.

I tried to change the size of buffers to the maximum (256 bytes) and it's going better... but the problem still appears eventually. When i send bytes from the computer to the board the sending from the board to the computer eventually freezes. Any other idea of how to solve it?

Kind Regards.

PaulS

Quote
Any other idea of how to solve it?

Let me see if I can find my crystal ball. I'll be back in a couple of days.

You really could speed up that time if you posted your code.

RompiendoHuesos

Ok, i started with the ASCII Characters example comes with the program:

Code: [Select]
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // send an intro:
  Serial.println("send any byte and I'll tell you everything I can about it");
  Serial.println();
}

void loop() {
  // get any incoming bytes:
  if (Serial.available() > 0) {
    int thisChar = Serial.read();

    // say what was sent:
    Serial.print("You sent me: \'");
    Serial.write(thisChar);
    Serial.print("\'  ASCII Value: ");
    Serial.println(thisChar);

    // analyze what was sent:
    if(isAlphaNumeric(thisChar)) {
      Serial.println("it's alphanumeric");
    }
    if(isAlpha(thisChar)) {
      Serial.println("it's alphabetic");
    }
    if(isAscii(thisChar)) {
      Serial.println("it's ASCII");
    }
    if(isWhitespace(thisChar)) {
      Serial.println("it's whitespace");
    }
    if(isControl(thisChar)) {
      Serial.println("it's a control character");
    }
    if(isDigit(thisChar)) {
      Serial.println("it's a numeric digit");
    }
    if(isGraph(thisChar)) {
      Serial.println("it's a printable character that's not whitespace");
    }
    if(isLowerCase(thisChar)) {
      Serial.println("it's lower case");
    }
    if(isPrintable(thisChar)) {
      Serial.println("it's printable");
    }
    if(isPunct(thisChar)) {
      Serial.println("it's punctuation");
    }
    if(isSpace(thisChar)) {
      Serial.println("it's a space character");
    }
    if(isUpperCase(thisChar)) {
      Serial.println("it's upper case");
    }
    if (isHexadecimalDigit(thisChar)) {
      Serial.println("it's a valid hexadecimaldigit (i.e. 0 - 9, a - F, or A - F)");
    }

    // add some space and ask for another byte:
    Serial.println();
    Serial.println("Give me another byte:");
    Serial.println();
  }
}


I tryed to modify it to write only one byte instead of the string of characters of this program, but the result it's the same.

Kind Regards

PaulS

Quote
but the result it's the same.

The same as what?

RompiendoHuesos

An example of the result from serial monitor when everything goes OK:

Code: [Select]
You sent me: ''  ASCII Value: 3
it's ASCII
it's a control character

Give me another byte:

You sent me: ''  ASCII Value: 0
it's ASCII
it's a control character

Give me another byte:

You sent me: '


But when Aria/Player is working i obtain results like that:

Code: [Select]
send any byte and I'll tell you everything I can about it

You sent me: 'W'  ASCII Value: 87
it's alphanumeric
it's alphit'aracsctnothme: 'M'  ASCII Value: 77
it's alphanumeric
it's alphabetic
aciæ???~fàà


As you can see, the second byte analisis starts before the first byte analisis ended.

PaulS

It looks to me like you are talking to Aria/Player on the same serial port that you are trying to talk to the Serial Monitor on. That isn't going to work.

Go Up