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:22] – 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) | if (r[6] != SLAVEID) | ||
| continue; // not for us | continue; // not for us | ||
| + | | ||
| cmd = r[7]; // command | cmd = r[7]; // command | ||
| - | switch (cmd) { | + | switch (cmd) { // write commands |
| - | case 1: // read coil status | + | |
| - | case 2: // read input status | + | |
| - | break; // no write action, only reply needed | + | |
| 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]<< | ||
| - | | + | |
| - | case 0: | + | |
| - | if (data == 0) | + | break; |
| - | | + | case 15: // write to multiple coils |
| - | | + | addr = r[8]<< |
| - | nb_dio_set(" | + | cnt = r[10]<< |
| - | | + | for (i = 0; i < cnt; i ++) |
| - | case 1: | + | |
| - | | + | |
| - | | + | |
| - | else | + | |
| - | nb_dio_set(" | + | |
| - | | + | |
| - | } | + | |
| break; | break; | ||
| } | } | ||
| Line 76: | Line 105: | ||
| case 2: // read input status | case 2: // read input status | ||
| case 5: // write to a single coil | 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( | resp = mkstruct( | ||
| - | " | + | " |
| - | " | + | " |
| - | " | + | " |
| - | " | + | " |
| ); | ); | ||
| if (nb_modbus_reply(cfd, | if (nb_modbus_reply(cfd, | ||
| Line 92: | Line 125: | ||
| } | } | ||
| } | } | ||
| - | |||
| /* not reached */ | /* not reached */ | ||
| </ | </ | ||