This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
sdk:dio2modbustcp [2015/05/12 08:17] – fachet | sdk:dio2modbustcp [2018/01/24 15:53] (current) – juraschek | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Access to the digital I/Os via Modbus TCP ====== | ====== Access to the digital I/Os via Modbus TCP ====== | ||
- | <code c dio2modbustcp.are>/ | + | <code c dio2modbustcp.are> |
+ | /* DESC: This script can be used to access to the digital I/Os via Modbus TCP | ||
* Copyright (C) 2015 NetModule AG, Switzerland | * Copyright (C) 2015 NetModule AG, Switzerland | ||
- | | + | * |
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
| | ||
- | | + | |
- | | + | |
+ | | ||
+ | * | ||
*/ | */ | ||
TCP_PORT | TCP_PORT | ||
SLAVEID | SLAVEID | ||
+ | |||
+ | int set_io(int adr, int dat) | ||
+ | { | ||
+ | ret = 0; | ||
+ | switch (adr) { //switch on address | ||
+ | case 0: | ||
+ | if (dat == 0) | ||
+ | nb_dio_set(" | ||
+ | else | ||
+ | nb_dio_set(" | ||
+ | break; | ||
+ | case 1: | ||
+ | if (dat == 0) | ||
+ | nb_dio_set(" | ||
+ | else | ||
+ | nb_dio_set(" | ||
+ | break; | ||
+ | default: | ||
+ | ret = -1; | ||
+ | break; | ||
+ | } | ||
+ | return ret; | ||
+ | } | ||
if ((fd = socket(AF_INET, | if ((fd = socket(AF_INET, | ||
Line 23: | Line 56: | ||
exit(3); | exit(3); | ||
} | } | ||
+ | |||
cfd = -1; | cfd = -1; | ||
for(;;) { | for(;;) { | ||
Line 38: | Line 72: | ||
} | } | ||
} | } | ||
+ | | ||
if ((r = nb_modbus_receive(cfd)) == NULL) { | if ((r = nb_modbus_receive(cfd)) == NULL) { | ||
printf(" | printf(" | ||
Line 45: | Line 80: | ||
continue; | continue; | ||
} | } | ||
- | if (r[6] == SLAVEID) | + | |
- | cmd = r[7]; // command | + | |
- | switch (cmd) { | + | |
- | case 1: // read coil status | + | |
- | case 2: // read input status | + | |
- | break; // no write action, only reply needed | + | switch (cmd) { // write commands |
- | case 5: // write to a single coil | + | case 5: // write to a single coil |
- | addr = r[8]<< | + | addr = r[8]<< |
- | data = r[10]<< | + | data = r[10]<< |
- | | + | if (set_io(addr, |
- | case 0: | + | |
- | | + | break; |
- | | + | case 15: // write to multiple coils |
- | | + | |
- | nb_dio_set(" | + | cnt = r[10]<< |
- | | + | |
- | case 1: | + | |
- | if (data == 0) | + | |
- | | + | break; |
- | | + | } |
- | nb_dio_set(" | + | |
- | break; | + | |
- | } | + | |
- | | + | |
- | } | + | |
| | ||
- | | + | |
- | case 1: // read coil status | + | case 1: // read coil status |
- | case 2: // read input status | + | case 2: // read input status |
- | case 5: // write to a single coil | + | case 5: // write to a single coil |
- | resp = mkstruct( | + | case 4: // read input register |
- | " | + | case 15: // write to multiple coils |
- | " | + | syst = nb_status(" |
- | " | + | wwan = nb_status(" |
- | " | + | |
- | ); | + | " |
- | if (nb_modbus_reply(cfd, | + | " |
- | printf(" | + | " |
- | nb_modbus_unregister(cfd); | + | " |
- | close(cfd); | + | ); |
- | cfd = -1; | + | if (nb_modbus_reply(cfd, |
- | continue; | + | printf(" |
- | } | + | nb_modbus_unregister(cfd); |
- | break; | + | close(cfd); |
- | } | + | cfd = -1; |
+ | continue; | ||
+ | } | ||
+ | break; | ||
+ | } | ||
} | } | ||
- | |||
/* not reached */ | /* not reached */ | ||
</ | </ |