Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
sdk:dio2modbustcp [2015/05/12 08:17] fachetsdk: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>/* DESC: This script can be used to access to the digital I/Os via Modbus TCP+<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
- 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(digital I/O): 1=out1, 2=out2 +     Coil status(digital I/O): 1=out1, 2=out2 
-     Input(digital In): 1=in1, 2=in2 +     Input satus(digital In): 1=in1, 2=in2   
 +     Input register : 1=temperature 2=mobile 1 rsrp 
 + *
  */  */
  
 TCP_PORT    = 1502;  // sdk port must be higher than 1024 TCP_PORT    = 1502;  // sdk port must be higher than 1024
 SLAVEID     = 7; SLAVEID     = 7;
 +
 +int set_io(int adr, int dat)
 +{
 +  ret = 0;
 +  switch (adr) { //switch on address 
 +    case 0:
 +      if (dat == 0)
 +        nb_dio_set("out1", 0);
 +      else
 +        nb_dio_set("out1", 1);
 +      break;
 +    case 1:
 +      if (dat == 0)
 +        nb_dio_set("out2", 0);
 +      else
 +        nb_dio_set("out2", 1);
 +      break;
 +    default:
 +    ret = -1;
 +    break;    
 +    }
 +  return ret;
 +}
  
 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {  if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { 
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("Unable to receive request (%s)\n", nb_modbus_last_error());     printf("Unable to receive request (%s)\n", nb_modbus_last_error());
Line 45: Line 80:
     continue;     continue;
   }    } 
-  if (r[6] == SLAVEID)  +   
-    cmd = r[7]; // command +  if (r[6] != SLAVEID) 
-    switch (cmd) { +    continue; // not for us  
-      case 1: // read coil status +   
-      case 2: // read input status +  cmd = r[7]; // command 
-        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]<<8 + r[9]; +      addr = r[8]<<8 + r[9]; 
-        data = r[10]<<8 + r[11]; +      data = r[10]<<8 + r[11]; 
-        switch (addr) { //switch on address  +      if (set_io(addr, data== -1
-          case 0: +        continue// error 
-            if (data == 0+      break; 
-              nb_dio_set("out1", 0)+    case 15// write to multiple coils 
-            else +      addr r[8]<<8 + r[9]; 
-              nb_dio_set("out1", 1); +      cnt r[10]<<8 + r[11]; 
-            break; +      for (i = 0; i < cnt; i ++) 
-          case 1+        if (set_io(addr+i(int) (r[13+i/8] & 1<<(i%256))) == -1) 
-            if (data == 0) +          continue// error 
-              nb_dio_set("out2", 0)+      break; 
-            else +  }   
-              nb_dio_set("out2", 1); +
-            break+
-        } +
-        break; +
-    }   +
          
-    switch (cmd) { // reply for all commands +  switch (cmd) { // reply for all commands 
-      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 
-          "bits", mkarray(nb_dio_get("in1"), nb_dio_get("in2")), +    case 15: // write to multiple coils 
-          "ibits", mkarray(nb_dio_get("out1"), nb_dio_get("out2")), +      syst = nb_status("system"); 
-          "regs", mkarray(), +      wwan = nb_status("wwan"); 
-          "iregs", mkarray() +      resp = mkstruct( 
-        ); +        "bits", mkarray(nb_dio_get("out1"), nb_dio_get("out2")), 
-        if (nb_modbus_reply(cfd, r, resp) < 0) { +        "ibits", mkarray(nb_dio_get("in1"), nb_dio_get("in2")), 
-            printf("Unable to reply%s)\n", nb_modbus_last_error()); +        "regs", mkarray(0,0), 
-            nb_modbus_unregister(cfd);  +        "iregs", mkarray((int)syst.TEMPERATURE, (int)wwan.MOBILE1_RSRP
-            close(cfd);  +      ); 
-            cfd = -1;  +      if (nb_modbus_reply(cfd, r, resp) < 0) { 
-            continue; +        printf("Unable to reply%s)\n", nb_modbus_last_error()); 
-        +        nb_modbus_unregister(cfd);  
-        break; +        close(cfd);  
-    }+        cfd = -1;  
 +        continue; 
 +      
 +      break; 
 +  }
 }    }   
- 
 /* not reached */ /* not reached */
 </code> </code>