14.06 проверка на другом компе
This commit is contained in:
parent
803fef1412
commit
992b620014
2
.vscode/arduino.json
vendored
2
.vscode/arduino.json
vendored
@ -2,5 +2,5 @@
|
|||||||
"sketch": "project\\project.ino",
|
"sketch": "project\\project.ino",
|
||||||
"output": "build",
|
"output": "build",
|
||||||
"board": "arduino:avr:uno",
|
"board": "arduino:avr:uno",
|
||||||
"port": "COM4"
|
"port": "COM6"
|
||||||
}
|
}
|
77
main.py
77
main.py
@ -1,37 +1,46 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from pymodbus.client import ModbusSerialClient as ModbusClient
|
from pymodbus.client import ModbusSerialClient
|
||||||
import uvicorn
|
import threading
|
||||||
import threading
|
import time
|
||||||
import time
|
import logging
|
||||||
|
|
||||||
app = FastAPI()
|
serial_port = "COM6"
|
||||||
|
baud_rate = 9600
|
||||||
|
modbus_client = ModbusSerialClient(serial_port, baudrate=baud_rate)
|
||||||
|
modbus_data = "0"
|
||||||
|
|
||||||
# Create a Modbus client instance
|
logging.basicConfig()
|
||||||
client = ModbusClient(method='rtu', port='COM4', baudrate=9600, timeout=1)
|
log = logging.getLogger()
|
||||||
client.connect()
|
log.setLevel(logging.DEBUG)
|
||||||
|
|
||||||
# Shared variable to store the Modbus register value
|
def poll_modbus():
|
||||||
line = '0'
|
global modbus_data
|
||||||
|
modbus_client.connect()
|
||||||
def read_modbus_value():
|
while True:
|
||||||
global line
|
try:
|
||||||
while True:
|
response = modbus_client.read_discrete_inputs(10001, 1, slave=1)
|
||||||
# Read holding register (adjust the register address as needed)
|
#print(response)
|
||||||
result = client.read_holding_registers(address=1, count=1, unit=1)
|
|
||||||
if not result.isError():
|
if not response.isError():
|
||||||
line = str(result.registers[0])
|
modbus_data = response.registers[0]
|
||||||
time.sleep(1) # Poll every 1 second
|
print(modbus_data)
|
||||||
|
except:
|
||||||
# Start the Modbus reading in a separate thread
|
pass
|
||||||
threading.Thread(target=read_modbus_value, daemon=True).start()
|
|
||||||
|
|
||||||
@app.get("/stat")
|
|
||||||
async def get_stat():
|
threading.Thread(target=poll_modbus, daemon=True).start()
|
||||||
return {'state': line}
|
|
||||||
|
app = FastAPI()
|
||||||
@app.get("/")
|
|
||||||
async def root():
|
@app.get("/modbus_data")
|
||||||
return {'message': 'hello world'}
|
async def get_modbus_data():
|
||||||
|
return {"modbus_data": modbus_data}
|
||||||
if __name__ == "__main__":
|
|
||||||
uvicorn.run(app, host="0.0.0.0", port=12346)
|
@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)
|
@ -1,20 +1,28 @@
|
|||||||
#include <ModbusSerial.h>
|
#include <ModbusSerial.h>
|
||||||
|
const int GPIOCoil = 0;
|
||||||
|
|
||||||
// Define pins
|
#define MySerial Serial // define serial port used, Serial most of the time, or Serial1, Serial2 ... if available
|
||||||
#define PIN_INPUT 3
|
|
||||||
#define PIN_LED 13
|
bool coils[10];
|
||||||
|
int pinToRead = 2;
|
||||||
// Create Modbus object
|
|
||||||
ModbusSerial mb(Serial, 1); // Assuming slave ID is 1
|
ModbusSerial mb (MySerial, 1);
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
|
||||||
pinMode(PIN_INPUT, INPUT);
|
|
||||||
pinMode(PIN_LED, OUTPUT);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
MySerial.begin (9600, MB_PARITY_NONE); // prefer this line in accordance with the modbus standard.
|
||||||
int sensorVal = digitalRead(PIN_INPUT);
|
while (! MySerial)
|
||||||
mb.task(); // Update Modbus communication
|
;
|
||||||
delay(500);
|
|
||||||
|
mb.config (9600);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
// 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