a Unicode string is a sequence of code points, which are numbers from 0 through 0x10FFFF (1,114,111 decimal). This sequence needs to be represented as a set of bytes (meaning, values from 0 through 255) in memory. The rules for translating a Unicode string into a sequence of bytes are called an encoding.
write(data)
Parameters: data – Data to send.
Returns: Number of bytes written.
Return type: int
Raises SerialTimeoutException:
In case a write timeout is configured for the port and the time is exceeded.
Write the bytes data to the port. This should be of type bytes (or compatible such as bytearray or memoryview). Unicode strings must be encoded (e.g. 'hello'.encode('utf-8').
os — Miscellaneous operating system interfaceshttps://docs.python.org/3/library/os.html
Yo quiero ser como tu :( hay algo queno sepas.Google no encuentre?
MUCHAS GRACIAS.
$ 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
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);
}
}
Gracias tsk por las molestias pero no estoy intentando comparar números ni trabajo con arduino.
Saludos!
'1234' --> Texto
"1234" --> Texto
1234 --> Número
>>> type(1234)
<class 'int'>
>>> type('1234')
<class 'str'>
>>> type("1234")
<class 'str'>
>>>
timeout (float) – Set a read timeout value.
Possible values for the parameter timeout which controls the behavior of read():
timeout = None: wait forever / until requested number of bytes are received
timeout = 0: non-blocking mode, return immediately in any case, returning zero or more, up to the requested number of bytes
timeout = x: set timeout to x seconds (float allowed) returns immediately when the requested number of bytes are available, otherwise wait until the timeout expires and return all bytes that were received until then.