Python 变量串行通信
我试图通过OS X上的Pyserial与我的Arduino接口。我通过发送0到9的数字来控制LED。 代码如中所示Python 变量串行通信,python,serial-port,arduino,pyserial,Python,Serial Port,Arduino,Pyserial,我试图通过OS X上的Pyserial与我的Arduino接口。我通过发送0到9的数字来控制LED。 代码如中所示 import serial arduino = serial.Serial('/dev/tty.usbserial', 9600) arduino.write('5') 工作得非常好,但我尝试将本例中的5作为可变变量,但类似于 arduino.write('%d') % 5 不行。我不知道如何格式化输出等于工作示例的变量?您没有格式化字符串%d,而是格式化函数调用: ardu
import serial
arduino = serial.Serial('/dev/tty.usbserial', 9600)
arduino.write('5')
工作得非常好,但我尝试将本例中的5作为可变变量,但类似于
arduino.write('%d') % 5
不行。我不知道如何格式化输出等于工作示例的变量?您没有格式化字符串
%d
,而是格式化函数调用:
arduino.write('%d' % 5)
%
符号应立即位于'
后面
这将起作用,但最好使用具有格式字符串的元组:
arduino.write('%d' % (5,))
因为当您有多个参数时,您无论如何都必须这样使用它:
arduino.write('%d.%d' % (2, 3))
您没有格式化字符串
%d
,而是格式化函数调用:
arduino.write('%d' % 5)
%
符号应立即位于'
后面
这将起作用,但最好使用具有格式字符串的元组:
arduino.write('%d' % (5,))
因为当您有多个参数时,您无论如何都必须这样使用它:
arduino.write('%d.%d' % (2, 3))
字符串格式必须放在函数调用的括号内 e、 g.
arduino.write(“%d”%5)
这里有一些关于字符串格式必须放在函数调用的括号内的信息 e、 g.
arduino.write(“%d”%5)
这里有一些关于的信息,我想我在这里使用的是“旧式格式”。那么“新”的格式化方式是什么@birgit检查“新”字符串格式语法。我假设我在这里使用“旧式格式”。那么“新”的格式化方式是什么@birgit检查“新”字符串格式语法。