import minimalmodbus
import serial
usbDevice = '/dev/ttyUSB0'
modbusSlaveID = 1
# can be 'ascii' or 'rtu'
modbusFormat = 'rtu'
registerToRead = 64
# 3 is for Holding Registers, 4 is for Input Registers
functionCode = 3
# initialize the device
device = minimalmodbus.Instrument(usbDevice, modbusSlaveID, modbusFormat)
# set the various options, which will depend on the device you are communicating with
device.debug = True
device.serial.baudrate = 9600
device.serial.bytesize = 8
device.serial.parity = serial.PARITY_NONE
device.serial.stopbits = 1
device.serial.timeout = 2 # seconds
print device.read_register(registerToRead, functioncode=functionCode)
2 回答
大多数硬件上的UART接口与这些类型的传感器兼容 . 默认情况下,电路板/模块上的UART引脚工作在TTL logic levels . 像 RS-232 和 RS-485 这样的电气标准使用相同的基本协议,但修改输出电压和信号线的配置 .
因此,在您的情况下,您只需要找到TTL和RS-485之间的转换器,就像您提到的MAX485一样 . 将其连接到电路板上的任何可用UART,并使用相同的Peripheral I/O APIs与Android Things进行通信 .
我不熟悉Android Things,但希望这会指出你正确的方向......我在Raspberry Pi上使用USB to 485 converter和minimalmodbus python library取得了很大的成功 . 请参阅下面的一些我过去使用的示例代码 . 这是非常基本的,但应该让你开始 .
附:这是我的第一个答案,希望我做得对...