Go Down

Topic: send to arduino, using pyserial,string length 336 characters (Read 365 times) previous topic - next topic


The question is this, i need to send to arduino a mapp.
Now the map have this format:

Code: [Select]

Problem, i know that arduino have a limited memory, right? So arduino manage only 140 character of the 336 in my message.

Anyone here have a issues to avoid this problem?

I have already try, and i'm in trouble, to cut the string in subsets of strings, but seems to be not stable for me.

If anyone have tried out something, i'm in search of solution.

Thanks guis


The serial input basics tutorial may help you.  You could set the numChars constant to the number of characters that you want to receive.  That would bypass the receive buffer which has a limited size.

Code: [Select]
const byte numChars = 340;
char receivedChars[numChars];   // an array to store the received data


If anyone have tried out something, i'm in search of solution.
But, you failed to post your code, so you really aren't look to us for the solution, obviously.
The art of getting good answers lies in asking good questions.


thanks groundFungus and sorry PaulS.
So before posting again i have get a look into serial input basic tutorial, as suggested.
But seems that problem remain the same.
Now i will post the part of the code responsable to broke my plans:
Note, this is not the full code, but it is only the serialEvent and relative


Code: [Select]


const byte numChars = 400;
char receivedChars[numChars];

String codes[] = {

//SERIAL EVENT///////////////////////////////////////////////////////////////

void serialEvent() {
  if (not Serial.available()) {


  int command;
  inString = Serial.readString();

  Serial.print("instring ");
  Serial.println(receivedChars[0, -1]); //prima 0 ultima -1
  String code = inString.substring(0, 5);
  String info = inString.substring(5, -1);
  int pos = inString.substring(5, -1).toInt();
  for (int i = 0; i < 100; i++) {
    if (codes[i] == code) {
      Serial.print(" : ");
      command = i;
  switch (command) {
    case 1:
      moveToPos(1, pos);
    case 2:
      moveToPos(2, pos);
    case 3:
      moveToPos(3, pos);
    case 4:
      moveToPos(4, pos);
    case 5:
    case 6:
    case 7:
    case 8:
    case 9:
    case 12:
    case 15:
    case 16:
    case 17:
    case 18:
    case 19:
    case 20:
    case 31:
      setEngineSpeed(1, pos);
    case 32:
      setEngineSpeed(2, pos);
    case 33:
      setEngineSpeed(3, pos);
    case 34:
      setEngineSpeed(4, pos);
    case 35:
      setEngineAccel(1, pos);
    case 36:
      setEngineAccel(2, pos);
    case 37:
      setEngineAccel(3, pos);
    case 38:
      setEngineAccel(4, pos);
    case 41:
    case 42:
    case 43:
    case 44:
    case 40:
    case 50:
    case 51:
    case 52:
    case 53:
    case 60:
    case 61:
    case 62:
    case 63:
    case 64:
    case 65:


void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;

        else if (rc == startMarker) {
            recvInProgress = true;

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        newData = false;

PYTHON side:

Code: [Select]


def _hardConnect(rate):
 ports = list(serial.tools.list_ports.comports())
 for p in ports:
 if 'Arduino' in p.description:
 print('Arduino found on: ' + p[0] + ' port.')
 ARDUINO = serial.Serial(port = port, baudrate = rate,timeout=0,writeTimeout=0)
 return ARDUINO

ARDUINO = _hardConnect(115200)


both files are semplify for readability, I hope I have not forgotten anything.
in short:
python correctly connect to serial of the arduino due;
python, using pyserial.write(), send a string long +/- 340 characters;
However, even using the example in the basic tutorial, I can only get about 140 correctly received characters.

Maybe i do something of wrong? If someone noticed that part of code are krappy, please tell me.

googling around give me no much more, but:
1:cutting the long string in substrings; not easy to know if the data is correcly received and find the correct time between sending data and elaborations of the data.
2:enlarge the buffer size of tx/rx on arduino; really not easy to do with arduino due( actually i cant find the SAM folder where are the files stored, im working under ubuntu and a portable version of arduino software)

thanks in advance for every suggest, and sorry for my poor googled english,


Note, this is not the full code, but it is only the serialEvent and relative
You need to post a complete Arduino program - by all means leave out stuff that is irrelevant, but test the program to make sure it compiles and then post some examples of the output it produces.

Two or three hours spent thinking and reading documentation solves most programming problems.


Code: [Select]
String codes[] = {
There is NO excuse for pissing away resources using the String class to hold constant data.

Code: [Select]
void serialEvent() {
  if (not Serial.available()) {

Absolutely pointless. The function would not have been called if there were no data to read.

Code: [Select]
  inString = Serial.readString();
You have NOT used Robin2's hard work. You have NO control over how much data to read. You are pissing into the wind by not understanding what you are doing, and by NOT sending and receiving controlled data.

I'm done with trying to help you.
The art of getting good answers lies in asking good questions.


Get rid of the serial event. Put the calls to Robin's functions in loop() as he has demonstrated in his examples.

Process the data in e.g. showNewData (and give that function a better name in that case).

How does your code in reply #3 relate to the data in the opening post? I seem to be missing that.

With 65 cases in your switch, you might be better of looking into an array of function pointers. Or possibly an array of struct with the text and a function pointer. That will reduce your processing basically to a couple of lines.

And what is this supposed to do?
Code: [Select]
Serial.println(receivedChars[0, -1]); //prima 0 ultima -1
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up