From 868daf94f29ce1ffdd799a68c07bb668cd373bcd Mon Sep 17 00:00:00 2001 From: HP\李良庭 <liliangting@lanpucloud.cn:1111> Date: 星期二, 08 七月 2025 11:49:03 +0800 Subject: [PATCH] 提交分辨率自适应版本V3.1.0.1500 --- bin/core/ActuatorDemo.cpp | 429 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 429 insertions(+), 0 deletions(-) diff --git a/bin/core/ActuatorDemo.cpp b/bin/core/ActuatorDemo.cpp new file mode 100644 index 0000000..032b069 --- /dev/null +++ b/bin/core/ActuatorDemo.cpp @@ -0,0 +1,429 @@ +锘�// ActuatorDemo.cpp : 姝ゆ枃浠跺寘鍚� "main" 鍑芥暟銆傜▼搴忔墽琛屽皢鍦ㄦ澶勫紑濮嬪苟缁撴潫銆� +// + +#include <iostream> +#include <thread> +#include <windows.h> +#include "../Actuator//Actuator.h" + +/******************* 涓篜LC鑷姩鐩戞祴鍑嗗鐩稿叧鍏ㄩ儴鍙橀噺******************************** */ +static bool plcScan = true; +static bool devRunning = false; +/************************************************************************************* +**************************************************************************************/ + + +//绯绘暟 +const double coef[5] = { -6.204673384923,0.025161776479,-0.000014765722,0.000000003440,0 }; +const char sPortName[] = "COM6"; + +//鎵嬪姩鍐欑伯椋熷搧绉� +void ManReady() +{ + if (devRunning){printf("姘村垎浠繖!"); return;} + + devRunning = true; + void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1); + if (hWrDev) + { + int ret = ConnectDevice(hWrDev); + + if (ret >= 0) + { + + ret = SendCommands(hWrDev, WR_READY, 0x01, 0, 500000); //涓嬪彂纭 + + if (ret <= 0) + printf("鍑嗗澶辫触锛乗n"); + else + { + printf("鍑嗗灏辩华锛孫K锛乗n"); + } + } + + Disconnect(hWrDev); + DestroyDevice(hWrDev); + } + + devRunning = false; +} + +//鎵嬪姩妫�娴� +void ManTest() +{ + if (devRunning) { printf("姘村垎浠繖!"); return; } + + devRunning = true; + + void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1); + if (hWrDev) + { + int ret = ConnectDevice(hWrDev); + + if (ret >= 0) + { + ret = SendCommands(hWrDev, WR_DETECT, 0, 60, 0); //鎵ц鍚姩鍛戒护 + + if (ret <= 0) + { + printf("鎵ц妫�娴嬪け璐ワ紒\n"); + } + else + { + unsigned short nDatas[10] = { 0 }; + + //浠庢按鍒嗕华鍣ㄨ鍙栨暟鎹� + ret = ReadDatas(hWrDev, nDatas); + + //绗竴涓�间负鐢靛钩鍊硷紝鐢ㄦ潵璁$畻姘村垎鐜� + double v = coef[0] + + coef[1] * nDatas[0] + + coef[2] * nDatas[0] * nDatas[0] + + coef[3] * nDatas[0] * nDatas[0] * nDatas[0] + + coef[4] * nDatas[0] * nDatas[0] * nDatas[0] * nDatas[0]; + + v = CalcValue(nDatas[0], coef[0], coef[1], coef[2], coef[3], coef[4]); + + printf(" After Detection,"); + printf(" %d %d %d %d", nDatas[0], nDatas[1], nDatas[2], nDatas[3]); + printf(" Water Rate:%lf", v); + printf(" \n"); + printf("妫�娴嬫墽琛孫K!\n"); + + } + } + + Disconnect(hWrDev); + DestroyDevice(hWrDev); + } + + devRunning = false; +} + +//鎵嬪姩澶嶄綅 +void ManReset() +{ + if (devRunning) { printf("姘村垎浠繖!"); return; } + + devRunning = true; + + void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1); + + if (hWrDev) + { + int ret = ConnectDevice(hWrDev); + + if (ret >= 0) + { + ret = SendCommands(hWrDev, WR_RESET, 0, 15, 0); //涓嬪彂澶嶄綅 + + if (ret <= 0) + { + printf("澶嶄綅澶辫触锛乗n"); + } + else + { + printf("澶嶄綅鎴愬姛锛乗n"); + } + } + + Disconnect(hWrDev); + DestroyDevice(hWrDev); + } + + devRunning = false; +} + +void GetWeight() +{ + if (devRunning) { printf("姘村垎浠繖!"); return; } + + devRunning = true; + + void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1); + + if (hWrDev) + { + + int ret = ConnectDevice(hWrDev); + + + unsigned short nWeight = 0; + + if (ret >= 0) + { + //浠庢按鍒嗕华鍣ㄨ鍙栨暟鎹� + ret = ReadWeight(hWrDev, &nWeight); + + if (ret >= 0) + { + printf(" Weight Val: %d", nWeight); + printf(" \n"); + printf("绉伴噸鎵цOK!\n"); + } + else + { + printf("绉伴噸鎵ц澶辫触!\n"); + } + } + + Disconnect(hWrDev); + DestroyDevice(hWrDev); + } + + devRunning = false; +} + +void CurrVersion() +{ + if (devRunning) { printf("姘村垎浠繖!"); return; } + + devRunning = true; + + void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1); + if (hWrDev) + { + int ret = ConnectDevice(hWrDev); + + //unsigned char nVersions[2] = { 0 }; + unsigned short nVersions; + + if (ret >= 0) + { + //浠庢按鍒嗕华鍣ㄨ鍙栨暟鎹� + ret = ReadVersion(hWrDev, &nVersions); + + if (ret >= 0) + { + printf(" Version1 Val: %d", nVersions & 0x00FF); + printf(" \n"); + printf(" Version2 Val: %d", nVersions >> 8); + printf(" \n"); + } + else + { + printf("鐗堟湰鎵ц澶辫触!\n"); + } + } + + Disconnect(hWrDev); + DestroyDevice(hWrDev); + } + + devRunning = false; +} + +void ExecPlcReady(void* hPlc, int nGrainType) +{ + if (devRunning) { printf("姘村垎浠繖!"); return; } + + devRunning = true; + + void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1); + if (hWrDev) + { + int ret = ConnectDevice(hWrDev); + + if (ret >= 0) + { + //鍚戞按鍒嗕华鍙戠伯绉嶆洿鏀癸紝鍚姩鍑嗗 + ret = SendCommands(hWrDev, WR_READY, nGrainType, 0, 500000); + if (ret < 0) + { + printf("绮鍝佺淇敼澶辫触锛佹按鍒嗕华鏁呴殰锛乗n"); + ret = ResponseState(hPlc, DEVICE_WR_FAULT); + } + else + { + printf("绮鍝佺淇敼鎴愬姛锛佸噯澶囧氨缁猏n"); + ret = ResponseState(hPlc, READY_FINISH); + } + } + + Disconnect(hWrDev); + DestroyDevice(hWrDev); + } + + devRunning = false; +} +void ExecPlcDetect(void* hPlc, int nTimeout /*浠ョ璁�*/) +{ + if (devRunning) { printf("姘村垎浠繖!"); return; } + + devRunning = true; + + void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1); + if (hWrDev && hPlc) + { + int ret = ConnectDevice(hWrDev); + + if (ret >= 0) + { + ret = SendCommands(hWrDev, WR_DETECT, 0, nTimeout, 0); + if (ret <= 0) + { + printf("鎵ц妫�娴嬪け璐ワ紒\n"); + ret = ResponseState(hPlc, DEVICE_WR_FAULT); + } + else + { + unsigned short nDatas[10] = { 0 }; + unsigned short nSendData[3] = { 0 }; + + //浠庢按鍒嗕华鍣ㄨ鍙栨暟鎹� + ret = ReadDatas(hWrDev, nDatas); + + printf("鎵ц妫�娴嬫垚鍔燂紒\n"); + ret = ResponseState(hPlc, DETECT_FINISH); + + //璁$畻姘村垎鐜� + double v = coef[0] + + coef[1] * nDatas[0] + + coef[2] * nDatas[0] * nDatas[0] + + coef[3] * nDatas[0] * nDatas[0] * nDatas[0] + + coef[4] * nDatas[0] * nDatas[0] * nDatas[0] * nDatas[0]; + + + nSendData[0] = 0x01; // 绮鍝佺 + nSendData[1] = 100 * v; // 21.30% //鍚按鐜� + nSendData[2] = 100 * nDatas[1] / 0.52; // 51.50; //瀹归噸姣� + + printf("Get Vals: wr:%f, weight:%f, temperatur:%f, humidity:%f", + v, + nDatas[1], + nDatas[2] / 100.f, + nDatas[3] / 100.f); + + ret = UploadData(hPlc, nSendData); + } + + } + + Disconnect(hWrDev); + + DestroyDevice(hWrDev); + } + + devRunning = false; +} +void ExecPlcReset(void* hPlc, int nTimeout /*浠ョ璁�*/) +{ + if (devRunning) { printf("姘村垎浠繖!"); return; } + + devRunning = true; + + void* hWrDev = CreateWrDevice(sPortName, 115200, 'N', 8, 1); + if (hWrDev) + { + int ret = ConnectDevice(hWrDev); + + if (ret >= 0) + { + //鍚戞按鍒嗕华鍙戠伯绉嶆洿鏀癸紝鍚姩鍑嗗 + ret = SendCommands(hWrDev, WR_RESET, 0, nTimeout, 0); + if (ret <= 0) + { + printf("澶嶄綅澶辫触锛乗n"); + ret = ResponseState(hPlc, DEVICE_WR_FAULT); + } + else + { + printf("澶嶄綅鎴愬姛!\n"); + } + } + + Disconnect(hWrDev); + DestroyDevice(hWrDev); + } + + devRunning = false; +} + +static void ExecPLCCommand() +{ + + unsigned short nPlcCommandState = 0; + unsigned short nPlcCommandGrainType = 0; + float fPlcCommandDrift = 0; + + bool ret = -1; + void* hPlcDev = CreatePlcDevice("127.0.0.1", 1502); + if (hPlcDev) ret = ConnectDevice(hPlcDev); + + while (plcScan) //鎸� c 閫�鍑鸿嚜鍔ㄦ娴� + { + ret = ReadState(hPlcDev, &nPlcCommandState, &nPlcCommandGrainType); + if (ret <= 0) + { + printf("璇诲彇PLC鐘舵�佸瓧寮傚父锛乗n"); + } + else + { + //璇籔LC鐘舵�佸瓧 + if (nPlcCommandGrainType > 0 && nPlcCommandState > 0) + { + //涓嬮潰鏄富娴佺▼鐨勫鐞� + switch (nPlcCommandState) + { + case READY_GRAIN: //绮鍝佺淇敼浜� + ExecPlcReady(hPlcDev, nPlcCommandGrainType); + break; + case DETECT_START: //寮�濮嬫祴璇� + ExecPlcDetect(hPlcDev, 60); + break; + case DEVICE_WR_RESET: //澶嶄綅鍛戒护 + ExecPlcReset(hPlcDev, 10); + break; + } + } + } + /*涓�绉掕鍙朠LC涓�娆′俊鎭�*/ + Sleep(1000); + } + +} + +int main() +{ + + std::cout << "姘村垎浠祴璇曠▼搴忓凡寮�鍚�!\n"; + + char ch = 0; + while ('x' != ( ch = getchar() )) //鎸� x 閫�鍑虹▼搴� + { + switch (ch) + { + case 'a': //鑷姩娴嬭瘯 + { + std::thread workThread = std::thread(ExecPLCCommand); + workThread.detach(); + } + break; + case 'r': //鎵嬪姩妫�娴�,闇�瑕佸噯澶� + ManReady(); + break; + case 'd': //鎵嬪姩妫�娴嬶紝涓嶈鍑嗗 + ManTest(); + break; + case 'z': //澶嶄綅娓呴浂 + ManReset(); + break; + case 'w': + GetWeight(); + break; + case 'V': + CurrVersion(); + break; + default: + break; + } + + } + + return 1; +} + + + + + -- Gitblit v1.9.3