This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| sdk:dio2modbustcp [2015/05/12 09:44] – fachet | sdk:dio2modbustcp [2018/01/24 15:53] (current) – juraschek | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| /* DESC: This script can be used to access to the digital I/Os via Modbus TCP | /* 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 | ||
| - | | + | * |
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| | | ||
| | | ||
| | | ||
| | | ||
| + | * | ||
| */ | */ | ||
| Line 33: | Line 41: | ||
| break; | break; | ||
| } | } | ||
| - | | + | |
| } | } | ||
| Line 48: | Line 56: | ||
| exit(3); | exit(3); | ||
| } | } | ||
| + | |||
| cfd = -1; | cfd = -1; | ||
| for(;;) { | for(;;) { | ||
| Line 63: | Line 72: | ||
| } | } | ||
| } | } | ||
| + | | ||
| if ((r = nb_modbus_receive(cfd)) == NULL) { | if ((r = nb_modbus_receive(cfd)) == NULL) { | ||
| printf(" | printf(" | ||
| Line 70: | 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]<< | ||
| - | | + | |
| - | continue; // error | + | continue; // error |
| break; | break; | ||
| - | case 15: | + | case 15: // write to multiple coils |
| - | addr = r[8]<< | + | addr = r[8]<< |
| - | cnt = r[10]<< | + | cnt = r[10]<< |
| for (i = 0; i < cnt; i ++) | for (i = 0; i < cnt; i ++) | ||
| if (set_io(addr+i, | if (set_io(addr+i, | ||
| Line 96: | 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 15: // write to multiple | + | case 15: // write to multiple |
| - | syst = nb_status(" | + | syst = nb_status(" |
| - | wwan = nb_status(" | + | wwan = nb_status(" |
| resp = mkstruct( | resp = mkstruct( | ||
| " | " | ||
| " | " | ||
| - | " | + | " |
| " | " | ||
| ); | ); | ||
| Line 116: | Line 125: | ||
| } | } | ||
| } | } | ||
| - | /* not reached */</ | + | /* not reached */ |
| + | </ | ||