我正在尝试在8通道USB中继卡velleman上实现基本的LED开关。从谷歌那里找到了一些示例代码,这给包装带来了一些麻烦。有人知道我该如何解决吗?
import os
import time
import serial
import subprocess
from struct import pack, unpack, calcsize
ser = serial.Serial(port='COM3', baudrate=9600, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE)
ser.isOpen()
print(ser)
channel = 0x01
print("RELAY1_ON_COMMAND: 0xFF 0x01 0x01")
state = 0x01
int_msg = [0xFF, channel, state]
print(int_msg)
byte_msg = map(chr, int_msg)
print(byte_msg)
packet = pack('=ccc', b'byte_msg')
print(packet)
ser.write(packet)
time.sleep(1)
print("RELAY1_OFF_COMMAND: 0xFF 0x01 0x00")
state = 0
int_msg = [0xFF, channel, state]
byte_msg = map(chr, int_msg)
packet = pack('=ccc', *byte_msg)
ser.write(packet)
ser.close()
Result:
Traceback (most recent call last):
Serial<id=0x3832fd0, open=True>(port='COM3', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
RELAY1_ON_COMMAND: 0xFF 0x01 0x01
[255, 1, 1]
Tests/USBRelayCard.py", line 25, in <module>
<map object at 0x0000000003A94588>
packet = pack('=ccc', b'byte_msg')
struct.error: pack expected 3 items for packing (got 1)
Process finished with exit code 1