Hi guys,
I'm using pyserial in order to send some values from Python PC to Arduino, the main problem is that I use serial.write(1) several times in Python and then read them with Serial.parseInt();
The Python program get stucks and then receive all the prints that I have between serial.write all together except the first one. Any suggestions??
Arduino code:
void setup() {
/*
* --- SERIAL COMMUNICATION DATA FROM RASPBERRY/PC
*/
//Start serial communication
Serial.begin(9600);
//Set preescaler
int preescaler = 0;
while(preescaler == 0){
preescaler = Serial.parseInt();
}
//Set compare register
int registerCheck = 0;
while(registerCheck == 0){
registerCheck = Serial.parseInt();
}
//Set key length
while(keyLen == 0){
keyLen = Serial.parseInt();
}
//Read as many keys as indicated and store them in a byte array
byte laserArray[keyLen] = {0};
for(int i = 0;i<keyLen;i++){
int data = 0;
while(data == 0){
data = Serial.parseInt();
laserArray[i] = data;
}
}
And Python:
ser = serial.Serial(ArduinoVars.PORT.value, ArduinoVars.BOUND.value)
progress_callback.emit(1)
#Write preescalar to arduino
self.__writeDataToArduino(ser,self.__prescaler)
print("WROTE")
#Write register to arduino
self.__writeDataToArduino(ser, self.__register)
print("WROTE")
#For each laser array send to arduino
for register in self.__laserArray:
print("WROTE")
self.__writeDataToArduino(ser, register)
print("WROTE END")
#Wait until beacon signal to start sending all to 1
while self.__waitFlag[0]:
pass
self.__waitFlag[0] = True
print("DONE")
progress_callback.emit(2)
#Send signal of starting all to 1 beacon
self.__writeDataToArduino(ser,1)
#Wait until signal that beacon is ok FROM TCP
while self.__waitFlag[0]:
pass
#Send signal to start sending beacon msg and then qubits
self.__writeDataToArduino(ser,1)
#Wait until arduino say it has finished
print(self.__readDataFromArduino(ser))
print("FINISH")
def __writeDataToArduino(self,ser,msg):
ser.write(msg)
I just only see the first WROTE and then after a few seconds all the WROTE come together. By the way, when I use the Serial Monitor it works well.