forked from thestealth131205/vfdmod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
743 lines (615 loc) · 25 KB
/
main.cpp
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#include <math.h>
#include <getopt.h>
#include <signal.h>
#include <modbus/modbus-rtu.h>
#include <QVector>
#include <QFileInfo>
#include "structures.h"
#include "default-values.h"
const char *github="Sources: https://github.com/aekhv/vfdmod/\n";
const char *copyright = "2020 (c) Alexander E. (Khabarovsk, Russia)\n";
int checkFlag, debugFlag, exitFlag, okCounter, serialDeviceIsOpened;
QString exeName;
hal_main_data_t *hal_mdata;
hal_user_data_t **hal_udata;
const char *short_options = "cdhnvPV";
const struct option long_options[] = {
{"check", no_argument, 0, 'c'},
{"debug", no_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{"new", no_argument, 0, 'n'},
{"version", no_argument, 0, 'v'},
{"postgui", no_argument, 0, 'P'},
{"pyvcp", no_argument, 0, 'V'},
{0, 0, 0, 0}
};
int load_config(const QString &inifile, main_config_t &mconfig, QVector<user_config_t> &uconfig);
void make_blank_config();
void make_postgui_config(const main_config_t &mconfig, const QVector<user_config_t> &uconfig);
void make_pyvcp_config(const QString &inifile, const main_config_t &mconfig, const QVector<user_config_t> &uconfig);
void print_help()
{
printf("\nUsage:\n"
"\t%s [--debug] [--check|--postgui|--pyvcp] CONFIGFILE\n"
"\t%s --new\n",
qPrintable(exeName),
qPrintable(exeName));
printf("\nKeys:\n"
"\t-c, --check\tCheck config file for errors.\n"
"\t-d, --debug\tEnable debug mode.\n"
"\t-h, --help\tPrint this help.\n"
"\t-n, --new\tMake a blank config file.\n"
"\t-P, --postgui\tMake postgui HAL file from existing config file.\n"
"\t-V, --pyvcp\tMake PyVcp XML file from existing config file.\n"
"\t-v, --version\tPrint program's version.\n"
"\tCONFIGFILE\tPlain text INI file.\n");
printf("\nDescription:\n"
"\tBlah-blah and blah-blah-blah...\n");
printf("\nExamples:\n"
"\t%s --new > config.ini\n"
"\t%s --check config.ini\n"
"\t%s --postgui config.ini > postgui.hal\n"
"\t%s --pyvcp config.ini > my-pyvcp-panel.xml\n"
"\t%s config.ini\n",
qPrintable(exeName),
qPrintable(exeName),
qPrintable(exeName),
qPrintable(exeName),
qPrintable(exeName));
}
void protocol_delay(const rs485_config_t &cfg)
{
int startBit = 1;
if (cfg.parity != "N")
startBit++;
long ns = (1000000000l / cfg.baudRate) * cfg.protocolDelay * (startBit + cfg.dataBits + cfg.stopBits);
struct timespec loop_timespec = {0, ns};
nanosleep(&loop_timespec, NULL);
}
void delay_ms(const int &t)
{
long s = t / 1000;
long ns = (t % 1000) * 1000000l;
struct timespec loop_timespec = {s, ns};
nanosleep(&loop_timespec, NULL);
}
void closeRequest(int param) {
Q_UNUSED(param)
exitFlag = 1;
printf("%s: close request received.\n", qPrintable(exeName));
}
int read_modbus_data(modbus_t *ctx, main_config_t &mconfig, QVector<user_config_t> &uconfig)
{
uint16_t value;
/* Reading spindle output speed */
if (debugFlag)
printf("\n%s: reading '%s.%s' from address %d (0x%04X)...\n",
qPrintable(exeName),
HAL_GROUP_SPINDLE,
HAL_PIN_RPM_OUT,
mconfig.rpmOut.address,
mconfig.rpmOut.address);
protocol_delay(mconfig.rs485);
if (1 != modbus_read_registers(ctx, mconfig.rpmOut.address, 1, &value))
goto fail;
if (debugFlag)
printf("%s: returned value is %d (0x%04X)\n", qPrintable(exeName), value, value);
okCounter++;
*hal_mdata->spindleRpmOut = double(value) * mconfig.rpmOut.multiplier / mconfig.rpmOut.divider;
/* Reading user parameters */
for (int i = 0; i < uconfig.count(); ++i) {
if (debugFlag)
printf("\n%s: reading '%s.%s' from address %d (0x%04X)...\n",
qPrintable(exeName),
HAL_GROUP_USER_PARAMETERS,
qPrintable(uconfig.at(i).pinName),
uconfig.at(i).address,
uconfig.at(i).address);
protocol_delay(mconfig.rs485);
if (uconfig.at(i).functionCode == MODBUS_FUNC_READ_MULTIPLE_COILS) {
uint8_t bit_value = 0;
if (1 != modbus_read_bits(ctx, uconfig.at(i).address, 1, &bit_value))
goto fail;
if (debugFlag)
printf("%s: returned value is %s\n",
qPrintable(exeName),
bit_value != 0 ? "TRUE" : "FALSE");
if (bit_value != 0) {
*hal_udata[i]->bitPin = 1;
*hal_udata[i]->bitNotPin = 0;
} else {
*hal_udata[i]->bitPin = 0;
*hal_udata[i]->bitNotPin = 1;
}
}
if (uconfig.at(i).functionCode == MODBUS_FUNC_READ_MULTIPLE_HOLDING_REGISTERS) {
if (1 != modbus_read_registers(ctx, uconfig.at(i).address, 1, &value))
goto fail;
if (debugFlag)
printf("%s: returned value is %d (0x%04X)\n", qPrintable(exeName), value, value);
switch (uconfig.at(i).pinType) {
case HAL_FLOAT:
*hal_udata[i]->floatPin = double(value) * uconfig.at(i).multiplier / uconfig.at(i).divider;
break;
case HAL_S32:
*hal_udata[i]->s32Pin = value * uconfig.at(i).multiplier / uconfig.at(i).divider;
break;
case HAL_U32:
*hal_udata[i]->u32Pin = value * uconfig.at(i).multiplier / uconfig.at(i).divider;
break;
case HAL_BIT:
if ((value & uconfig.at(i).bitMask) != 0) {
*hal_udata[i]->bitPin = 1;
*hal_udata[i]->bitNotPin = 0;
} else {
*hal_udata[i]->bitPin = 0;
*hal_udata[i]->bitNotPin = 1;
}
break;
default:
fprintf(stderr, "%s: incorrect HAL pin type!\n", qPrintable(exeName));
}
}
okCounter++;
}
return 0;
fail:
*hal_mdata->lastError = errno;
(*hal_mdata->errorCount)++;
okCounter = 0;
return -1;
}
int write_modbus_data(modbus_t *ctx, main_config_t &mconfig)
{
int result = 0;
uint16_t value;
/* Setting command speed */
hal_float_t speed = abs(*hal_mdata->spindleRpmIn);
if (speed < mconfig.common.minSpeedRpm)
speed = mconfig.common.minSpeedRpm;
if (speed > mconfig.common.maxSpeedRpm)
speed = mconfig.common.maxSpeedRpm;
speed = speed * mconfig.rpmIn.multiplier / mconfig.rpmIn.divider;
value = int(speed);
if (debugFlag)
printf("\n%s: setting command speed value to %d (0x%04X)...\n", qPrintable(exeName), value, value);
protocol_delay(mconfig.rs485);
// Function code 0x06
if (mconfig.rpmIn.functionCode == MODBUS_FUNC_WRITE_SINGLE_HOLDING_REGISTER)
result = modbus_write_register(ctx, mconfig.rpmIn.address, value);
// Function code 0x10
if (mconfig.rpmIn.functionCode == MODBUS_FUNC_WRITE_MULTIPLE_HOLDING_REGISTERS)
result = modbus_write_registers(ctx, mconfig.rpmIn.address, 1, &value);
if (result != 1)
goto fail;
else
okCounter++;
/* Setting control word value for function codes 0x06 & 0x10 */
if ((mconfig.control.functionCode == MODBUS_FUNC_WRITE_SINGLE_HOLDING_REGISTER)
|| (mconfig.control.functionCode == MODBUS_FUNC_WRITE_MULTIPLE_HOLDING_REGISTERS)) {
value = mconfig.control.stopValue;
if (0 != *hal_mdata->runReverse)
value = mconfig.control.runRevValue;
if (0 != *hal_mdata->runForward)
value = mconfig.control.runFwdValue;
if ((0 != *hal_mdata->faultReset)
&& (mconfig.control.faultResetValue != INACTIVE_FLAG))
value = mconfig.control.faultResetValue;
if (debugFlag)
printf("\n%s: setting control word value to %d (0x%04X)...\n", qPrintable(exeName), value, value);
protocol_delay(mconfig.rs485);
if (mconfig.control.functionCode == MODBUS_FUNC_WRITE_SINGLE_HOLDING_REGISTER)
result = modbus_write_register(ctx, mconfig.control.address, value);
if (mconfig.control.functionCode == MODBUS_FUNC_WRITE_MULTIPLE_HOLDING_REGISTERS)
result = modbus_write_registers(ctx, mconfig.control.address, 1, &value);
if (result != 1)
goto fail;
else
okCounter++;
// If fault reset has been written successfully then inactive it's input
if (value == mconfig.control.faultResetValue)
*hal_mdata->faultReset = 0;
}
// Setting coils for function codes 0x05 & 0x0F
if ((mconfig.control.functionCode == MODBUS_FUNC_WRITE_SINGLE_COIL)
|| (mconfig.control.functionCode == MODBUS_FUNC_WRITE_MULTIPLE_COILS)) {
uint8_t bit_value = 0;
// Direction coil
bit_value = *hal_mdata->runReverse != 0 ? 1 : 0;
if (debugFlag)
printf("\n%s: setting direction coil to %s...\n",
qPrintable(exeName),
bit_value != 0 ? "TRUE" : "FALSE");
protocol_delay(mconfig.rs485);
if (mconfig.control.functionCode == MODBUS_FUNC_WRITE_SINGLE_COIL)
result = modbus_write_bit(ctx, mconfig.control.directionCoil, bit_value);
if (mconfig.control.functionCode == MODBUS_FUNC_WRITE_MULTIPLE_COILS)
result = modbus_write_bits(ctx, mconfig.control.directionCoil, 1, &bit_value);
if (result != 1)
goto fail;
else
okCounter++;
// Run coil
bit_value = ((*hal_mdata->runForward != 0) || (*hal_mdata->runReverse != 0)) ? 1 : 0;
if (debugFlag)
printf("\n%s: setting run coil to %s...\n",
qPrintable(exeName),
bit_value != 0 ? "TRUE" : "FALSE");
protocol_delay(mconfig.rs485);
if (mconfig.control.functionCode == MODBUS_FUNC_WRITE_SINGLE_COIL)
result = modbus_write_bit(ctx, mconfig.control.runCoil, bit_value);
if (mconfig.control.functionCode == MODBUS_FUNC_WRITE_MULTIPLE_COILS)
result = modbus_write_bits(ctx, mconfig.control.runCoil, 1, &bit_value);
if (result != 1)
goto fail;
else
okCounter++;
// Fault reset coil
if ((*hal_mdata->faultReset != 0)
&& (mconfig.control.faultResetCoil != INACTIVE_FLAG)) {
bit_value = 1;
if (debugFlag)
printf("\n%s: setting fault reset coil to %s...\n",
qPrintable(exeName),
bit_value != 0 ? "TRUE" : "FALSE");
protocol_delay(mconfig.rs485);
if (mconfig.control.functionCode == MODBUS_FUNC_WRITE_SINGLE_COIL)
result = modbus_write_bit(ctx, mconfig.control.faultResetCoil, bit_value);
if (mconfig.control.functionCode == MODBUS_FUNC_WRITE_MULTIPLE_COILS)
result = modbus_write_bits(ctx, mconfig.control.faultResetCoil, 1, &bit_value);
if (result != 1)
goto fail;
else
okCounter++;
// If fault reset has been written successfully then inactive it's input
*hal_mdata->faultReset = 0;
}
}
return 0;
fail:
*hal_mdata->lastError = errno;
(*hal_mdata->errorCount)++;
okCounter = 0;
return -1;
}
int main(int argc, char *argv[])
{
main_config_t mconfig;
QVector<user_config_t> uconfig;
QString inifile;
checkFlag = 0;
debugFlag = 0;
exitFlag = 0;
okCounter = 0;
serialDeviceIsOpened = 0;
int newFlag = 0;
int pyvcpFlag = 0;
int postguiFlag = 0;
exeName = QFileInfo(argv[0]).fileName();
int arg;
int index;
int count = 0;
while ((arg = getopt_long(argc, argv, short_options, long_options, &index)) != -1) {
switch (arg) {
case 'c':
checkFlag = 1;
count++;
break;
case 'd':
debugFlag = 1;
break;
case 'h':
print_help();
return 0;
case 'n':
newFlag = 1;
count++;
break;
case 'v':
printf("%s %s\n", APP_TARGET, APP_VERSION);
printf(github);
printf(copyright);
return 0;
case 'P':
postguiFlag = 1;
count++;
break;
case 'V':
pyvcpFlag = 1;
count++;
break;
default:
fprintf(stderr, "Arguments are wrong! Type '%s -h' for help.\n", qPrintable(exeName));
return 0;
}
}
if (count > 1) {
fprintf(stderr, "Too many keys! Type '%s -h' for help.\n", qPrintable(exeName));
return 0;
}
/* If --new flag specified */
if (newFlag) {
make_blank_config();
return 0;
}
if ((argc - optind) == 0) {
fprintf(stderr, "File name is missing! Type '%s -h' for help.\n", qPrintable(exeName));
return 0;
}
if ((argc - optind) > 1) {
fprintf(stderr, "Too many arguments! Type '%s -h' for help.\n", qPrintable(exeName));
return 0;
}
inifile = argv[optind];
/* Finally, trying to load an existing config */
if (load_config(inifile, mconfig, uconfig) < 0)
return -1;
/* If --check flag specified */
if (checkFlag) {
// Nothing to do, just return
return 0;
}
/* If --postgui flag specified */
if (postguiFlag) {
make_postgui_config(mconfig, uconfig);
return 0;
}
/* If --pyvcp flag specified */
if (pyvcpFlag) {
make_pyvcp_config(QFileInfo(inifile).baseName(), mconfig, uconfig);
return 0;
}
/* HAL init */
int hal_comp_id = hal_init(qPrintable(mconfig.common.componentName));
if (hal_comp_id < 0)
return -1;
/* HAL memory allocation for main parameters */
hal_mdata = (hal_main_data_t *)hal_malloc(sizeof(hal_main_data_t));
if (!hal_mdata)
goto fail;
if (0 != hal_pin_bit_newf(HAL_OUT, &(hal_mdata->isConnected), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_RS485,
HAL_PIN_IS_CONNECTED))
goto fail;
if (0 != hal_pin_s32_newf(HAL_OUT, &(hal_mdata->errorCount), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_RS485,
HAL_PIN_ERROR_COUNT))
goto fail;
if (0 != hal_pin_s32_newf(HAL_OUT, &(hal_mdata->lastError), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_RS485,
HAL_PIN_LAST_ERROR))
goto fail;
if (0 != hal_pin_float_newf(HAL_IN, &(hal_mdata->spindleRpmIn), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_SPINDLE,
HAL_PIN_RPM_IN))
goto fail;
if (0 != hal_pin_float_newf(HAL_OUT, &(hal_mdata->spindleRpmOut), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_SPINDLE,
HAL_PIN_RPM_OUT))
goto fail;
if (0 != hal_pin_bit_newf(HAL_OUT, &(hal_mdata->atSpeed), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_SPINDLE,
HAL_PIN_AT_SPEED))
goto fail;
if (0 != hal_pin_bit_newf(HAL_IN, &(hal_mdata->faultReset), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_CONTROL,
HAL_PIN_FAULT_RESET))
goto fail;
if (0 != hal_pin_bit_newf(HAL_IN, &(hal_mdata->runForward), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_CONTROL,
HAL_PIN_RUN_FORWARD))
goto fail;
if (0 != hal_pin_bit_newf(HAL_IN, &(hal_mdata->runReverse), hal_comp_id, "%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_CONTROL,
HAL_PIN_RUN_REVERSE))
goto fail;
*hal_mdata->isConnected = 0;
*hal_mdata->errorCount = 0;
*hal_mdata->lastError = 0;
*hal_mdata->spindleRpmIn = 0;
*hal_mdata->spindleRpmOut = 0;
*hal_mdata->atSpeed = 0;
*hal_mdata->runForward = 0;
*hal_mdata->runReverse = 0;
/* HAL memory allocation for user parameters */
hal_udata = new hal_user_data_t*[uconfig.count()];
for (int i = 0; i < uconfig.count(); ++i) {
hal_udata[i] = (hal_user_data_t *)hal_malloc(sizeof(hal_user_data_t));
if (!hal_udata[i])
goto fail;
}
for (int i = 0; i < uconfig.count(); ++i) {
if ((uconfig.at(i).functionCode == MODBUS_FUNC_READ_MULTIPLE_COILS)
|| (uconfig.at(i).pinType == HAL_BIT)) {
// Direct bit pin
if (0 == hal_pin_bit_newf(HAL_OUT, &(hal_udata[i]->bitPin),
hal_comp_id,
"%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_USER_PARAMETERS,
qPrintable(uconfig.at(i).pinName)))
*hal_udata[i]->bitPin = 0;
else
goto fail;
// Inverted bit pin
if (0 == hal_pin_bit_newf(HAL_OUT, &(hal_udata[i]->bitNotPin),
hal_comp_id,
"%s.%s.%s-not",
qPrintable(mconfig.common.componentName),
HAL_GROUP_USER_PARAMETERS,
qPrintable(uconfig.at(i).pinName)))
*hal_udata[i]->bitNotPin = 1;
else
goto fail;
} else
switch (uconfig.at(i).pinType) {
case HAL_FLOAT:
if (0 == hal_pin_float_newf(HAL_OUT, &(hal_udata[i]->floatPin),
hal_comp_id,
"%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_USER_PARAMETERS,
qPrintable(uconfig.at(i).pinName)))
*hal_udata[i]->floatPin = 0;
else
goto fail;
break;
case HAL_S32:
if (0 == hal_pin_s32_newf(HAL_OUT, &(hal_udata[i]->s32Pin),
hal_comp_id,
"%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_USER_PARAMETERS,
qPrintable(uconfig.at(i).pinName)))
*hal_udata[i]->s32Pin = 0;
else
goto fail;
break;
case HAL_U32:
if (0 == hal_pin_u32_newf(HAL_OUT, &(hal_udata[i]->u32Pin),
hal_comp_id,
"%s.%s.%s",
qPrintable(mconfig.common.componentName),
HAL_GROUP_USER_PARAMETERS,
qPrintable(uconfig.at(i).pinName)))
*hal_udata[i]->u32Pin = 0;
else
goto fail;
break;
default:
fprintf(stderr, "%s: incorrect HAL pin type!\n", qPrintable(exeName));
}
}
/* Modbus init */
modbus_t *ctx;
ctx = modbus_new_rtu(qPrintable(mconfig.rs485.serialDevice),
mconfig.rs485.baudRate,
mconfig.rs485.parity.at(0).toLatin1(),
mconfig.rs485.dataBits,
mconfig.rs485.stopBits);
if (!ctx)
goto fail;
modbus_set_debug(ctx, debugFlag);
modbus_set_slave(ctx, mconfig.rs485.slaveAddress);
if (modbus_connect(ctx) == -1) {
fprintf(stderr, "%s: %s\n", qPrintable(exeName), modbus_strerror(errno));
modbus_free(ctx);
goto fail;
}
serialDeviceIsOpened = 1;
signal(SIGINT, closeRequest);
signal(SIGKILL, closeRequest);
signal(SIGTERM, closeRequest);
/* Now HAL component is ready */
hal_ready(hal_comp_id);
/* MAIN LOOP */
while (!exitFlag) {
/* Reading modbus data */
if (serialDeviceIsOpened) {
if (read_modbus_data(ctx, mconfig, uconfig) < 0)
/* Is last error code means connection lost? */
foreach (int err, mconfig.rs485.criticalErrors) {
if (*hal_mdata->lastError == err) {
/* Close connection */
modbus_close(ctx);
if (debugFlag)
printf("\n%s: critical error detected, connection closed.\n", qPrintable(exeName));
serialDeviceIsOpened = 0;
break;
}
}
}
/* Writing modbus data */
if (serialDeviceIsOpened) {
if (write_modbus_data(ctx, mconfig) < 0)
/* Is last error code means connection lost? */
foreach (int err, mconfig.rs485.criticalErrors) {
if (*hal_mdata->lastError == err) {
/* Close connection */
modbus_close(ctx);
if (debugFlag)
printf("\n%s: critical error detected, connection closed.\n", qPrintable(exeName));
serialDeviceIsOpened = 0;
break;
}
}
}
/* Should we clear HAL output data if any error has been occured? */
/* Is this really necessary? I'm doubt... Anyway let's do it. */
if (okCounter == 0) {
*hal_mdata->spindleRpmOut = 0;
*hal_mdata->atSpeed = 0;
for (int i = 0; i < uconfig.count(); ++i) {
switch (uconfig.at(i).pinType) {
case HAL_FLOAT:
*hal_udata[i]->floatPin = 0;
break;
case HAL_S32:
*hal_udata[i]->s32Pin = 0;
break;
case HAL_U32:
*hal_udata[i]->u32Pin = 0;
break;
case HAL_BIT:
*hal_udata[i]->bitPin = 0;
*hal_udata[i]->bitNotPin = 1;
break;
default:
fprintf(stderr, "%s: incorrect HAL pin type!\n", qPrintable(exeName));
}
}
}
/* Is Modbus connection established? */
if (okCounter >= mconfig.rs485.isConnectedDelay) {
*hal_mdata->isConnected = 1;
okCounter = mconfig.rs485.isConnectedDelay; // to avoid okCounter overflow
} else
*hal_mdata->isConnected = 0;
/* At speed may be valid only when spindle is running */
if (((0 != *hal_mdata->runReverse) || (0 != *hal_mdata->runForward)) &&
(0 != *hal_mdata->spindleRpmIn) && (0 != *hal_mdata->isConnected)) {
hal_float_t rpmIn = abs(*hal_mdata->spindleRpmIn);
hal_float_t rpmOut = *hal_mdata->spindleRpmOut;
if ((abs(rpmOut - rpmIn) / rpmIn) <= mconfig.common.atSpeedThreshold)
*hal_mdata->atSpeed = 1;
else
*hal_mdata->atSpeed = 0;
} else
*hal_mdata->atSpeed = 0;
/* Reconnection */
if (!serialDeviceIsOpened) {
/* Connection delay */
delay_ms(mconfig.rs485.connectionDelay);
if (debugFlag)
printf("\n%s: connection attempt...\n", qPrintable(exeName));
/* Open connection */
if (modbus_connect(ctx) == -1) {
(*hal_mdata->errorCount)++;
} else {
serialDeviceIsOpened = 1;
}
} else
/* Loop delay */
delay_ms(mconfig.rs485.loopDelay);
}
modbus_close(ctx);
modbus_free(ctx);
hal_exit(hal_comp_id);
delete [] hal_udata;
printf("%s: application closed.\n", qPrintable(exeName));
return 0;
fail:
hal_exit(hal_comp_id);
delete [] hal_udata;
fprintf(stderr, "%s: fatal error.\n", qPrintable(exeName));
return -1;
}