Go Down

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

pandaro

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

Code: [Select]
0000000010000000000100100010010000000100000000001000000000000001000000000000
101011000001011110000000000010001010000011011110001001000010100000010000010010000
010010010010000100001000100000010000010000100010000000000000001001000000000000000
000000001101000001000000000000100000000000000011011001100000001000000000000011000
00010000000001000


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

groundFungus

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

PaulS

Quote
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.

pandaro

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

ARDUINO side:

Code: [Select]


//vars

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


//CODES///////////////////////////////////////////////////////////////////
String codes[] = {
  "empty",//0
  "&Ypos",//1
  "&Wpos",//2
  "&Xpos",//3
  "&Zpos",//4
  "&Lpos",//5
  "&Rpos",//6
  "$wait",//7
  "$play",//8
  "$stop",//9
  "$apld",//10
  "$push",//11
  "$RAll",//12
  "$load",//13
  "$prsd",//14
  "$RSTY",//15
  "$RSTW",//16
  "$RSTX",//17
  "$RSTZ",//18
  "%mapp",//19
  "$lstn",//20
  "?",//21
  "?",//22
  "?",//23
  "?",//24
  "?",//25
  "?",//26
  "?",//27
  "?",//28
  "?",//29
  "?",//30
  "&SSAY",//31
  "&SSAW",//32
  "&SSAX",//33
  "&SSAZ",//34
  "&ACCY",//35
  "&ACCW",//36
  "&ACCX",//37
  "&ACCZ",//38
  "?",//39
  "$STP0",//40
  "$STP1",//41
  "$STP2",//42
  "$STP3",//43
  "$STP4",//44
  "?",//45
  "?",//46
  "?",//47
  "?",//48
  "?",//49
  "%ZPlY",//50
  "%ZTrY",//51
  "%Z00W",//52
  "%ZLoW",//53
  "?",//54
  "?",//55
  "?",//56
  "?",//57
  "?",//58
  "?",//59
  "%SRxN",//60
  "%SRxN",//61
  "%SRxO",//62
  "%SRxO",//63
  "%SRxC",//64
  "%SRxC",//65
  "?",//66
  "?",//67
  "?",//68
  "?",//69
  "?",//70
  "?",//71
  "?",//72
  "?",//73
  "?",//74
  "?",//75
  "?",//76
  "?",//77
  "?",//78
  "?",//79
  "?",//80
  "?",//81
  "?",//82
  "?",//83
  "?",//84
  "?",//85
  "?",//86
  "?",//87
  "?",//88
  "?",//89
  "?",//90
  "?",//91
  "?",//92
  "?",//93
  "?",//94
  "?",//95
  "?",//96
  "?",//97
  "?",//98
  "?",//99
};

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

void serialEvent() {
  if (not Serial.available()) {
    return;
  }

  //recvWithStartEndMarkers();
  //showNewData();


  int command;
  inString = Serial.readString();
  Serial.println("serialEvent");
  Serial.println(inString);

  
  
  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(i);
      Serial.print(" : ");
      Serial.println(code);
      command = i;
      break;
    }
  }
  switch (command) {
    case 1:
      moveToPos(1, pos);
      break;
    case 2:
      moveToPos(2, pos);
      break;
    case 3:
      moveToPos(3, pos);
      break;
    case 4:
      moveToPos(4, pos);
      break;
    case 5:
      servoLx.writeMicroseconds(pos);
      break;
    case 6:
      servoRx.writeMicroseconds(pos);
      break;
    case 7:
      pause();
      break;
    case 8:
      play();
      break;
    case 9:
      alt();
      break;
    case 12:
      resetAll();
      break;
    case 15:
      resetY();
      break;
    case 16:
      resetW();
      break;
    case 17:
      resetX();
      break;
    case 18:
      resetZ();
      break;
    case 19:
      loadNewMap(info);
      break;
    case 20:
      pong();
      break;
    case 31:
      setEngineSpeed(1, pos);
      break;
    case 32:
      setEngineSpeed(2, pos);
      break;
    case 33:
      setEngineSpeed(3, pos);
      break;
    case 34:
      setEngineSpeed(4, pos);
      break;
    case 35:
      setEngineAccel(1, pos);
      break;
    case 36:
      setEngineAccel(2, pos);
      break;
    case 37:
      setEngineAccel(3, pos);
      break;
    case 38:
      setEngineAccel(4, pos);
      break;
    case 41:
      stopY();
      break;
    case 42:
      stopW();
      break;
    case 43:
      stopX();
      break;
    case 44:
      stopZ();
      break;
    case 40:
      stopAll();
    case 50:
      setZPlY(pos);
      break;
    case 51:
      setZTrY(pos);
      break;
    case 52:
      setZ00W(pos);
      break;
    case 53:
      setZLoW(pos);
      break;
    case 60:
      setSRxN(pos);
      break;
    case 61:
      setSLxN(pos);
      break;
    case 62:
      setSRxO(pos);
      break;
    case 63:
      setSLxO(pos);
      break;
    case 64:
      setSRxC(pos);
      break;
    case 65:
      setSLxC(pos);
      break;


  }
}




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;
                Serial.println(rc);
                Serial.println(ndx);
                ndx++;
                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 ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}



PYTHON side:

Code: [Select]


#connection
ARDUINO = 'ARDUINO NON PRESENTE, CONNETTI ARDUINO'

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

ARDUINO = _hardConnect(115200)

ARDUINO.write('<%mapp111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111>')



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.

otherwise:
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,

Robin2

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.

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

PaulS

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()) {
    return;
  }

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.

sterretje

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