# How can I read data from an Arduino without resetting it?

I would like to read the data printed by an Arduino in Blender but the Arduino is reset each time Blender reads the data.
Here is my Blender code:

import bge
import mathutils
import serial

from math import pi
def Player():
cont = bge.logic.getCurrentController()
obj = cont.owner

serialport = serial.Serial('COM3', 115200)

# tell Arduino to send values
serialport.write('a'.encode('ascii'))

# read a line from the serial port

# Pi radians = 180 degrees so, since Arduino sends values between 0 and
# 255, we devide by 255 in order to spread the 255 over pi radians (from
# -90 to +90 degrees).

# There is one angle for each axis, and there are 3 axes.
Angles=[0.0,0.0,0.0]  # inicial angles for X, Y and Z axes
AnglesNumber=3       # number/quantity of angles/axes
for i in range(AnglesNumber):
Angles[i] = Angles[i] + (float)(line.split()[i])
# And we convert each value to radians (from -pi/2 to +pi/2).

mat_rot_z = mathutils.Matrix.Rotation(Angles[0], 3, 'X')
mat_rot_x = mathutils.Matrix.Rotation(Angles[1], 3, 'Y')
mat_rot_y = mathutils.Matrix.Rotation(Angles[2], 3, 'Z')

obj.localOrientation = mat_rot_x * mat_rot_y * mat_rot_z

• First try to minimise the code. Your Arduino will probably not be reset by parsing text as floats or converting angles to matrices. By removing all but the relevant code it'll be easier for you and us to drill down to the real problem. Commented Dec 7, 2017 at 20:35
• Commented Dec 7, 2017 at 23:04
• I wonder if the problem does not come because the sensor true level triggering is activated, but if I deactivate it, i have to use a while loop only to read the given in a loop. but when i do that like this, blender don't work, so i don't know how to do... Commented Dec 8, 2017 at 1:05

You create a new Serial each time you call that script.

It is better to create it once and communicate with the connection as long as it is established.

Unfortunately I can't provide you with working code due to non-existence environment.

# Connecting

One script gets executed and establishes the connection once:

serial = serial.Serial('COM3', 115200)
owner["serial"] = serial


# Communicating

A second script handles communication (polls for data) running at each single frame.

serial = owner["serial"]
serial.write('a'.encode('ascii'))
...


# Combined

Due to the strong dependency between connecting and communicating, you might want to combine both operations:

serial = owner.get("serial");
if not serial:
serial = serial.Serial('COM3', 115200)
owner["serial"] = serial

serial.write('a'.encode('ascii'))