This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
sdk:dio2modbustcp [2015/05/11 16:00] – 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 | ||
- | | + | * |
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
| | ||
- | | + | |
- | * Input(digital In): 1=in1, 2=in2 | + | |
+ | | ||
+ | * | ||
*/ | */ | ||
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, | ||
- | | + | |
- | exit(1); | + | exit(1); |
} | } | ||
if (bind(fd, TCP_PORT, "" | if (bind(fd, TCP_PORT, "" | ||
- | | + | |
- | exit(2); | + | exit(2); |
} | } | ||
if (listen(fd, 1) < 0) { | if (listen(fd, 1) < 0) { | ||
- | | + | |
- | exit(3); | + | exit(3); |
- | } | + | } |
- | + | ||
cfd = -1; | cfd = -1; | ||
for(;;) { | for(;;) { | ||
- | | + | |
- | { | + | { |
- | if ((cfd = accept(fd)) < 0) { | + | if ((cfd = accept(fd)) < 0) { |
- | printf(" | + | printf(" |
- | continue; | + | continue; |
- | } | + | |
- | if (nb_modbus_register(cfd, | + | |
- | printf(" | + | |
- | close(cfd); | + | |
- | cfd = -1; | + | |
- | continue; | + | |
- | } | + | |
} | } | ||
- | if ((r = nb_modbus_receive(cfd)) == NULL) { | + | |
- | printf(" | + | printf(" |
+ | close(cfd); | ||
+ | cfd = -1; | ||
+ | continue; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | | ||
+ | printf(" | ||
+ | nb_modbus_unregister(cfd); | ||
+ | close(cfd); | ||
+ | cfd = -1; | ||
+ | continue; | ||
+ | } | ||
+ | |||
+ | if (r[6] != SLAVEID) | ||
+ | continue; // not for us | ||
+ | |||
+ | cmd = r[7]; // command | ||
+ | switch (cmd) { // write commands | ||
+ | case 5: // write to a single coil | ||
+ | addr = r[8]<< | ||
+ | data = r[10]<< | ||
+ | if (set_io(addr, | ||
+ | continue; // error | ||
+ | break; | ||
+ | case 15: // write to multiple coils | ||
+ | addr = r[8]<< | ||
+ | cnt = r[10]<< | ||
+ | for (i = 0; i < cnt; i ++) | ||
+ | if (set_io(addr+i, | ||
+ | continue; // error | ||
+ | break; | ||
+ | } | ||
+ | |||
+ | switch (cmd) { // reply for all commands | ||
+ | case 1: // read coil status | ||
+ | case 2: // read input status | ||
+ | case 5: // write to a single coil | ||
+ | case 4: // read input register | ||
+ | case 15: // write to multiple coils | ||
+ | syst = nb_status(" | ||
+ | wwan = nb_status(" | ||
+ | resp = mkstruct( | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ); | ||
+ | if (nb_modbus_reply(cfd, | ||
+ | printf(" | ||
nb_modbus_unregister(cfd); | nb_modbus_unregister(cfd); | ||
close(cfd); | close(cfd); | ||
cfd = -1; | cfd = -1; | ||
continue; | continue; | ||
- | | + | |
- | if (r[6] == SLAVEID) { | + | break; |
- | switch (r[7]) { // command | + | } |
- | case 1: // read coil status | + | |
- | | + | |
- | case 5: // write to a single coil | + | |
- | addr = r[8]<< | + | |
- | data = r[10]<< | + | |
- | switch (addr) { //switch on address | + | |
- | case 0: | + | |
- | if (data == 0) | + | |
- | nb_dio_set(" | + | |
- | else | + | |
- | nb_dio_set(" | + | |
- | break; | + | |
- | case 1: | + | |
- | if (data == 0) | + | |
- | nb_dio_set(" | + | |
- | else | + | |
- | nb_dio_set(" | + | |
- | break; | + | |
- | } | + | |
- | break; | + | |
- | } | + | |
- | resp = mkstruct( | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | " | + | |
- | ); | + | |
- | if (nb_modbus_reply(cfd, | + | |
- | { | + | |
- | printf(" | + | |
- | nb_modbus_unregister(cfd); | + | |
- | close(cfd); | + | |
- | cfd = -1; | + | |
- | continue; | + | |
- | } | + | |
- | | + | |
} | } | ||
- | |||
/* not reached */ | /* not reached */ | ||
</ | </ |