在python版本3.5.2和pyserial版本3.4的虚拟环境中,我无法写入10个字节。我正在用Salae逻辑分析仪嗅探总线。相同的代码和硬件可在Ubuntu 16虚拟机上运行,但不能直接在主机Mac上运行。
UART_DEVICE = '/dev/ttyUSB0' # '/dev/tty.usbserial-DN04ARYR'
class MyClass():
def __init__(self, port, baudrate=9600, timeout=0.5):
self._serial = serial.Serial()
self._serial.port = port
self._serial.baudrate = baudrate
self._serial.timeout = timeout
self._serial.open()
def __del__(self):
self._serial.close()
def send_test_packet(self):
packet = bytearray([0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A])
print('About to write {}'.format(packet.hex()))
#num_bytes_written = os.write(self._serial.fd, packet)
num_bytes_written = self._serial.write(packet)
print("sent {} bytes".format(num_bytes_written))
mc = MyClass(UART_DEVICE)
mc.send_dummy_packet()
Python和pyserial版本:
$ python --version
Python 3.5.2
$ pip freeze
pyserial==3.4
MacOS版本:10.14.3