[WIP] пытаемся modbus
This commit is contained in:
parent
8a6bfbc62c
commit
12d647d68a
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": "COM6"
|
"port": "COM4"
|
||||||
}
|
}
|
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@ -18,6 +18,8 @@
|
|||||||
"includePath": [
|
"includePath": [
|
||||||
"C:\\Users\\dimae\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino",
|
"C:\\Users\\dimae\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\cores\\arduino",
|
||||||
"C:\\Users\\dimae\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard",
|
"C:\\Users\\dimae\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\avr\\1.8.6\\variants\\standard",
|
||||||
|
"C:\\Users\\dimae\\OneDrive\\Документы\\Arduino\\libraries\\Modbus-Serial\\src",
|
||||||
|
"C:\\Users\\dimae\\OneDrive\\Документы\\Arduino\\libraries\\Modbus-Arduino\\src",
|
||||||
"c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\lib\\gcc\\avr\\7.3.0\\include",
|
"c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\lib\\gcc\\avr\\7.3.0\\include",
|
||||||
"c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\lib\\gcc\\avr\\7.3.0\\include-fixed",
|
"c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\lib\\gcc\\avr\\7.3.0\\include-fixed",
|
||||||
"c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\avr\\include"
|
"c:\\users\\dimae\\appdata\\local\\arduino15\\packages\\arduino\\tools\\avr-gcc\\7.3.0-atmel3.6.1-arduino7\\avr\\include"
|
||||||
|
24
main.py
24
main.py
@ -1,20 +1,29 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
import serial
|
from pymodbus.client import ModbusSerialClient as ModbusClient
|
||||||
import uvicorn
|
import uvicorn
|
||||||
import threading
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
|
|
||||||
ser = serial.Serial('COM6', 9600)
|
# Create a Modbus client instance
|
||||||
|
client = ModbusClient(method='rtu', port='COM4', baudrate=9600, timeout=1)
|
||||||
|
client.connect()
|
||||||
|
|
||||||
|
# Shared variable to store the Modbus register value
|
||||||
line = '0'
|
line = '0'
|
||||||
|
|
||||||
def read_value():
|
def read_modbus_value():
|
||||||
global line
|
global line
|
||||||
while True:
|
while True:
|
||||||
if ser.in_waiting > 0:
|
# Read holding register (adjust the register address as needed)
|
||||||
line = ser.read().decode()
|
result = client.read_holding_registers(address=1, count=1, unit=1)
|
||||||
|
if not result.isError():
|
||||||
|
line = str(result.registers[0])
|
||||||
|
time.sleep(1) # Poll every 1 second
|
||||||
|
|
||||||
threading.Thread(target=read_value, daemon=True).start()
|
# Start the Modbus reading in a separate thread
|
||||||
|
threading.Thread(target=read_modbus_value, daemon=True).start()
|
||||||
|
|
||||||
@app.get("/stat")
|
@app.get("/stat")
|
||||||
async def get_stat():
|
async def get_stat():
|
||||||
@ -24,4 +33,5 @@ async def get_stat():
|
|||||||
async def root():
|
async def root():
|
||||||
return {'message': 'hello world'}
|
return {'message': 'hello world'}
|
||||||
|
|
||||||
uvicorn.run(app, host="0.0.0.0", port=12346)
|
if __name__ == "__main__":
|
||||||
|
uvicorn.run(app, host="0.0.0.0", port=12346)
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
|
#include <ModbusSerial.h>
|
||||||
|
|
||||||
|
// Define pins
|
||||||
#define PIN_INPUT 2
|
#define PIN_INPUT 2
|
||||||
#define PIN_LED 13
|
#define PIN_LED 13
|
||||||
|
|
||||||
|
// Create Modbus object
|
||||||
|
ModbusSerial mb(Serial, 1); // Assuming slave ID is 1
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
pinMode(PIN_INPUT, INPUT);
|
pinMode(PIN_INPUT, INPUT);
|
||||||
@ -9,6 +15,6 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
int sensorVal = digitalRead(PIN_INPUT);
|
int sensorVal = digitalRead(PIN_INPUT);
|
||||||
Serial.print(sensorVal); // Выводим значение в мониторе порта 3
|
mb.task(); // Update Modbus communication
|
||||||
delay(500);
|
delay(500);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user