Python 端程序:
python的串口write函数要求的格式是bytes和bytearray
错误的用法:
期待发送字节0x01
s.write(0x01) 在python中0x01是int类型
s.write(1)
正确的用法:
s.write([0x01])
s.write(six.int2byte(1))
s.write(bytes([0x01]))
import serial
port="COM6"
import time
s=serial.Serial(port,9600)
s.flushInput()
import six
b1=six.int2byte(1)
b0=six.int2byte(0)
s1 = bytes('1', encoding = "utf8") #传字符串
while True:
time.sleep(1)
s.write([0x01])
time.sleep(1)
s.write(bytes([0]))
Arduino端程序
int pin= 13;
void setup()
{
pinMode(pin, OUTPUT);
Serial.begin(9600);
}
int v=0;
void loop()
{
if(Serial.available() > 0)
{
data= Serial.read();//接收单个字节
digitalWrite(pin, data);
}
delay(50);
}