Sto testando la velocità di trasmissione seriale di Arduino UNO. Per le mie esigenze devo trasferire 3KB/s dal PC host all'Arduino. Ho scritto un programma molto semplice che replica il risultato di Serial.available()
e poi lo ha testato nel monitor seriale dell'IDE di Arduino. Ho iniziato a inviare caratteri fino a raggiungere il massimo che è di 63 byte. Ne sono rimasto piuttosto sorpreso perché ho letto da qualche parte che l'Arduino ha un buffer Serial 128byte.Trasmissione seriale Slow Arduino
In ogni caso, ho creato un protocollo molto semplice che trasmette i dati in pacchetti da 48 byte (in realtà 49 byte a causa del carattere dell'intestazione). L'host invia un carattere d
quindi 48 byte di dati. Per testare la validità della trasmissione sto inviando una semplice sequenza di byte da 0 a 47 che viene convalidata sul lato Arduino. Se la convalida fallisce, l'UNO inizia a lampeggiare il LED integrato sul PIN13. Una volta inviati i byte, l'host attende una conferma che è un semplice carattere . L'Arduino lo invia una volta che ha finito di elaborare il pacchetto vero e proprio.
Il programma host misura il numero di pacchetti trasmessi e lo visualizza dopo 1 secondo. Con una velocità di trasmissione di 9600 il PC trasmette con successo ~ 16 pacchetti/secondo (~ 800 byte/sec) che è abbastanza ok. Ho cercato di migliorare questo aumentando il baud rate su entrambi i lati a 57600; tuttavia, il numero di pacchetti inviati aumenta solo di poco. Non so quale sia il problema. Forse ho raggiunto qualche limite del convertitore seriale USB?
Ecco il mio codice.
PC (Java, sto usando jSSC per la comunicazione seriale)
package hu.inagy.tapduino.server;
import jssc.SerialPort;
import jssc.SerialPortException;
/**
* Test Arduino communication.
*/
public class App
{
private static void testComm(SerialPort port) throws SerialPortException {
long runningSeconds = 0;
long time = System.currentTimeMillis();
long numberOfPackets = 0;
boolean packetSent = false;
while (runningSeconds < 10) {
long currentTime = System.currentTimeMillis();
if (currentTime - time > 1000) {
runningSeconds++;
time = currentTime;
System.out.println(numberOfPackets + " packets/s");
numberOfPackets = 0;
}
if (!packetSent) {
packetSent = true;
port.writeByte((byte) 'd');
for (int i = 0; i < 48; i++) {
port.writeByte((byte) i);
}
} else {
byte[] received = port.readBytes();
if (received != null) {
if (received.length > 1) {
throw new IllegalStateException("One byte expected, instead got: " + received.length);
}
char cmd = (char) received[0];
if ('k' != cmd) {
throw new IllegalStateException("Expected response 'k', instead got: " + cmd);
}
packetSent = false;
numberOfPackets++;
}
}
}
}
public static void main(String[] args)
{
SerialPort port = new SerialPort("COM7");
try {
if (!port.openPort()) {
throw new IllegalStateException("Failed to open port.");
}
port.setParams(57600, 8, 1, 0);
} catch (SerialPortException e) {
throw new IllegalStateException("Exception while setting up port.", e);
}
try {
// Wait 1.5sec for Arduino to boot successfully.
Thread.sleep(1500);
} catch (InterruptedException e) {
throw new IllegalStateException("Interrupt while waiting?", e);
}
try {
testComm(port);
} catch (SerialPortException exc) {
throw new IllegalStateException("Failure while testing communication.", exc);
} finally {
try {
if (!port.closePort()) {
throw new IllegalStateException("Failed to close port.");
}
} catch (SerialPortException e) {
throw new IllegalStateException("Exception while closing port.", e);
}
}
}
}
Arduino
void setup() {
pinMode(13, OUTPUT);
Serial.begin(57600);
}
boolean error = false;
void loop() {
if (error) {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
} else {
while (Serial.available()<49);
char cmd = Serial.read();
if ('d'!=cmd) {
error=true;
return;
}
for (int i=0; i<48; i++) {
int r = Serial.read();
if (r!=i) {
error=true;
return;
}
}
Serial.write('k');
}
}
Ho chiesto la stessa domanda sui forum di Arduino come http://arduino.cc/forum/index.php/topic,104699.0.html – NagyI