This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next 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 | ||
- | * supports Read Coil Status (FC01), Read Input Status (FC02) and Force Single Coil (FC05) | + | * |
+ | * Commands: | ||
+ | * Read Coil Status (FC01) | ||
+ | * Read Input Status (FC02) | ||
+ | * Read Input Register (FC04) | ||
+ | * Force Single Coil (FC05) | ||
+ | * Write Multiple Coils (FC15) | ||
+ | * | ||
* Mapping: | * Mapping: | ||
* Coil status(digital I/O): 1=out1, 2=out2 | * Coil status(digital I/O): 1=out1, 2=out2 | ||
* Input satus(digital In): 1=in1, 2=in2 | * Input satus(digital In): 1=in1, 2=in2 | ||
* Input register : 1=temperature 2=mobile 1 rsrp | * Input register : 1=temperature 2=mobile 1 rsrp | ||
+ | * | ||
*/ | */ | ||
Line 33: | Line 41: | ||
break; | break; | ||
} | } | ||
- | return ret; | + | return ret; |
} | } | ||
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("Unable to receive request (%s)\n", nb_modbus_last_error()); | printf("Unable to receive request (%s)\n", nb_modbus_last_error()); | ||
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]<<8 + r[9]; | addr = r[8]<<8 + r[9]; | ||
data = r[10]<<8 + r[11]; | data = r[10]<<8 + r[11]; | ||
- | if (set_io(addr, data) == -1) | + | if (set_io(addr, data) == -1) |
- | continue; // error | + | continue; // error |
break; | break; | ||
- | case 15: | + | case 15: // write to multiple coils |
- | addr = r[8]<<8 + r[9]; | + | addr = r[8]<<8 + r[9]; |
- | cnt = r[10]<<8 + r[11]; | + | cnt = r[10]<<8 + r[11]; |
for (i = 0; i < cnt; i ++) | for (i = 0; i < cnt; i ++) | ||
if (set_io(addr+i, (int) (r[13+i/8] & 1<<(i%256))) == -1) | if (set_io(addr+i, (int) (r[13+i/8] & 1<<(i%256))) == -1) | ||
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 4: // read input register | + | case 4: // read input register |
- | case 15: // write to multiple coil | + | case 15: // write to multiple coils |
- | syst = nb_status("system"); | + | syst = nb_status("system"); |
- | wwan = nb_status("wwan"); | + | wwan = nb_status("wwan"); |
resp = mkstruct( | resp = mkstruct( | ||
"bits", mkarray(nb_dio_get("out1"), nb_dio_get("out2")), | "bits", mkarray(nb_dio_get("out1"), nb_dio_get("out2")), | ||
"ibits", mkarray(nb_dio_get("in1"), nb_dio_get("in2")), | "ibits", mkarray(nb_dio_get("in1"), nb_dio_get("in2")), | ||
- | "regs", mkarray(), | + | "regs", mkarray(0,0), |
"iregs", mkarray((int)syst.TEMPERATURE, (int)wwan.MOBILE1_RSRP) | "iregs", mkarray((int)syst.TEMPERATURE, (int)wwan.MOBILE1_RSRP) | ||
); | ); | ||
Line 116: | Line 125: | ||
} | } | ||
} | } | ||
- | /* not reached */</code> | + | /* not reached */ |
+ | </code> |