UpBge doesn't read serial port (arduino)

I try to send order to upbge with an arduino (leonardo).

My code arduino code send a word every 10 millis :

void setup(){
  Serial.begin(9600);
}
void loop(){
  output = "idle";
 // other stuff here change the output var
 // ...
  Serial.print( output );
  delay(10);
}

upbge has the following script launched by an always brick :

import bge, serial, time
ser = serial.Serial('/dev/ttyACM0', timeout=0)
bs = ser.readline() 
print( bs )
ser.close()

and print(bs) print nothing !

But, the arduino monitor displays the value.
I also test in the system console with this script, and it works well :

import serial, time

ser = serial.Serial('/dev/ttyACM0', timeout=0) 

while True:
	bs = ser.readline().decode()
	if len(bs) > 0:
		print( bs )
	time.sleep(0.0001)
ser.close()

any help welcome :slight_smile:

1 Like

I’ve found a solution.
replace :
bs = readline()
by
bs = ser.readline().strip().decode()

the minimal code could be :

import serial
ser = serial.Serial('/dev/ttyACM0', timeout=0)
bs = ser.readline().strip().decode()
if bs == "idle":
    # do something with idle
    pass
ser.close()
1 Like

What’s your project. It looks like it might be interesting.
What you gonna do, make a custom joystick?

I’m a graphic design teacher and I’m preparing a session for my students on creating “hybrid game” prototypes.

The idea is to do research on how to mix a traditional physical game (playing cards, game board, dice, etc.) with a video game.

We can imagine RFID sensors in the cards, buttons embedded in the board, and so on.

So I’m preparing the ground to give them technical support.

1 Like