Tiene que ver con la codificación, cuando lo imprimes en pantalla se ve esa b al inicio del texto recibido
No es lo mismo b’Texto’ a ‘Texto’ en python 3, en python2 si los toma como iguales.
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> d =b'23456'
>>> d
'23456'
>>> dd ="23456"
>>> dd == d
True
>>>
$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> d = b'2348'
>>> d
b'2348'
>>> c = "2348"
>>> c == d
False
>>> c == d.decode("utf-8")
True
Si quieres evitar saber cuantos bytes vas a recibir envía los datos con la terminación “\n” en ese momento puedes usar readline en lugar read, este es un ejemplo sencillo usando bottle (web framework para python) y arduino
import serial
import time
from bottle import run, route
#Init the serial port
ser = serial.Serial("/dev/ttyUSB0",9600,timeout=1)
time.sleep(2)
@route("/arduino1")
def arduino1():
ser.write('a')
read_val = ser.readline()
return '{"id":23,"value":%s}'%read_val
run(host="localhost",port=8080)
#close the serial port on exit
ser.close()
int randNumber;
int c;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
randomSeed(analogRead(0));
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available())
{
c = Serial.read();
randNumber = random(200);
Serial.println(randNumber,DEC);
}
}