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:
//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:
#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,