Python and Arduino Serial Communication -


i using python 3.2 trying communicate arduino through serial port. documentation, understad arduino serial.read() reads individual bytes. however, when tried implement this, serial.read() reads numbers sent. here code python , arduino

for example, have value 412 send arduino.

python:

xcoordint = 412 xcoordconverted = "%03d" % (xcoordint) xcoord = [int(i) in str(xcoordconverted)] xsingledigit0 = chr(int(xcoord[0] + 48)) xsingledigit1 = chr(int(xcoord[1] + 48)) xsingledigit2 = chr(int(xcoord[2] + 48)) ser.write (bytes(xsingledigit0, 'utf-8')) ser.write (bytes(xsingledigit1, 'utf-8')) ser.write (bytes(xsingledigit2, 'utf-8')) 

arduino:

char joincharx[3] ; int n_avail = serial.available();   if(n_avail>0){      (int i=0;i<3; i++){       joincharx[i] = serial.read();     } int xcoords = atoi(joincharx); serial.print(joincharx[0]); 

the joincharx[0] when returned 412 , not 4. wondering why , how read 1 individual byte @ time?

try:

char joincharx[3] ; int n_avail = serial.available();   if(n_avail>0){      (int i=0;i<3; i++){       joincharx[i] = serial.read(1);  # note parameter     } int xcoords = atoi(joincharx); serial.print(joincharx[0]); 

this should make read 1 char @ time.

correction:

you can't limit no chars above problem print statement takes first char start of string: if see first char need use:

char forprint; : : forprint = joincharx[0]; serial.print (forprint); 

Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -