Python Suddenly stops listening

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]*

You have got the code tags around the wrong part of your post which has made your description and question unreadable.

Try again.

...R
PS. ALWAYS read back your Post after posting it

Apologies, thank you for letting me know.

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
		
	#-----------------------------------]



[code][/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<float> 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[i] = Serial.readStringUntil(',').toInt();

    
  
      
    }

digitalWrite(13,HIGH);
  delay(100);
  digitalWrite(13,LOW);
  delay(100);
    
Serial.flush();
while (Serial.available() == 0){
  Serial.println("ready");
}

 
  }


}
]

That's an improvement. But next time show the Python and Arduino programs separately

You have not posted a complete Arduino program. But I see you are using the String class and that can cause an Arduino to crash. It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

I suspect the use of the String class is the main problem.

Also, IMHO your Arduino code for receiving data is very weak. Have a look at the examples Serial Input Basics - simple reliable ways to receive data. The technique in the 3rd example will be the most reliable and easily matched in Python.

If you still have problems get the Python program to print the output rather than sending to the Arduino does it get stuck? You could program it so you press RETURN to simulate an OK from the Arduino.

Your code is confusing when you use the word "arduino" for the name of your serial port. It looks like you are instructing the Arduino with arduino.reset_input_buffer()

You seem to send something to the Arduino after you receive a 'r' but before you send the data? Why?

...R
Python - Arduino demo