I have a file with numbers called fginputs.txt
. For example it could be something like this:
4958
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4154
4958
I want python to send each number to arduino via serial port. After each number is received, arduino should print back an acknowledgment number, indicating that it got a valid number, and then store that number in a dynamic array, because I could create larger files. When there are no more numbers left, send a '-1' to finish transmission.
Here's my arduino code:
// save some unsigned ints
uint16_t SIZE, *inputList, cont = 0;
boolean inputsReady = false;
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
//free dynamic array memory
free(inputList);
//counter for how many numbers I've received starts at 0
cont = 0;
//This is true when there are no more numbers to receive, meanwhile false
inputsReady = false;
setupInputList();
}
/* If there's not enough space, resize the array by one unit and store the number
*/
void growAndInsert(int currentSize, int newInt){
if(currentSize > SIZE)
inputList = (uint16_t *)realloc(inputList, (currentSize + 1)*sizeof(uint16_t));
inputList[currentSize] = newInt;
}
/**
init inputList with 100 blocks
*/
void setupInputList(){
SIZE = 10;
inputList = (uint16_t *)malloc(sizeof(uint16_t) * SIZE);
}
void clearBuffer(){
while(Serial.available() > 0)
Serial.read();
}
/**
Listens in serial port for an integer that represents a new input and returns it.
If it doesn't get anything useful from serial, return 0
*/
int getNewInputFromSerial(){
if (Serial.available() > 0) {
delay(100);
// look for the next word
int cmd = Serial.parseInt();
clearBuffer();
if(cmd == 4958)
Serial.write("4");
else if(cmd == 4154)
Serial.write("5");
else
Serial.write("0");
return cmd;
}
return 0;
}
void loop()
{
if(!inputsReady){
int newInput = getNewInputFromSerial();
if(newInput == 0)
return;
if(newInput != -1)
growAndInsert(cont++, newInput);
else{
inputsReady = true;
//initTimer();
}
}
}
and the python script:
global arduino
PORT = '/dev/ttyACM0'
FILENAME = "fginputs.txt"
#Read file with inputs
with open(FILENAME) as f:
content = f.readlines()
#init serial port
arduino = serial.Serial(PORT, 9600, timeout=1);
time.sleep(2);
#write
for input in content:
arduino.flush()
arduino.write(input)
time.sleep(.1);
resp = arduino.read();
print "i got " + resp
#Finish transmission with -1
arduino.flush()
arduino.write("-1")
#done
arduino.close();
The fist time I execute the script I get this:
i got 4
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 5
i got 4
Which is great. But if I run it a second time I get this:
i got 0
i got 4
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
i got 0
Which is terribly wrong because the file hasn't changed. I don't know what is going on here. If I unplug and plug back in the USB cable, transmission works flawlessly again.