Hello,
I have my arduino mega 2560 communicating with my computer over the serial port using Python's pyserial library (v3.3). I want to send a massive text file to the arduino, but since the arduino has limited memory, I am sending the file line by line. I have a program set up on the python side to read the text file, send a line of text, wait for a ready message from the arduino, send a message received message to the arduino, and then proceed to send the next line. On the Arduino side, it waits for first line of text. after receiving this line, it sends a ready signal to python via the serial port until the python code replies with its ready message, and then the process is repeated. This process is successful until line 57. Everytime at line 57, the python code just stops listening. I was hoping that the Arduino was the problem, but I am able to perform the same process over the serial monitor with no problems and go way pay 57 lines. I also tried adjusting the baud rate and setting the timeout to None, but to no avail. I will attach my python and arduino code.
Is there a reason that python is timing out at 57? Or is python somehow missing the message and never escaping an infinite loop?
**Note that in the python code I left out the part about grabbing the text file and plugging it into a matrix M because it does not really apply here.
Thank you for your time.
Python Code:
[#Setup Serial communication of matrix from python to Arduino
print ("Opening Serial port...")
arduino = serial.Serial("COM5", 9600,timeout = 1)
time.sleep(2)
print ("Initialise Complete")
M = Matrix;
#Tell arduino how many rows and columns are in Matrix
info = '%s,' % h
info += '%s,' % w
arduino.write(info.encode())
print(info)
print(h)
print(w)
time.sleep(3)
#perform operations for every row in matrix
for j in range(0,h):
#Package values in matrix for sending
command = ''
for i in range(0,w):
if i == 0:
command = '%s' % M[j][i]
elif i > 0 and i < w-1:
command += ',%s' % M[j][i]
else:
command += ',%s,' % M[j][i]
#Send row of matrix
arduino.write(command.encode())
time.sleep(.1)
print(command)
print(j)
#time.sleep(.1)
#------------------------------------
print('click')
arduino.reset_input_buffer()
while 1:
hold = arduino.read(5).decode();
if hold[0] == 'r':
print(hold)
ready = 'go'
arduino.write(ready.encode())
time.sleep(.1)
break]
Arduino Code:
[void loop () {
// Determine number of points received from Python:
while(Serial.available()==0){
}
if(Serial.available() > 0){
rose = Serial.readStringUntil(',').toInt();
colin = Serial.readStringUntil(',').toInt();
Serial.println(rose);
Serial.println(colin);
}
pos.resize(rose); // expand position vector to accomodate points
while(Serial.available() > 0){
Serial.read();
}
// Getting the points from the text file:
vector temppos(5);
for(int i = 0; i < rose; i++) { // loop through number of points to be read
while(Serial.available()==0) { // Do nothing until you receive serial input
}
if(Serial.available() > 0) {
for (int j =0; j < colin - 1; j++) { // read one line of the serial input
temppos[j] = Serial.readStringUntil(',').toFloat();
}
times = Serial.readStringUntil(',').toInt();
- }*
digitalWrite(13,HIGH); - delay(100);*
- digitalWrite(13,LOW);*
- delay(100);*
Serial.flush();
while (Serial.available() == 0){
-
Serial.println("ready");*
} -
}][/code]*