struct.error:必填参数不是整数


问题内容

我有以下python代码:

velocity = 0
rotation = 0
vr = velocity + (rotation/2)
vl = velocity - (rotation/2)
cmd = struct.pack(">Bhh", 145, vr, vl)

我处理以下错误:

File "control.py", line 125, in __init__  
cmd = struct.pack(">Bhh", 145, vr, vl)  
struct.error: required argument is not an integer

问题答案:

您为传入的参数使用了错误的格式hformat表示要存储的short,而传入的值(即vrvl)看起来像doubles。

考虑将它们进行类型转换int或使用">Bdd"格式。