HP\李良庭
2025-07-08 868daf94f29ce1ffdd799a68c07bb668cd373bcd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
// ActuatorDemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
 
#include <iostream>
#include <thread>
#include <windows.h>
#include "../Actuator//Actuator.h"
 
/******************* 为PLC自动监测准备相关全部变量******************************** */
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("准备就绪,OK!\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("检测执行OK!\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
        {
            //读PLC状态字
            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;
                }
            }
        }
        /*一秒读取PLC一次信息*/
        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;
}