From 38f3d606d022db797fafc9c4fbd749f91787e840 Mon Sep 17 00:00:00 2001 From: tim33824 Date: Thu, 14 Aug 2025 16:31:26 +0800 Subject: [PATCH] 123 --- .vscode/.vscode/tasks.json | 20 ++++++++++++ .vscode/c_cpp_properties.json | 16 ++++++++++ .vscode/launch.json | 44 ++++++++++++++++++++++++++ .vscode/settings.json | 59 +++++++++++++++++++++++++++++++++++ MQTT_to_PLC.py | 50 +++++++++++++++++++++++++++++ PLC_to_Web.py | 25 +++++++++++++++ Web.py | 47 ++++++++++++++++++++++++++++ cl_py_test.py | 11 +++++++ list_ip.py | 8 +++++ py_test.py | 6 ++-- pyshark.py | 56 +++++++++++++++++++++++++++++++++ templates/index.html | 47 ++++++++++++++++++++++++++++ test.c | 31 ++++++++++++++++++ 13 files changed, 416 insertions(+), 4 deletions(-) create mode 100644 .vscode/.vscode/tasks.json create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 MQTT_to_PLC.py create mode 100644 PLC_to_Web.py create mode 100644 Web.py create mode 100644 cl_py_test.py create mode 100644 list_ip.py create mode 100644 pyshark.py create mode 100644 templates/index.html create mode 100644 test.c diff --git a/.vscode/.vscode/tasks.json b/.vscode/.vscode/tasks.json new file mode 100644 index 0000000..d20e82d --- /dev/null +++ b/.vscode/.vscode/tasks.json @@ -0,0 +1,20 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build C", + "type": "shell", + "command": "C:/msys64/mingw64/bin/gcc.exe", + "args": [ + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}.exe" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": ["$gcc"] + } + ] +} diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..cb9ae4f --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "windows-gcc-x64", + "includePath": [ + "${workspaceFolder}/**", + "C:/msys64/mingw64/include" + ], + "compilerPath": "C:/msys64/mingw64/bin/gcc.exe", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "windows-gcc-x64" + } + ], + "version": 4 +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..f9c0a72 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,44 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C Launch", + "type": "cppdbg", + "request": "launch", + "program": "${fileDirname}/${fileBasenameNoExtension}.exe", + "args": [], + "stopAtEntry": false, + "cwd": "${fileDirname}", + "environment": [], + "externalConsole": true, + "MIMode": "gdb", + "miDebuggerPath": "C:/msys64/mingw64/bin/gdb.exe", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + }, + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": true, + "cwd": "c:/Users/huiting/Desktop/py_document", + "program": "c:/Users/huiting/Desktop/py_document/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bb879da --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "C:/Program Files/Microsoft Visual Studio/VR_NR/Community/VC/Auxiliary/Build/vcvarsall.bat", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file diff --git a/MQTT_to_PLC.py b/MQTT_to_PLC.py new file mode 100644 index 0000000..881980c --- /dev/null +++ b/MQTT_to_PLC.py @@ -0,0 +1,50 @@ +# pip install paho-mqtt +import json +import time +from paho.mqtt import client as mqtt + +BROKER_HOST = "169.254.11.130" +BROKER_PORT = 1886 +TOPIC = "topic_plc_and_py_for_AXIS" + +# 建議測試用 QoS=1;retain=False +QOS = 1 +RETAIN = False + +def main(): + client = mqtt.Client(client_id="py_pub_plc_test", protocol=mqtt.MQTTv311) + + # 若 Broker 有帳密、TLS 在這裡設定(目前不需要) + # client.username_pw_set("user", "pass") + # client.tls_set(ca_certs="ca.pem") # 若是 TLS + + client.connect(BROKER_HOST, BROKER_PORT, keepalive=60) + client.loop_start() + time.sleep(0.3) # 等連線建立 + + # ---- 第一組: ---- + payload_forward = { + "Move_Forward": False, # BOOL + "Move_Forward_Velocity": 6.2 # REAL (float) + } + msg1 = json.dumps(payload_forward, ensure_ascii=False) + r1 = client.publish(TOPIC, msg1, qos=QOS, retain=RETAIN) + r1.wait_for_publish() + + time.sleep(3)#第二顆馬達等等再開啟 + + # ---- 第二組: ---- + payload_updown = { + "Move_UPDown": False, # BOOL + "Move_UPDown_Velocity": 5.1 # REAL (float) + } + msg2 = json.dumps(payload_updown, ensure_ascii=False) + r2 = client.publish(TOPIC, msg2, qos=QOS, retain=RETAIN) + r2.wait_for_publish() + + time.sleep(0.3) + client.loop_stop() + client.disconnect() + +if __name__ == "__main__": + main() diff --git a/PLC_to_Web.py b/PLC_to_Web.py new file mode 100644 index 0000000..aa5a0e0 --- /dev/null +++ b/PLC_to_Web.py @@ -0,0 +1,25 @@ +import asyncio +from pymodbus.server import StartAsyncTcpServer +from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext +from pymodbus.datastore.store import ModbusSequentialDataBlock + +# 建立 HR 暫存區,從位址 1633 開始,總共 100 筆 +store = ModbusSlaveContext( + hr=ModbusSequentialDataBlock(1633, [0]*100) +) +context = ModbusServerContext(slaves={255: store}, single=False) + +async def read_loop(): + while True: + # 從 HR address 1633 開始,讀取 3 筆資料 + values = context[255].getValues(3, 1633, count=3) + print(f"PLC 寫入的資料: {values}") + await asyncio.sleep(1) # 每秒顯示一次 + +async def run_server(): + asyncio.create_task(read_loop()) # 啟動背景讀取任務 + await StartAsyncTcpServer(context, address=("169.254.11.130", 502)) + + +if __name__ == "__main__": + asyncio.run(run_server()) diff --git a/Web.py b/Web.py new file mode 100644 index 0000000..fa5c9bb --- /dev/null +++ b/Web.py @@ -0,0 +1,47 @@ +import struct +import time +import threading +from pymodbus.client import ModbusTcpClient +from flask import Flask, jsonify + +app = Flask(__name__) + +modbus_data = {'rpm': 0, 'ai': 0, 'analog': 0} # 全域變數儲存資料 + +def read_modbus_data(): + client = ModbusTcpClient(host="169.254.11.130", port=502) + if client.connect(): + result = client.read_holding_registers(address=1633, count=6, slave=255) + client.close() + if result.isError(): + return {'rpm': 'Error', 'ai': 'Error', 'analog': 'Error'} + else: + regs = result.registers + def to_float32(low, high): + return struct.unpack(' + + + + Modbus 即時資料 + + + +

📊 Modbus 即時資料

+

RPM (地址1633): 載入中...

+

AI 電壓 (地址1635): 載入中...

+

類比訊號值 (地址1637): 載入中...

+ + + + + +""" + +@app.route("/") +def index(): + return render_template_string(html_page) diff --git a/test.c b/test.c new file mode 100644 index 0000000..2d090e0 --- /dev/null +++ b/test.c @@ -0,0 +1,31 @@ +#include +#include + +typedef struct point { + int x; + int y; + char *number; + struct point *ptr_for_go_next; +} point_t; + +int main() { + point_t p2 = { + .x = 20, + .y = 40, + .number = "number_2", + .ptr_for_go_next = NULL + }; + + point_t p1 = { + .x = 10, + .y = 20, + .number = "number_1", + .ptr_for_go_next = &p2 + }; + + printf("x: %d\n", p1.x); + printf("y: %d\n", p1.y); + printf("number: %s\n", p1.number); + + return 0; +}