我是一名使用传感器进行串行通信的初学者,在我最近的项目中,我一直试图让气象站使用MODBUS RTU通过RS485与RevPi Compact进行通信,我似乎总是遇到以下错误:
**Traceback (most recent call last):
File "backup_rs485.py", line 98, in <module>
data = [getWindspeed()[0]/10, getPrec()[0]/10, getPrec_intensity()[0]/10, getPrec_amount()[0]/10, getPrec_type()[0]/10, getAirtemp()[0]/10, getWinddirection()[0]/10] # save data as a array
File "backup_rs485.py", line 23, in getWindspeed
windspeed = client.read_input_registers(0x7533, 10, unit=0)
File "/home/pi/venv/lib/python3.5/site-packages/pymodbus/client/common.py", line 125, in read_input_registers
return self.execute(request)
File "/home/pi/venv/lib/python3.5/site-packages/pymodbus/client/sync.py", line 107, in execute
raise ConnectionException("Failed to connect[%s]" % (self.__str__()))
pymodbus.exceptions.ConnectionException: Modbus Error: [Connection] Failed to connect[ModbusSerialClient(rtu baud[19200])]**
以下是我的代码示例:
import minimalmodbus
client = ModbusClient(method='rtu', port='/dev/ttyRS485', timeout=100)
connection = client.connect()
print(connection) # true if connection ist established
def getWinddirection() -> list:
"""Returns the wind direction in degrees"""
degree = client.read_input_registers(0x75FB, 10, unit=0)
return degree.registers
def getWindspeed() -> list:
"""Returns the wind speed in m/s."""
windspeed = client.read_input_registers(0x7533, 10, unit=0)
return windspeed.registers
def getPrec() -> list:
"""Returns the precipitation status."""
precipitation = client.read_input_registers(0x7AA9, 1, unit=0)
return precipitation.registers
def getPrec_intensity() -> list:
"""Returns the precipitation intensity."""
precipitation_intensity = client.read_input_registers(0x7AAB, 1000, unit=0)
return precipitation_intenstity.registers
def getPrec_amount() -> list:
"""Returns the amount of precipitation."""
precipitation_amount = client.read_input_registers(0x7AAD, 1000, unit=0)
return precipitation_amount.registers
def getPrec_type() -> list:
"""Returns the type of precipitation."""
precipitation_type = client.read_input_registers(0x7AAF, 1, unit=0)
return precipitation_type.registers
def getAirtemp() -> list:
"""Returns the air temperature."""
temperature = client.read_input_registers(0x76C1, 1, unit=0)
return temperature.registers
def getLog() -> list:
"""Returns the Log of all Values"""
return log
def getloggedWindspeed() -> list:
return speed
def getloggedPrec() -> list:
return prec
def getloggedPrec_intensity() -> list:
return prec_intensity
def getloggedPrec_amount() -> list:
return prec_amount
def getloggedPrec_type() -> list:
return prec_type
def getloggedAirtemp() -> list:
return temp
def getloggedWinddirection() -> list:
return direction
def suddenwind() -> bool:
over = 0
for i in speed:
if i >= 2.0:
over += 1
if over >= 21:
return True
else:
return False
if __name__ == '__main__':
log = []
speed, prec, prec_intensity, prec_amount, prec_type, temp, direction = [], [], [], [], [], [], []
for i in range(1, 30):
data = [getWindspeed()[0]/10, getPrec()[0]/10, getPrec_intensity()[0]/10, getPrec_amount()[0]/10, getPrec_type()[0]/10, getAirtemp()[0]/10, getWinddirection()[0]/10] # save data as a array
log.append(data) # adding the data array to the log
print("Windspeed: " + str(data[0]) + " m/s")
print("Precipitation: " + str(data[1]) + " 0 = No, 1= Yes")
print("Precipitation intensity: " + str(data[2]) + " mm/h")
print("Precipitation amount: " + str(data[3]) + " mm/h")
print("Precipitation type: " + str(data[4]) + " 0 No precipitation")
print("Temperature: " + str(data[5]) + " °C")
print("Winddirection: " + str(data[6]) + " °\n")
time.sleep(1)
for datas in log: # creating lists for each individual parameter so they can be read out
speed.append(datas[0])
prec.append(datas[1])
prec_intensity.append(datas[2])
prec_amount.append(datas[3])
prec_type.append(datas[4])
temp.append(datas[5])
direction.append(datas[6])
我还尝试通过用替换代码中的一行来更改波特率
client = ModbusClient(method='rtu', port='/dev/ttyRS485', timeout=100, baudrate=9600)
但我最终也犯了类似的错误
任何帮助或建议都将不胜感激。 再次感谢这个美好的社区
目前没有回答
相关问题 更多 >
编程相关推荐