УСПЕХ!!!
This commit is contained in:
parent
992b620014
commit
9ff5347b05
90
main.py
90
main.py
@ -1,46 +1,54 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI
|
||||
from pymodbus.client import ModbusSerialClient
|
||||
import threading
|
||||
import time
|
||||
import threading
|
||||
import time
|
||||
import logging
|
||||
import sys
|
||||
|
||||
serial_port = "COM6"
|
||||
baud_rate = 9600
|
||||
modbus_client = ModbusSerialClient(serial_port, baudrate=baud_rate)
|
||||
modbus_data = "0"
|
||||
serial_port = "COM6"
|
||||
baud_rate = 9600
|
||||
modbus_client = ModbusSerialClient(serial_port, baudrate=baud_rate)
|
||||
modbus_data = "0"
|
||||
|
||||
logging.basicConfig()
|
||||
log = logging.getLogger()
|
||||
log.setLevel(logging.DEBUG)
|
||||
# logging.basicConfig()
|
||||
# log = logging.getLogger()
|
||||
# log.setLevel(logging.DEBUG)
|
||||
|
||||
def poll_modbus():
|
||||
global modbus_data
|
||||
modbus_client.connect()
|
||||
while True:
|
||||
try:
|
||||
response = modbus_client.read_discrete_inputs(10001, 1, slave=1)
|
||||
#print(response)
|
||||
|
||||
if not response.isError():
|
||||
modbus_data = response.registers[0]
|
||||
print(modbus_data)
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
threading.Thread(target=poll_modbus, daemon=True).start()
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/modbus_data")
|
||||
async def get_modbus_data():
|
||||
return {"modbus_data": modbus_data}
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Welcome to FastAPI with Modbus"}
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run(app, host="0.0.0.0", port=12345)
|
||||
|
||||
def poll_modbus():
|
||||
global modbus_data
|
||||
while True:
|
||||
try:
|
||||
modbus_client.connect()
|
||||
time.sleep(3.0)
|
||||
response = modbus_client.read_discrete_inputs(0, 1, slave=1)
|
||||
#print(response.bits[0])
|
||||
|
||||
if not response.isError():
|
||||
#modbus_data = response.registers[0]
|
||||
modbus_data = response.bits[0]
|
||||
print(modbus_data)
|
||||
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
threading.Thread(target=poll_modbus, daemon=True).start()
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/modbus_data")
|
||||
async def get_modbus_data():
|
||||
return {"modbus_data": int(modbus_data)}
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Welcome to FastAPI with Modbus"}
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
uvicorn.run(app, host="0.0.0.0", port=12345)
|
||||
|
@ -15,14 +15,15 @@ void setup() {
|
||||
;
|
||||
|
||||
mb.config (9600);
|
||||
mb.addIsts(0);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// mb.setIsts(0, digitalRead(pinToRead));
|
||||
// mb.task();
|
||||
int r = Serial.read();
|
||||
if (r >= 0){
|
||||
Serial.write(r);
|
||||
Serial.write(r);
|
||||
}
|
||||
mb.setIsts(0, digitalRead(pinToRead));
|
||||
mb.task();
|
||||
// int r = Serial.read();
|
||||
// if (r >= 0){
|
||||
// Serial.write(r);
|
||||
// Serial.write(r);
|
||||
// }
|
||||
}
|
Loading…
Reference in New Issue
Block a user