Four our project, I want to send data mesured by an Arduino due to a computer via Pyton very vast. So I tried to make a simple benchmark by sending 10240 int datas to determine the time it take. In order to do this, I first try using the programming port and execute this simple script:
int i=1;
int maxv=10240;
int value[10240];
int deb, fin;
void setup() {
// put your setup code here, to run once:
Serial.begin(250000); //serial initialisation
for (i=0 ; i<maxv ; i++){
value[i]=i;
}
delay(200);
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("starti"); //flag
deb=micros();
for (i=0 ; i<maxv ; i++) { // sending loop
Serial.println(value[i]);
}
fin=micros();
Serial.println("stopi");
Serial.println((fin-deb)/1000000.0);
delay(2000); //waiting
}
So with this I can mesure the time it take to send all the datas. And I receive it with this Python's script:
import numpy as np
import serial
ser = serial.Serial('/dev/ttyACM0', 250000)
value=np.empty(10240)
temp=ser.readline()
while temp!='starti\r\n': #search for the flag
temp=ser.readline()
for j in range(10240):
value[j]=int(ser.readline())
ser.readline()
time=float(ser.readline())
print time
with this beaudrate, it take nearly 2.4s which too slow for my purpose. If try another baudrate, 500000 for example I read data like this : �Ë
$Pd@s`¥š... I have this for all baudrate higher than 250000.
So I tried the native port which normaly send the data as fast as he can, but we this it took nearly 1.3s, and it's again too slow for my purpose.
I checked that it is not python wich is too slow, by doing the same than in the last example:
import time
import numpy as np
value=np.empty(10240)
deb1 = time.clock()
for j in range(10240):
value[j]=j
fin1= time.clock()
print fin1-deb1
and it took only 4ms. So is it clearly the arduino wich send the data to slowly.
So how can I increase the data flux? I have seen the serial.write(buf,len) function which seems does not convert the data into ascii caracters. Does this function will be faster by putting the correct arguments? And, if it's the cas, how can I read this with Python?