-
Notifications
You must be signed in to change notification settings - Fork 0
/
drvModbusAsyn.c
executable file
·2187 lines (1971 loc) · 89.2 KB
/
drvModbusAsyn.c
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*----------------------------------------------------------------------
* file: drvModbusAsyn.c
*----------------------------------------------------------------------
* EPICS asyn driver support for Modbus protocol communication with PLCs
*
* Mark Rivers, University of Chicago
* Original Date March 3, 2007
*
* Based on the modtcp and plctcp code from Rolf Keitel of Triumf, with
* work from Ivan So at NSLS.
*-----------------------------------------------------------------------
*
*/
/* ANSI C includes */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
/* EPICS includes */
#include <dbAccess.h>
#include <epicsStdio.h>
#include <epicsString.h>
#include <epicsThread.h>
#include <epicsMutex.h>
#include <epicsEvent.h>
#include <epicsTime.h>
#include <epicsEndian.h>
#include <cantProceed.h>
#include <errlog.h>
#include <osiSock.h>
#include <iocsh.h>
/* Asyn includes */
#include "asynDriver.h"
#include "asynOctetSyncIO.h"
#include "asynCommonSyncIO.h"
#include "asynStandardInterfaces.h"
#include "modbus.h"
#include "drvModbusAsyn.h"
#include <epicsExport.h>
/* Defined constants */
#define MAX_READ_WORDS 125 /* Modbus limit on number of words to read */
#define MAX_WRITE_WORDS 123 /* Modbus limit on number of words to write */
#define HISTOGRAM_LENGTH 200 /* Length of time histogram */
#define MODBUS_READ_TIMEOUT 2.0 /* Timeout for asynOctetSyncIO->writeRead */
/* Note: this value actually has no effect, the real
* timeout is set in modbusInterposeConfig */
#define WAGO_ID_STRING "Wago" /* If the plcName parameter to drvModbusAsynConfigure contains
* this substring then the driver will do the initial readback
* for write operations and the readback for read/modify/write
* operations at address plus 0x200. */
#define WAGO_OFFSET 0x200 /* The offset for readback operations on Wago devices */
/* MODBUS_READ_WRITE_MULTIPLE_REGISTERS (function 23) is not very common, and is problematic.
* It can use a different range of registers for reading and writing.
* We handle this by defining 2 new function codes.
* These will use MODBUS_READ_WRITE_MULTIPLE_REGISTERS
* but one is read-only and the other is write-only.
* Each EPICS modbus driver will use one or the other, and hence will behave like
* READ_INPUT_REGISTERS or WRITE_MULTIPLE_REGISTERS. */
#define MODBUS_READ_INPUT_REGISTERS_F23 123
#define MODBUS_WRITE_MULTIPLE_REGISTERS_F23 223
/* Structures for drvUser interface */
typedef enum {
modbusDataCommand,
modbusReadCommand,
modbusEnableHistogramCommand,
modbusReadHistogramCommand,
modbusHistogramBinTimeCommand,
modbusHistogramTimeAxisCommand,
modbusPollDelayCommand,
modbusReadOKCommand,
modbusWriteOKCommand,
modbusIOErrorsCommand,
modbusLastIOTimeCommand,
modbusMaxIOTimeCommand
} modbusCommand;
/* Note, this constant must match the number of enums in modbusCommand */
#define MAX_MODBUS_COMMANDS 12
typedef struct {
modbusCommand command;
char *commandString;
} modbusCommandStruct;
static modbusCommandStruct modbusCommands[MAX_MODBUS_COMMANDS] = {
{modbusDataCommand, MODBUS_DATA_STRING},
{modbusReadCommand, MODBUS_READ_STRING},
{modbusEnableHistogramCommand, MODBUS_ENABLE_HISTOGRAM_STRING},
{modbusReadHistogramCommand, MODBUS_READ_HISTOGRAM_STRING},
{modbusHistogramBinTimeCommand, MODBUS_HISTOGRAM_BIN_TIME_STRING},
{modbusHistogramTimeAxisCommand, MODBUS_HISTOGRAM_TIME_AXIS_STRING},
{modbusPollDelayCommand, MODBUS_POLL_DELAY_STRING},
{modbusReadOKCommand, MODBUS_READ_OK_STRING},
{modbusWriteOKCommand, MODBUS_WRITE_OK_STRING},
{modbusIOErrorsCommand, MODBUS_IO_ERRORS_STRING},
{modbusLastIOTimeCommand, MODBUS_LAST_IO_TIME_STRING},
{modbusMaxIOTimeCommand, MODBUS_MAX_IO_TIME_STRING}
};
typedef struct {
modbusDataType_t dataType;
char *dataTypeString;
} modbusDataTypeStruct;
static modbusDataTypeStruct modbusDataTypes[MAX_MODBUS_DATA_TYPES] = {
{dataTypeUInt16, MODBUS_UINT16_STRING},
{dataTypeInt16SM, MODBUS_INT16_SM_STRING},
{dataTypeBCDUnsigned, MODBUS_BCD_UNSIGNED_STRING},
{dataTypeBCDSigned, MODBUS_BCD_SIGNED_STRING},
{dataTypeInt16, MODBUS_INT16_STRING},
{dataTypeInt32LE, MODBUS_INT32_LE_STRING},
{dataTypeInt32BE, MODBUS_INT32_BE_STRING},
{dataTypeFloat32LE, MODBUS_FLOAT32_LE_STRING},
{dataTypeFloat32BE, MODBUS_FLOAT32_BE_STRING},
{dataTypeFloat64LE, MODBUS_FLOAT64_LE_STRING},
{dataTypeFloat64BE, MODBUS_FLOAT64_BE_STRING},
};
/* The structure for the drvModbus asyn port or "object" */
typedef struct modbusStr *PLC_ID;
typedef struct modbusStr
{
char *portName; /* asyn port name for this server */
char *octetPortName; /* asyn port name for the asyn octet port */
char *plcType; /* String describing PLC type */
int isConnected; /* Connection status */
int ioStatus; /* I/O error status */
asynUser *pasynUserOctet; /* asynUser for asynOctet interface to asyn octet port */
asynUser *pasynUserCommon; /* asynUser for asynCommon interface to asyn octet port */
asynUser *pasynUserTrace; /* asynUser for asynTrace on this port */
asynStandardInterfaces asynStdInterfaces; /* Structure for standard interfaces */
int modbusSlave; /* Modbus slave address */
int modbusFunction; /* Modbus function code */
int modbusStartAddress; /* Modbus starting addess for this port */
int modbusLength; /* Number of words or bits of Modbus data */
modbusDataType_t *dataType; /* Data type */
epicsUInt16 *data; /* Memory buffer */
char modbusRequest[MAX_MODBUS_FRAME_SIZE]; /* Modbus request message */
char modbusReply[MAX_MODBUS_FRAME_SIZE]; /* Modbus reply message */
double pollDelay; /* Delay for readPoller */
epicsThreadId readPollerThreadId;
epicsEventId readPollerEventId;
int forceCallback;
int readOnceFunction;
int readOnceDone;
int readOK; /* Statistics */
int writeOK;
int IOErrors;
int maxIOMsec;
int lastIOMsec;
epicsInt32 timeHistogram[HISTOGRAM_LENGTH]; /* Histogram of read-times */
epicsInt32 histogramTimeAxis[HISTOGRAM_LENGTH]; /* Time axis of histogram of read-times */
int enableHistogram;
int histogramMsPerBin;
int readbackOffset; /* Readback offset for Wago devices */
} modbusStr_t;
/* Local variable declarations */
static char *driver = "drvModbusAsyn"; /* String for asynPrint */
/* Local function declarations */
/* These functions are in the asynCommon interface */
static void asynReport (void *drvPvt, FILE *fp, int details);
static asynStatus asynConnect (void *drvPvt, asynUser *pasynUser);
static asynStatus asynDisconnect (void *drvPvt, asynUser *pasynUser);
static asynStatus drvUserCreate (void *drvPvt, asynUser *pasynUser,
const char *drvInfo,
const char **pptypeName, size_t *psize);
static asynStatus drvUserGetType (void *drvPvt, asynUser *pasynUser,
const char **pptypeName, size_t *psize);
static asynStatus drvUserDestroy (void *drvPvt, asynUser *pasynUser);
/* These functions are in the asynUInt32Digital interface */
static asynStatus writeUInt32D (void *drvPvt, asynUser *pasynUser,
epicsUInt32 value, epicsUInt32 mask);
static asynStatus readUInt32D (void *drvPvt, asynUser *pasynUser,
epicsUInt32 *value, epicsUInt32 mask);
static asynStatus setInterrupt (void *drvPvt, asynUser *pasynUser,
epicsUInt32 mask, interruptReason reason);
static asynStatus clearInterrupt (void *drvPvt, asynUser *pasynUser,
epicsUInt32 mask);
static asynStatus getInterrupt (void *drvPvt, asynUser *pasynUser,
epicsUInt32 *mask, interruptReason reason);
/* These functions are in the asynInt32 interface */
static asynStatus writeInt32 (void *drvPvt, asynUser *pasynUser,
epicsInt32 value);
static asynStatus readInt32 (void *drvPvt, asynUser *pasynUser,
epicsInt32 *value);
static asynStatus getBounds (void *drvPvt, asynUser *pasynUser,
epicsInt32 *low, epicsInt32 *high);
/* These functions are in the asynFloat64 interface */
static asynStatus writeFloat64 (void *drvPvt, asynUser *pasynUser,
epicsFloat64 value);
static asynStatus readFloat64 (void *drvPvt, asynUser *pasynUser,
epicsFloat64 *value);
/* These functions are in the asynInt32Array interface */
static asynStatus readInt32Array (void *drvPvt, asynUser *pasynUser,
epicsInt32 *data, size_t maxChans,
size_t *nactual);
static asynStatus writeInt32Array (void *drvPvt, asynUser *pasynUser,
epicsInt32 *data, size_t maxChans);
/* These functions are not in any of the asyn interfaces */
static void readPoller(PLC_ID pPlc);
static int doModbusIO(PLC_ID pPlc, int slave, int function, int start,
epicsUInt16 *data, int len);
static asynStatus readPlcInt (modbusStr_t *pPlc, int offset, epicsInt32 *value, int *bufferLen);
static asynStatus writePlcInt (modbusStr_t *pPlc, int offset, epicsInt32 value, epicsUInt16 *buffer, int *bufferLen);
static asynStatus readPlcFloat (modbusStr_t *pPlc, int offset, epicsFloat64 *value, int *bufferLen);
static asynStatus writePlcFloat(modbusStr_t *pPlc, int offset, epicsFloat64 value, epicsUInt16 *buffer, int *bufferLen);
/* asynCommon methods */
static const struct asynCommon drvCommon = {
asynReport,
asynConnect,
asynDisconnect
};
/* asynDrvUser methods */
static asynDrvUser drvUser = {
drvUserCreate,
drvUserGetType,
drvUserDestroy
};
/* asynUInt32Digital methods */
static struct asynUInt32Digital drvUInt32D = {
writeUInt32D,
readUInt32D,
setInterrupt,
clearInterrupt,
getInterrupt,
NULL,
NULL
};
/* asynInt32 methods */
static asynInt32 drvInt32 = {
writeInt32,
readInt32,
getBounds,
NULL,
NULL
};
/* asynFloat64 methods */
static asynFloat64 drvFloat64 = {
writeFloat64,
readFloat64,
NULL,
NULL
};
/* asynInt32Array methods */
static asynInt32Array drvInt32Array = {
writeInt32Array,
readInt32Array,
NULL,
NULL
};
/********************************************************************
** global driver functions
*********************************************************************
*/
/*
** drvModbusAsynConfigure() - create and init an asyn port driver for a PLC
**
*/
int drvModbusAsynConfigure(char *portName,
char *octetPortName,
int modbusSlave,
int modbusFunction,
int modbusStartAddress,
int modbusLength,
modbusDataType_t dataType,
int pollMsec,
char *plcType)
{
int status;
PLC_ID pPlc;
char readThreadName[100];
int needReadThread=0;
int IOLength=0;
int maxLength=0;
int i;
pPlc = callocMustSucceed(1, sizeof(*pPlc), "drvModbusAsynConfigure");
pPlc->portName = epicsStrDup(portName);
pPlc->octetPortName = epicsStrDup(octetPortName);
pPlc->plcType = epicsStrDup(plcType);
pPlc->modbusSlave = modbusSlave;
pPlc->modbusFunction = modbusFunction;
pPlc->modbusStartAddress = modbusStartAddress;
pPlc->modbusLength = modbusLength;
pPlc->pollDelay = pollMsec/1000.;
pPlc->histogramMsPerBin = 1;
/* Set readback offset for Wago devices for which the register readback address
* is different from the register write address */
if (strstr(pPlc->plcType, WAGO_ID_STRING) != NULL)
pPlc->readbackOffset = WAGO_OFFSET;
switch(pPlc->modbusFunction) {
case MODBUS_READ_COILS:
case MODBUS_READ_DISCRETE_INPUTS:
IOLength = pPlc->modbusLength/16;
maxLength = MAX_READ_WORDS;
needReadThread = 1;
break;
case MODBUS_READ_HOLDING_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS_F23:
IOLength = pPlc->modbusLength;
maxLength = MAX_READ_WORDS;
needReadThread = 1;
break;
case MODBUS_WRITE_SINGLE_COIL:
case MODBUS_WRITE_MULTIPLE_COILS:
IOLength = pPlc->modbusLength/16;
maxLength = MAX_WRITE_WORDS;
if (pollMsec != 0) pPlc->readOnceFunction = MODBUS_READ_COILS;
break;
case MODBUS_WRITE_SINGLE_REGISTER:
case MODBUS_WRITE_MULTIPLE_REGISTERS:
IOLength = pPlc->modbusLength;
maxLength = MAX_WRITE_WORDS;
if (pollMsec != 0) pPlc->readOnceFunction = MODBUS_READ_HOLDING_REGISTERS;
break;
case MODBUS_WRITE_MULTIPLE_REGISTERS_F23:
IOLength = pPlc->modbusLength;
maxLength = MAX_WRITE_WORDS;
if (pollMsec != 0) pPlc->readOnceFunction = MODBUS_READ_INPUT_REGISTERS_F23;
break;
default:
errlogPrintf("%s::drvModbusAsynConfig port %s unsupported"
" Modbus function %d\n",
driver, pPlc->portName, pPlc->modbusFunction);
return asynError;
}
/* Make sure memory length is valid. */
if (pPlc->modbusLength <= 0) {
errlogPrintf("%s::drvModbusConfigure, port %s"
" memory length<=0\n",
driver, pPlc->portName);
return asynError;
}
if (IOLength > maxLength) {
errlogPrintf("%s::drvModbusConfigure, port %s"
" memory length=%d too large, max=%d\n",
driver, pPlc->portName, IOLength, maxLength);
return asynError;
}
/* Note that we always allocate modbusLength words of memory.
* This is needed even for write operations because we need a buffer to convert
* data for asynInt32Array writes. */
pPlc->data = callocMustSucceed(pPlc->modbusLength, sizeof(epicsUInt16),
"drvModbusAsynConfigure");
pPlc->dataType = callocMustSucceed(pPlc->modbusLength, sizeof(modbusDataType_t),
"drvModbusAsynConfigure");
/* Set the data type for all addresses to the default data type passed to this function */
for (i=0; i<pPlc->modbusLength; i++) {
pPlc->dataType[i] = dataType;
}
/* Connect to asyn octet port with asynOctetSyncIO */
status = pasynOctetSyncIO->connect(octetPortName, 0, &pPlc->pasynUserOctet, 0);
if (status != asynSuccess) {
errlogPrintf("%s::drvModbusAsynConfigure port %s"
" can't connect to asynOctet on Octet server %s.\n",
driver, portName, octetPortName);
return asynError;
}
/* Connect to asyn octet port with asynCommonSyncIO */
status = pasynCommonSyncIO->connect(octetPortName, 0, &pPlc->pasynUserCommon, 0);
if (status != asynSuccess) {
errlogPrintf("%s::drvModbusAsynConfigure port %s"
" can't connect to asynCommon on Octet server %s.\n",
driver, portName, octetPortName);
return asynError;
}
/* Create asynUser for asynTrace */
pPlc->pasynUserTrace = pasynManager->createAsynUser(0, 0);
pPlc->pasynUserTrace->userPvt = pPlc;
status = pasynManager->registerPort(pPlc->portName,
ASYN_MULTIDEVICE | ASYN_CANBLOCK,
1, /* autoconnect */
0, /* medium priority */
0); /* default stack size */
if (status != asynSuccess) {
errlogPrintf("%s::drvModbusAsynConfigure port %s"
" can't register port\n",
driver, pPlc->portName);
return asynError;
}
/* Create asyn interfaces and register with asynManager */
pPlc->asynStdInterfaces.common.pinterface = (void *)&drvCommon;
pPlc->asynStdInterfaces.drvUser.pinterface = (void *)&drvUser;
pPlc->asynStdInterfaces.uInt32Digital.pinterface = (void *)&drvUInt32D;
pPlc->asynStdInterfaces.int32.pinterface = (void *)&drvInt32;
pPlc->asynStdInterfaces.float64.pinterface = (void *)&drvFloat64;
pPlc->asynStdInterfaces.int32Array.pinterface = (void *)&drvInt32Array;
pPlc->asynStdInterfaces.uInt32DigitalCanInterrupt = 1;
pPlc->asynStdInterfaces.int32CanInterrupt = 1;
pPlc->asynStdInterfaces.float64CanInterrupt = 1;
pPlc->asynStdInterfaces.int32ArrayCanInterrupt = 1;
status = pasynStandardInterfacesBase->initialize(pPlc->portName, &pPlc->asynStdInterfaces,
pPlc->pasynUserTrace, pPlc);
if (status != asynSuccess) {
errlogPrintf("%s::drvModbusAsynConfigure port %s"
" can't register standard interfaces: %s\n",
driver, pPlc->portName, pPlc->pasynUserTrace->errorMessage);
return asynError;
}
/* Connect to device */
status = pasynManager->connectDevice(pPlc->pasynUserTrace, pPlc->portName, 0);
if (status != asynSuccess) {
errlogPrintf("%s::drvModbusAsynConfigure port %s"
" connectDevice failed %s\n",
driver, pPlc->portName, pPlc->pasynUserTrace->errorMessage);
return asynError;
}
/* If this is an output function do a readOnce operation if required. */
if (pPlc->readOnceFunction) {
status = doModbusIO(pPlc, pPlc->modbusSlave, pPlc->readOnceFunction,
(pPlc->modbusStartAddress + pPlc->readbackOffset),
pPlc->data, pPlc->modbusLength);
if (status == asynSuccess) pPlc->readOnceDone = 1;
}
/* Create the epicsEvent to wake up the readPoller.
* We do this even if there is no poller. */
pPlc->readPollerEventId = epicsEventCreate(epicsEventEmpty);
/* Create the thread to read registers if this is a read function code */
if (needReadThread) {
epicsSnprintf(readThreadName, 100, "%sRead", pPlc->portName);
pPlc->readPollerThreadId = epicsThreadCreate(readThreadName,
epicsThreadPriorityMedium,
epicsThreadGetStackSize(epicsThreadStackSmall),
(EPICSTHREADFUNC)readPoller,
pPlc);
pPlc->forceCallback = 1;
}
return asynSuccess;
}
/* asynDrvUser routines */
static asynStatus drvUserCreate(void *drvPvt, asynUser *pasynUser,
const char *drvInfo,
const char **pptypeName, size_t *psize)
{
PLC_ID pPlc = (PLC_ID)drvPvt;
int i;
int offset;
char *pstring;
/* We are passed a string that identifies this command.
* Set dataType and/or pasynUser->reason based on this string */
for (i=0; i<MAX_MODBUS_DATA_TYPES; i++) {
pstring = modbusDataTypes[i].dataTypeString;
if (epicsStrCaseCmp(drvInfo, pstring) == 0) {
pasynManager->getAddr(pasynUser, &offset);
if ((offset < 0) || (offset >= pPlc->modbusLength)) {
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::drvUserCreate port %s invalid memory request %d, max=%d\n",
driver, pPlc->portName, offset, pPlc->modbusLength);
return asynError;
}
pPlc->dataType[offset] = modbusDataTypes[i].dataType;
pasynUser->reason = modbusDataCommand;
if (pptypeName) *pptypeName = epicsStrDup(MODBUS_DATA_STRING);
if (psize) *psize = sizeof(MODBUS_DATA_STRING);
asynPrint(pasynUser, ASYN_TRACE_FLOW,
"%s::drvUserCreate, port %s data type=%s\n",
driver, pPlc->portName, pstring);
return asynSuccess;
}
}
for (i=0; i<MAX_MODBUS_COMMANDS; i++) {
pstring = modbusCommands[i].commandString;
if (epicsStrCaseCmp(drvInfo, pstring) == 0) {
pasynUser->reason = modbusCommands[i].command;
if (pptypeName) *pptypeName = epicsStrDup(pstring);
if (psize) *psize = sizeof(modbusCommands[i].command);
asynPrint(pasynUser, ASYN_TRACE_FLOW,
"%s::drvUserCreate, port %s command=%s\n",
driver, pPlc->portName, pstring);
return asynSuccess;
}
}
asynPrint(pasynUser, ASYN_TRACE_ERROR,
"%s::drvUserCreate, port %s unknown command=%s\n",
driver, pPlc->portName, drvInfo);
return asynError;
}
static asynStatus drvUserGetType(void *drvPvt, asynUser *pasynUser,
const char **pptypeName, size_t *psize)
{
int command = pasynUser->reason;
if (pptypeName)
*pptypeName = epicsStrDup(modbusCommands[command].commandString);
if (psize) *psize = sizeof(command);
return asynSuccess;
}
static asynStatus drvUserDestroy(void *drvPvt, asynUser *pasynUser)
{
return asynSuccess;
}
/***********************/
/* asynCommon routines */
/***********************/
/* Connect */
static asynStatus asynConnect(void *drvPvt, asynUser *pasynUser)
{
PLC_ID pPlc = (PLC_ID)drvPvt;
int signal;
pasynManager->getAddr(pasynUser, &signal);
if (signal < pPlc->modbusLength) {
pasynManager->exceptionConnect(pasynUser);
return asynSuccess;
} else {
return asynError;
}
}
/* Disconnect */
static asynStatus asynDisconnect(void *drvPvt, asynUser *pasynUser)
{
/* Does nothing for now.
* May be used if connection management is implemented */
pasynManager->exceptionDisconnect(pasynUser);
return asynSuccess;
}
/* Report parameters */
static void asynReport(void *drvPvt, FILE *fp, int details)
{
PLC_ID pPlc = (PLC_ID)drvPvt;
fprintf(fp, "modbus port: %s\n", pPlc->portName);
if (details) {
fprintf(fp, " asynOctet server: %s\n", pPlc->octetPortName);
fprintf(fp, " modbusSlave: %d\n", pPlc->modbusSlave);
fprintf(fp, " modbusFunction: %d\n", pPlc->modbusFunction);
fprintf(fp, " modbusStartAddress: 0%o\n", pPlc->modbusStartAddress);
fprintf(fp, " modbusLength: 0%o\n", pPlc->modbusLength);
fprintf(fp, " plcType: %s\n", pPlc->plcType);
fprintf(fp, " I/O errors: %d\n", pPlc->IOErrors);
fprintf(fp, " Read OK: %d\n", pPlc->readOK);
fprintf(fp, " Write OK: %d\n", pPlc->writeOK);
fprintf(fp, " pollDelay: %f\n", pPlc->pollDelay);
fprintf(fp, " Time for last I/O %d msec\n", pPlc->lastIOMsec);
fprintf(fp, " Max. I/O time: %d msec\n", pPlc->maxIOMsec);
fprintf(fp, " Time per hist. bin: %d msec\n", pPlc->histogramMsPerBin);
}
}
/*
** asynUInt32D support
*/
static asynStatus readUInt32D(void *drvPvt, asynUser *pasynUser, epicsUInt32 *value,
epicsUInt32 mask)
{
PLC_ID pPlc = (PLC_ID)drvPvt;
int offset;
switch(pasynUser->reason) {
case modbusDataCommand:
if (pPlc->ioStatus != asynSuccess) return(pPlc->ioStatus);
pasynManager->getAddr(pasynUser, &offset);
*value = 0;
if (offset >= pPlc->modbusLength) {
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::readUInt32D port %s invalid memory request %d, max=%d\n",
driver, pPlc->portName, offset, pPlc->modbusLength);
return asynError;
}
switch(pPlc->modbusFunction) {
case MODBUS_READ_COILS:
case MODBUS_READ_DISCRETE_INPUTS:
case MODBUS_READ_HOLDING_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS_F23:
*value = pPlc->data[offset];
if ((mask != 0 ) && (mask != 0xFFFF)) *value &= mask;
break;
case MODBUS_WRITE_SINGLE_COIL:
case MODBUS_WRITE_MULTIPLE_COILS:
case MODBUS_WRITE_SINGLE_REGISTER:
case MODBUS_WRITE_MULTIPLE_REGISTERS:
case MODBUS_WRITE_MULTIPLE_REGISTERS_F23:
if (!pPlc->readOnceDone) return asynError;
*value = pPlc->data[offset];
if ((mask != 0 ) && (mask != 0xFFFF)) *value &= mask;
break;
default:
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::readUInt32D port %s invalid request for Modbus"
" function %d\n",
driver, pPlc->portName, pPlc->modbusFunction);
return asynError;
}
asynPrint(pPlc->pasynUserTrace, ASYN_TRACEIO_DRIVER,
"%s::readUInt32D port %s function=0x%x,"
" offset=0%o, mask=0x%x, value=0x%x\n",
driver, pPlc->portName, pPlc->modbusFunction,
offset, mask, *value);
break;
case modbusEnableHistogramCommand:
*value = pPlc->enableHistogram;
break;
default:
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::writeUInt32D port %s invalid pasynUser->reason %d\n",
driver, pPlc->portName, pasynUser->reason);
return asynError;
}
return asynSuccess;
}
static asynStatus writeUInt32D(void *drvPvt, asynUser *pasynUser, epicsUInt32 value,
epicsUInt32 mask)
{
PLC_ID pPlc = (PLC_ID)drvPvt;
int offset;
int modbusAddress;
int i;
epicsUInt16 data = value;
asynStatus status;
switch(pasynUser->reason) {
case modbusDataCommand:
pasynManager->getAddr(pasynUser, &offset);
modbusAddress = pPlc->modbusStartAddress + offset;
if (offset >= pPlc->modbusLength) {
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::writeUInt32D port %s invalid memory request %d, max=%d\n",
driver, pPlc->portName, offset, pPlc->modbusLength);
return asynError;
}
switch(pPlc->modbusFunction) {
case MODBUS_WRITE_SINGLE_COIL:
status = doModbusIO(pPlc, pPlc->modbusSlave, pPlc->modbusFunction,
modbusAddress, &data, 1);
if (status != asynSuccess) return(status);
break;
case MODBUS_WRITE_SINGLE_REGISTER:
/* Do this as a read/modify/write if mask is not all 0 or all 1 */
if ((mask == 0) || (mask == 0xFFFF)) {
status = doModbusIO(pPlc, pPlc->modbusSlave, pPlc->modbusFunction,
modbusAddress, &data, 1);
} else {
status = doModbusIO(pPlc, pPlc->modbusSlave, MODBUS_READ_HOLDING_REGISTERS,
modbusAddress + pPlc->readbackOffset, &data, 1);
if (status != asynSuccess) return(status);
/* Set bits that are set in the value and set in the mask */
data |= (value & mask);
/* Clear bits that are clear in the value and set in the mask */
data &= (value | ~mask);
status = doModbusIO(pPlc, pPlc->modbusSlave, pPlc->modbusFunction,
modbusAddress, &data, 1);
}
if (status != asynSuccess) return(status);
break;
default:
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::writeUInt32D port %s invalid request for Modbus"
" function %d\n",
driver, pPlc->portName, pPlc->modbusFunction);
return asynError;
}
asynPrint(pPlc->pasynUserTrace, ASYN_TRACEIO_DRIVER,
"%s::writeUInt32D port %s function=0x%x,"
" address=0%o, mask=0x%x, value=0x%x\n",
driver, pPlc->portName, pPlc->modbusFunction,
modbusAddress, mask, data);
break;
case modbusEnableHistogramCommand:
if ((value != 0) && pPlc->enableHistogram == 0) {
/* We are turning on histogram enabling, erase existing data first */
for (i=0; i<HISTOGRAM_LENGTH; i++) {
pPlc->timeHistogram[i] = 0;
}
}
pPlc->enableHistogram = value ? 1 : 0;
break;
default:
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::writeUInt32D port %s invalid pasynUser->reason %d\n",
driver, pPlc->portName, pasynUser->reason);
return asynError;
}
return asynSuccess;
}
/* setInterrupt, clearInterrupt, and getInterrupt are required by the asynUInt32Digital
* interface.
* They are used to control hardware interrupts for drivers that use them.
* They are not needed for Modbus, so we just return asynSuccess. */
static asynStatus setInterrupt (void *drvPvt, asynUser *pasynUser, epicsUInt32 mask,
interruptReason reason)
{
return asynSuccess;
}
static asynStatus clearInterrupt (void *drvPvt, asynUser *pasynUser, epicsUInt32 mask)
{
return asynSuccess;
}
static asynStatus getInterrupt (void *drvPvt, asynUser *pasynUser, epicsUInt32 *mask,
interruptReason reason)
{
return asynSuccess;
}
/*
** asynInt32 support
*/
static asynStatus readInt32 (void *drvPvt, asynUser *pasynUser, epicsInt32 *value)
{
PLC_ID pPlc = (PLC_ID)drvPvt;
int offset;
asynStatus status;
int bufferLen;
pasynManager->getAddr(pasynUser, &offset);
*value = 0;
switch(pasynUser->reason) {
case modbusDataCommand:
if (pPlc->ioStatus != asynSuccess) return(pPlc->ioStatus);
if ((offset < 0) || (offset >= pPlc->modbusLength)) {
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::readInt32 port %s invalid memory request %d, max=%d\n",
driver, pPlc->portName, offset, pPlc->modbusLength);
return asynError;
}
switch(pPlc->modbusFunction) {
case MODBUS_READ_COILS:
case MODBUS_READ_DISCRETE_INPUTS:
*value = pPlc->data[offset];
break;
case MODBUS_READ_HOLDING_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS_F23:
status = readPlcInt(pPlc, offset, value, &bufferLen);
if (status != asynSuccess) return status;
break;
case MODBUS_WRITE_SINGLE_COIL:
case MODBUS_WRITE_MULTIPLE_COILS:
if (!pPlc->readOnceDone) return asynError ;
*value = pPlc->data[offset];
break;
case MODBUS_WRITE_SINGLE_REGISTER:
case MODBUS_WRITE_MULTIPLE_REGISTERS:
case MODBUS_WRITE_MULTIPLE_REGISTERS_F23:
if (!pPlc->readOnceDone) return asynError;
status = readPlcInt(pPlc, offset, value, &bufferLen);
if (status != asynSuccess) return status;
break;
default:
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::readInt32 port %s invalid request for Modbus"
" function %d\n",
driver, pPlc->portName, pPlc->modbusFunction);
return asynError;
}
asynPrint(pPlc->pasynUserTrace, ASYN_TRACEIO_DRIVER,
"%s::readInt32 port %s function=0x%x,"
" offset=0%o, value=0x%x\n",
driver, pPlc->portName, pPlc->modbusFunction,
offset, *value);
break;
case modbusReadCommand:
*value = 1;
break;
case modbusReadOKCommand:
*value = pPlc->readOK;
break;
case modbusWriteOKCommand:
*value = pPlc->writeOK;
break;
case modbusIOErrorsCommand:
*value = pPlc->IOErrors;
break;
case modbusLastIOTimeCommand:
*value = pPlc->lastIOMsec;
break;
case modbusMaxIOTimeCommand:
*value = pPlc->maxIOMsec;
break;
case modbusHistogramBinTimeCommand:
*value = pPlc->histogramMsPerBin;
break;
default:
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::readInt32 port %s invalid pasynUser->reason %d\n",
driver, pPlc->portName, pasynUser->reason);
return asynError;
}
return asynSuccess;
}
static asynStatus writeInt32(void *drvPvt, asynUser *pasynUser, epicsInt32 value)
{
PLC_ID pPlc = (PLC_ID)drvPvt;
int offset;
int modbusAddress;
epicsUInt16 buffer[4];
int bufferLen=0;
int i;
asynStatus status;
pasynManager->getAddr(pasynUser, &offset);
switch(pasynUser->reason) {
case modbusDataCommand:
if ((offset < 0) || (offset >= pPlc->modbusLength)) {
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::writeInt32 port %s invalid memory request %d, max=%d\n",
driver, pPlc->portName, offset, pPlc->modbusLength);
return asynError;
}
modbusAddress = pPlc->modbusStartAddress + offset;
switch(pPlc->modbusFunction) {
case MODBUS_WRITE_SINGLE_COIL:
buffer[0] = value;
status = doModbusIO(pPlc, pPlc->modbusSlave, pPlc->modbusFunction,
modbusAddress, buffer, 1);
if (status != asynSuccess) return(status);
break;
case MODBUS_WRITE_SINGLE_REGISTER:
status = writePlcInt(pPlc, offset, value, buffer, &bufferLen);
if (status != asynSuccess) return(status);
for (i=0; i<bufferLen; i++) {
status = doModbusIO(pPlc, pPlc->modbusSlave, pPlc->modbusFunction,
modbusAddress+i, buffer+i, bufferLen);
}
if (status != asynSuccess) return(status);
break;
case MODBUS_WRITE_MULTIPLE_REGISTERS:
case MODBUS_WRITE_MULTIPLE_REGISTERS_F23:
status = writePlcInt(pPlc, offset, value, buffer, &bufferLen);
if (status != asynSuccess) return(status);
status = doModbusIO(pPlc, pPlc->modbusSlave, pPlc->modbusFunction,
modbusAddress, buffer, bufferLen);
if (status != asynSuccess) return(status);
break;
default:
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::writeInt32 port %s invalid request for Modbus"
" function %d\n",
driver, pPlc->portName, pPlc->modbusFunction);
return asynError;
}
asynPrint(pPlc->pasynUserTrace, ASYN_TRACEIO_DRIVER,
"%s::writeInt32 port %s function=0x%x,"
" modbusAddress=0%o, buffer[0]=0x%x, bufferLen=%d\n",
driver, pPlc->portName, pPlc->modbusFunction,
modbusAddress, buffer[0], bufferLen);
break;
case modbusReadCommand:
/* Read the data for this driver. This can be used when the poller is disabled. */
epicsEventSignal(pPlc->readPollerEventId);
break;
case modbusHistogramBinTimeCommand:
/* Set the time per histogram bin in ms */
pPlc->histogramMsPerBin = value;
if (pPlc->histogramMsPerBin < 1) pPlc->histogramMsPerBin = 1;
/* Since the time might have changed erase existing data */
for (i=0; i<HISTOGRAM_LENGTH; i++) {
pPlc->timeHistogram[i] = 0;
pPlc->histogramTimeAxis[i] = i * pPlc->histogramMsPerBin;
}
break;
default:
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::writeInt32 port %s invalid pasynUser->reason %d\n",
driver, pPlc->portName, pasynUser->reason);
return asynError;
}
return asynSuccess;
}
/* This function should return the valid range for asynInt32 data.
* However, for Modbus devices there is no way for the driver to know what the
* valid range is, since the device could be an 8-bit unipolar ADC, 12-bit bipolar DAC,
* etc. Just return 0.
* It is up to device support to correctly interpret the numbers that are returned. */
static asynStatus getBounds (void *drvPvt, asynUser *pasynUser,
epicsInt32 *low, epicsInt32 *high)
{
*high = 0;
*low = 0;
return asynSuccess;
}
/*
** asynFloat64 support
*/
static asynStatus readFloat64 (void *drvPvt, asynUser *pasynUser, epicsFloat64 *value)
{
PLC_ID pPlc = (PLC_ID)drvPvt;
int offset;
int bufferLen;
asynStatus status = asynSuccess;
pasynManager->getAddr(pasynUser, &offset);
*value = 0;
switch(pasynUser->reason) {
case modbusDataCommand:
if (pPlc->ioStatus != asynSuccess) return(pPlc->ioStatus);
if ((offset < 0) || (offset >= pPlc->modbusLength)) {
asynPrint(pPlc->pasynUserTrace, ASYN_TRACE_ERROR,
"%s::readFloat64 port %s invalid memory request %d, max=%d\n",
driver, pPlc->portName, offset, pPlc->modbusLength);
return asynError;
}
switch(pPlc->modbusFunction) {
case MODBUS_READ_COILS:
case MODBUS_READ_DISCRETE_INPUTS:
*value = pPlc->data[offset];
break;
case MODBUS_READ_HOLDING_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS:
case MODBUS_READ_INPUT_REGISTERS_F23:
status = readPlcFloat(pPlc, offset, value, &bufferLen);
if (status != asynSuccess) return status;