예제 #1
0
static void *connection(void *arg) {
    modbus_t *ctx = (modbus_t *) arg;
    uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
    int bytes;
    uint16_t device;

    while (1) {
	bytes = modbus_receive(ctx, query);
	if (bytes > 0) {
	    /* More hacking: manually extract the modbus query address so that
	     * we can use it to update the holding registers. The project
	     * requires that the modus address match the BACnet device no */
	    device = (query[8] << 8) + query[9];
	    printf("Request for device %i\n", device);
	    file_update_regs(mb_mapping->tab_registers, device);
	    modbus_reply(ctx, query, bytes, mb_mapping);
	}

	/* Other end has disconnected */
	if (bytes == -1) break;
    }

    free(ctx);
    return arg;
}
예제 #2
0
void *serveClient(void *threadarg)
{
	SA *a;
	a = (SA *) threadarg;
	modbus_t *ctx = a->ctxt;
	modbus_mapping_t *mb_mapping = a->mm;	
	int id = a->id;
	printf("(%d) Serving... %p\n", id, ctx);
	for (;;) {
		uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
		int rc;
		printf("(%d) receiving...\n", id);
		rc = modbus_receive(ctx, query);
		if (rc > 0) {
		    /* rc is the query size */
		    modbus_reply(ctx, query, rc, mb_mapping);
		} else if (rc == -1) {
			printf("(%d) Disconnected %s\n", id, modbus_strerror(errno));
		    /* Connection closed by the client or error */
		    break;
		}
	}

	//clean-up
	free(ctx);
	free(a);
}
예제 #3
0
int main(int argc, char *argv[])
{
    modbus_t *m;
    uint8_t *q;
    int header_length;
    int socket;
    int rc;

    m = modbus_new_tcp("127.0.0.1", 1502);
    q = malloc(MODBUS_TCP_MAX_ADU_LENGTH);

    header_length = modbus_get_header_length(m);

    modbus_set_debug(m, TRUE);
    modbus_set_error_recovery(m, TRUE);

    socket = modbus_tcp_listen(m, 1);
    modbus_tcp_accept(m, &socket);

    while (1) {
	rc = modbus_receive(m, -1, q);
	if (rc < 0) {
	    error("modbus_receive() returned %d", rc);
	}

	dump_buffer(stdout, q, rc);
    }
}
예제 #4
0
bool Modbus::poll(int tm)
{
  timeval tv = { 0, tm };
  modbus_set_response_timeout(_modbus, &tv);
  int recv_bytes = modbus_receive(_modbus, _query);
  if (recv_bytes == -1)
  {
    return false;
  }
  DeviceModbus* pdev = findDevice(_query[0]);
  if (!pdev)
  {
    LOG_ERROR("Unknown slave ID: %d", _query[0]);
    return false;
  }
  if (pdev->board())
    pdev->board()->HandleRequest(_query, recv_bytes, pdev->modbusMap());
  modbus_set_slave(_modbus, pdev->slaveId());
  switch (_query[modbus_get_header_length(_modbus)])
  {
  case MODBUS_READ_COILS:
  case MODBUS_READ_DISCRETE_INPUTS:
  case MODBUS_READ_INPUT_REGISTERS:
  case MODBUS_READ_HOLDING_REGISTERS:
    pdev->updateValues();
    modbus_reply(_modbus, _query, recv_bytes, pdev->modbusMap());
    break;

  case MODBUS_WRITE_SINGLE_COIL:
    //LOG_INFO("MODBUS_WRITE_SINGLE_COIL");
    modbus_reply(_modbus, _query, recv_bytes, pdev->modbusMap());
    break;

  case MODBUS_WRITE_SINGLE_REGISTER:
    LOG_INFO("MODBUS_WRITE_SINGLE_REGISTER");
    modbus_reply(_modbus, _query, recv_bytes, pdev->modbusMap());
    break;

  case MODBUS_WRITE_MULTIPLE_COILS:
    LOG_INFO("MODBUS_WRITE_MULTIPLE_COILS");
    break;

  case MODBUS_WRITE_MULTIPLE_REGISTERS:
    LOG_INFO("MODBUS_WRITE_MULTIPLE_REGISTERS");
    break;

  default:
    LOG_ERROR("UNKNOWN FUNCTION");
    return false;
  }
  pdev->updateWidgets();
  return true;
}
int main(void)
{
    int s = -1;
    modbus_t *ctx;
    modbus_mapping_t *mb_mapping;

    ctx = modbus_new_tcp("127.0.0.1", 1502);
    /* modbus_set_debug(ctx, TRUE); */

    mb_mapping = modbus_mapping_new(500, 500, 500, 500);
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    s = modbus_tcp_listen(ctx, 1);
    modbus_tcp_accept(ctx, &s);

    for (;;) {
        uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
        int rc;

        rc = modbus_receive(ctx, query);
        if (rc > 0) {
            /* rc is the query size */
            modbus_reply(ctx, query, rc, mb_mapping);
        } else if (rc == -1) {
            /* Connection closed by the client or error */
            break;
        }
    }

    printf("Quit the loop: %s\n", modbus_strerror(errno));

    if (s != -1) {
        close(s);
    }
    modbus_mapping_free(mb_mapping);
    modbus_close(ctx);
    modbus_free(ctx);

    return 0;
}
예제 #6
0
static int ctx_receive(lua_State *L)
{
	ctx_t *ctx = ctx_check(L, 1);
	int rcount;

	uint8_t *req = malloc(ctx->max_len);
	int rc = modbus_receive(ctx->modbus, req);
	if (rc > 0) {
		lua_pushnumber(L, rc);
		lua_pushlstring(L, (char *)req, rc);
		rcount = 2;
	} else if (rc == 0) {
		printf("Special case for rc = 0, can't remember\n");
		rcount = 0;
	} else {
		rcount = libmodbus_rc_to_nil_error(L, rc, 0);
	}
	return rcount;
}
예제 #7
0
int main(int argc, char *argv[])
{
    int socket;
    modbus_t *ctx;
    modbus_mapping_t *mb_mapping;
    int rc;
    int use_backend;
    int justacounter;
    
    justacounter = 0;

while (TRUE) {
	
	
     /* TCP */
    if (argc > 1) {
        if (strcmp(argv[1], "tcp") == 0) {
            use_backend = TCP;
        } else if (strcmp(argv[1], "rtu") == 0) {
            use_backend = RTU;
        } else {
            printf("Usage:\n  %s [tcp|rtu] - Modbus client to measure data bandwith\n\n", argv[0]);
            exit(1);
        }
    } else {
        /* By default */
        use_backend = TCP;
	printf("bandwidth-server-one:\n Running in tcp mode - Modbus client to measure data bandwith\n\n");
    }

    if (use_backend == TCP) {
        printf("Waiting for TCP connection\n");
        ctx = modbus_new_tcp("127.0.0.1", 502);
        socket = modbus_tcp_listen(ctx, 1);
        modbus_tcp_accept(ctx, &socket);
        modbus_set_debug(ctx, TRUE);
	printf("TCP connection started!\n");
    } else {
	printf("Waiting for Serial connection\n");
        ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
        modbus_set_slave(ctx, 1);
        modbus_connect(ctx);
	printf("Serial connection started!\n");
    }

    mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 22,
                                    MODBUS_MAX_READ_REGISTERS, 22);
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    for(;;) {
        uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];

        rc = modbus_receive(ctx, query);
        if (rc >= 0) {
		mb_mapping->tab_input_registers[0] = justacounter++;	
	    printf("Replying to request.\n");
            modbus_reply(ctx, query, rc, mb_mapping);
        } else {
            /* Connection closed by the client or server */
           break;
        }
    }

    printf("Quit the loop: %s\n", modbus_strerror(errno));

    modbus_mapping_free(mb_mapping);
    close(socket);
    modbus_free(ctx);
}
    return 0;
}
예제 #8
0
int main(int argc, char *argv[])
{
    int s = -1;
    modbus_t *ctx = NULL;
    modbus_mapping_t *mb_mapping = NULL;
    int rc;
    int use_backend;

     /* TCP */
    if (argc > 1) {
        if (strcmp(argv[1], "tcp") == 0) {
            use_backend = TCP;
        } else if (strcmp(argv[1], "rtu") == 0) {
            use_backend = RTU;
        } else {
            printf("Usage:\n  %s [tcp|rtu] - Modbus client to measure data bandwith\n\n", argv[0]);
            exit(1);
        }
    } else {
        /* By default */
        use_backend = TCP;
    }

    if (use_backend == TCP) {
        ctx = modbus_new_tcp("127.0.0.1", 1502);
        s = modbus_tcp_listen(ctx, 1);
        modbus_tcp_accept(ctx, &s);

    } else {
        ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
        modbus_set_slave(ctx, 1);
        modbus_connect(ctx);
    }

    mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
                                    MODBUS_MAX_READ_REGISTERS, 0);
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    for(;;) {
        uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];

        rc = modbus_receive(ctx, query);
        if (rc > 0) {
            modbus_reply(ctx, query, rc, mb_mapping);
        } else if (rc  == -1) {
            /* Connection closed by the client or error */
            break;
        }
    }

    printf("Quit the loop: %s\n", modbus_strerror(errno));

    modbus_mapping_free(mb_mapping);
    if (s != -1) {
        close(s);
    }
    /* For RTU, skipped by TCP (no TCP connect) */
    modbus_close(ctx);
    modbus_free(ctx);

    return 0;
}
예제 #9
0
int main(int argc, char*argv[])
{
    int socket;
    modbus_t *ctx;
    modbus_mapping_t *mb_mapping;
    int rc;
    int i;
    int use_backend;
    uint8_t *query;
    int header_length;

    if (argc > 1) {
        if (strcmp(argv[1], "tcp") == 0) {
            use_backend = TCP;
        } else if (strcmp(argv[1], "rtu") == 0) {
            use_backend = RTU;
        } else {
            printf("Usage:\n  %s [tcp|rtu] - Modbus server for unit testing\n\n", argv[0]);
            return -1;
        }
    } else {
        /* By default */
        use_backend = TCP;
    }

    if (use_backend == TCP) {
        ctx = modbus_new_tcp("127.0.0.1", 1502);
        query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
    } else {
        ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
        modbus_set_slave(ctx, SERVER_ID);
        query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
    }
    header_length = modbus_get_header_length(ctx);

    modbus_set_debug(ctx, TRUE);
    modbus_set_error_recovery(ctx, TRUE);

    mb_mapping = modbus_mapping_new(
        UT_BITS_ADDRESS + UT_BITS_NB_POINTS,
        UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB_POINTS,
        UT_REGISTERS_ADDRESS + UT_REGISTERS_NB_POINTS,
        UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB_POINTS);
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    /* Examples from PI_MODBUS_300.pdf.
       Only the read-only input values are assigned. */

    /** INPUT STATUS **/
    modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
                               UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB_POINTS,
                               UT_INPUT_BITS_TAB);

    /** INPUT REGISTERS **/
    for (i=0; i < UT_INPUT_REGISTERS_NB_POINTS; i++) {
        mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
            UT_INPUT_REGISTERS_TAB[i];;
    }

    if (use_backend == TCP) {
        socket = modbus_tcp_listen(ctx, 1);
        modbus_tcp_accept(ctx, &socket);
    } else {
        rc = modbus_connect(ctx);
        if (rc == -1) {
            fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
            modbus_free(ctx);
            return -1;
        }
    }

    for (;;) {
        rc = modbus_receive(ctx, -1, query);
        if (rc == -1) {
            /* Connection closed by the client or error */
            break;
        }

        /* Read holding registers */
        if (query[header_length] == 0x03) {
            if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
                == UT_REGISTERS_NB_POINTS_SPECIAL) {
                printf("Set an incorrect number of values\n");
                MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
                                         UT_REGISTERS_NB_POINTS);
            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
                == UT_REGISTERS_ADDRESS_SPECIAL) {
                modbus_reply_exception(ctx, query,
                                       MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
                continue;
            }
        }

        rc = modbus_reply(ctx, query, rc, mb_mapping);
        if (rc == -1) {
            break;
        }
    }

    printf("Quit the loop: %s\n", modbus_strerror(errno));

    if (use_backend == TCP) {
        close(socket);
    }
    modbus_mapping_free(mb_mapping);
    free(query);
    modbus_free(ctx);

    return 0;
}
예제 #10
0
int slave()
{
    modbus_t *ctx;
    modbus_mapping_t *mb_mapping;
    int rc;
    uint8_t *query;
	uint8_t *raw_rsp;
	uint8_t *con_rsp;
	uint16_t *raw_rd_rsp;
	uint16_t *rd_rsp;
    int header_length;
	int year,month,day,hour,minute,second;
	int i,j;
	int rsp_length;
	int set_date[6];
	mb_mapping = modbus_mapping_new(
        UT_BITS_ADDRESS + UT_BITS_NB,
        UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
        UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
        UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
    if (mb_mapping == NULL) 
	{
        modbus_free(ctx);
        return -1;
    }
    ctx = modbus_new_rtu("/dev/ttyS2", 9600, 'N', 8, 1);
    modbus_set_slave(ctx, 1);
    query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
	raw_rsp = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
	con_rsp = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
	raw_rd_rsp = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
	rd_rsp = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
    header_length = modbus_get_header_length(ctx);

    modbus_set_debug(ctx, TRUE);
start:
    rc = modbus_connect(ctx);
    if (rc == -1) 
    {
		modbus_free(ctx);
		return -1;
    }
    for (;;) 
    {
        rc = modbus_receive(ctx, query);
        if (rc == -1) 
		{
            break;
   	    }
		//Send the raw message to SDL
		if(extend(query[2],query[3]) == 5152)
		{
			
			raw_rsp = raw_data_command(&rsp_length,rc,query);
			if(raw_rsp == NULL){printf("SDL is sleeping\n");goto close;}
			for(i=6;i<rsp_length;i++)
			{
				con_rsp[i-6]=raw_rsp[i];
			}
			modbus_send_raw_request(ctx, con_rsp, (rsp_length-6)*sizeof(uint8_t));
		}
		if(extend(query[2],query[3]) == 5168)
		{
			
			raw_rsp = raw_data_command(&rsp_length,rc,query);
			if(raw_rsp == NULL){printf("SDL is sleeping\n");goto close;}
			for(i=6;i<rsp_length;i++)
			{
				con_rsp[i-6]=raw_rsp[i];
			}
			modbus_send_raw_request(ctx, con_rsp, (rsp_length-6)*sizeof(uint8_t));
		}
		if(extend(query[2],query[3]) == 16384) 
		{	
			raw_rsp = sdl_read_buffsize(&rsp_length);
			if(raw_rsp == NULL){printf("SDL is sleeping\n");goto close;}
			for(i=0;i<2;i++)
			{
				con_rsp[i] = query[i];
			}
			con_rsp[2] = 0x4;
			for(i=0,j=3;i<rsp_length;i++)
			{
				con_rsp[j]=raw_rsp[i];
				j++;
			}
			modbus_send_raw_request(ctx, con_rsp, 7*sizeof(uint8_t));	
		}
		if(extend(query[2],query[3]) == 16386) 
		{	
			raw_rsp = sdl_read_data(&rsp_length,16);
			if(raw_rsp == NULL){printf("SDL is sleeping\n");goto close;}
			for(i=0;i<2;i++)
			{
				con_rsp[i] = query[i];
			}
			con_rsp[2] = 0x40;
			for(i=0,j=3;i<rsp_length;i++)
			{
				con_rsp[j]=raw_rsp[i];
				j++;
			}
			modbus_send_raw_request(ctx, con_rsp, 67*sizeof(uint8_t));	
		}
		
		if(extend(query[2],query[3]) == 1400) 
		{
			rc = modbus_reply(ctx, query, rc, mb_mapping);				
			set_date[0] = extend(query[7],query[8])+2000;
			set_date[1] = extend(query[9],query[10]);
			set_date[2]= extend(query[11],query[12]);
			set_date[3] = extend(query[13],query[14]);
			set_date[4] = extend(query[15],query[16]);
			set_date[5] = extend(query[17],query[18]);
			set_time(&set_date);
			//printf("%d %d %d %d %d %d\n",set_date[0],set_date[1],set_date[2],set_date[3],set_date[4],set_date[5]);
		}
		
    }
close:
	goto start;
    modbus_mapping_free(mb_mapping);
    free(query);
    modbus_free(ctx);
	

    return 0;
}
예제 #11
0
int main(int argc, char *argv[]) {
    modbus_t       *ctx_tcp;
    modbus_t    *ctx_serial;

    uint8_t        *query;
    uint8_t         *raw_query;
    uint8_t        *raw_reply;
    int             header_length;
    int             socket;
    int rc;

    modbus_mapping_t *mb_mapping;
    modbus_mapping_t *local_mb_mapping;

    const int RAW_REQ_LENGTH = 10;
    int RTU;
    int exitFlag = 0; // Set to non-zero to exit.
    //    int opt=1;
    int cnt=1;

    //    int tty_len=0;

    int baud_rate=9600;
    char parity='N';
    int length=8;
    int stop_bits=2;
    char *tty=(char *)NULL;
    char *ip=(char *)NULL;
    int port=1502;
    int verbose=1;
    int io_address=0;
    int tmp_address=0;

    uint16_t *localInputRegisters;
    int localInputRegistersSize;

    uint16_t *localHoldingRegisters;
    int localHoldingRegistersSize;

    int holdingShmKey=0;
    int holdingShmId=0;

    int holdingSemKey=0;
    int holdingSemId=0;


    int inputShmKey=0;
    int inputShmId=0;

    int inputSemKey=0;
    int inputSemId=0;

    int i;
    unsigned char *tmp;
    int ch;    // for getopt
    char *iniFile=(char *)NULL;
    char *ptr;

    int useSharedInput=0;
    int useSharedHolding=0;
    int modbusDebug = 0;
    int localRTU = 0;
    int serialRTUenabled = 0;

    char *iniFileName[] = {
        "/etc/newServer.ini",
        "/usr/local/etc/newServer.ini",
        "./newServer.ini"
    };

    while ((ch = getopt(argc,argv,"b:c:h?i:p:P:t:v")) != -1) {
        switch (ch) {
            case 'c':
                if ( iniFile != (char *)NULL) {
                    free(iniFile);
                }
                iniFile = strsave( optarg );

                if(verbose)
                    printf("Set ini file to %s\n",iniFile);
                break;
            case 'n':   // baud rate
                baud_rate=atoi(optarg);
                break;
            case 'h':
            case '?':
                usage();
                exit(0);
                break;
            case 'i':
                if ( ip != (char *)NULL) {
                    free(ip);
                }
                ip=strsave( optarg );
                break;
            case 'p':
                port=atoi( optarg );
                break;
            case 'P':
                parity=optarg[0];

                if ('E' != parity && 'O' != parity && 'N' != parity) {
                    fprintf(stderr,"Invalid parity %c, exiting.\n",parity);
                    exit(-1);
                }
                break;
            case 't':   // serial port
                if ( tty != (char *)NULL) {
                    free( tty );
                }
                tty=strsave( optarg );
                break;
            case 'v':
                verbose=1;
                break;
            default:
                break;
        }
    }

    if (iniFile == (char *)NULL) {
        for(i=0;(i<3 && (iniFile == (char *)NULL))  ;i++) {

            if ( verbose) {
                printf("%d:%s\n",i, iniFileName[i]);
            }

            if( 0 == access( iniFileName[i],R_OK)) {
                if(verbose ) {
                    printf("\tFile exists\n");
                }
                iniFile = strsave( iniFileName[i]);
            }
        }
    }    

    if (iniFile == (char *)NULL ) {
        printf("No existing ini file.\n");
        // Create an default file

        iniFile=strsave("./newServer.ini");
        printf("\tCreating new ini file %s\n", iniFile);

        createIni( iniFile );
        //        exit(0);
    }

    ini = iniparser_load( iniFile );
    ptr= iniparser_getstring(ini,"network:IP","127.0.0.1");

    if((char *)NULL == ip) {
        ip=strsave(ptr);
    }

    if (0 == port) {
        port = iniparser_getint(ini,"network:port",1502);
    }

    if (-1 == verbose) {
        verbose = iniparser_getboolean(ini,"system:verbose",0);
    }

    localHoldingRegistersSize = iniparser_getint(ini,"system:holding_reg_size", LOCAL_HOLDING_REGISTERS);

    localInputRegistersSize = iniparser_getint(ini,"system:input_reg_size", LOCAL_INPUT_REGISTERS);

    useSharedInput = iniparser_getboolean(ini,"system:share_input",0);
    useSharedHolding = iniparser_getboolean(ini,"system:share_holding",0);

    modbusDebug = iniparser_getboolean(ini,"modbus:debug",0);

    localRTU = iniparser_getint(ini,"modbus:LOCAL_RTU",255);

    serialRTUenabled = iniparser_getboolean(ini,"modbus:RTU",0);

    if( serialRTUenabled ) {
        if( (char *) NULL == tty) {
            tty = iniparser_getstring(ini,"modbus:tty","/dev/ttyUSB0");
        }
    } else {
        tty = (char *)NULL;
    }


    if(verbose) {
        printf("\n\t\tSettings\n");
        printf(  "\t\t========\n\n");
        printf("\tBuild Date\t:%s\n",__DATE__);
        printf("\tAddress\t\t:%s\n",ip);
        printf("\tNetwork port\t:%d\n",port);
        printf("\tSerial port\t:%s\n",tty);
        printf("\tBaud rate\t:%d\n",baud_rate);
        printf("\tParity\t\t:%c\n",parity);
    } else {
        printf("\nStarted.\n");
    }

    if (serialRTUenabled ) {
        if(access(tty,F_OK) == 0) {
            printf("Serial port %s exists.....\n",tty);
            // 
            // Now test that it is readable, writable.
            //
        } else {
            printf("Serial port %s does NOT exist\n",tty);
            printf("Disabling serial RTU.\n");
            free( tty );
            tty = (char *)NULL;
            serialRTUenabled=0;
        }
    }

    query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
    raw_query = malloc(RAW_REQ_LENGTH);
    raw_reply = malloc(MODBUS_TCP_MAX_ADU_LENGTH);

    mb_mapping = modbus_mapping_new(
            MODBUS_MAX_BITS,   // Coils
            MODBUS_MAX_BITS,   // Discrete inputs
            MODBUS_MAX_REGISTERS,   // Holding registers
            MODBUS_MAX_REGISTERS);  // Input registers

    local_mb_mapping = modbus_mapping_new(
            NULL,   // Coils
            NULL,   // Discrete inputs
            LOCAL_HOLDING_REGISTERS,   // Holding registers
            LOCAL_INPUT_REGISTERS);  // Input registers

    // 
    // Setup the memory for the holding registers.
    //
    if ( useSharedHolding ) {
        if (verbose)
            printf("Share the Holding regsisters R/W\n");

        localHoldingRegisters = local_mb_mapping->tab_registers;

        holdingShmKey = iniparser_getint(ini, "system:holding_reg_key",110);
        holdingSemKey = iniparser_getint(ini, "system:holding_reg_sema",111);

        holdingShmId = shmget(holdingShmKey, localHoldingRegistersSize, IPC_CREAT | 0600 );  // change to 0400

        if (holdingShmId < 0) {
            perror("Holding Reg shmget");
            exit(3);
        }
        local_mb_mapping->tab_registers = (uint16_t *)shmat(holdingShmId,NULL,0);
        if (local_mb_mapping->tab_registers < 0) {
            perror("Holding Reg shmat");
            exit(4);
        }

        /* 
         * Create the seamphore 
         */
        holdingSemId = semget( holdingSemKey, 1, IPC_CREAT | 0600 );
        if (-1 == holdingSemId) {
            perror("Input reg semaphore");
            exit(2);
        }
        /*
         *         Shared memory created, semaphore created (default is locked)
         *         release the seamphore !
         */
        rc = semctl(holdingSemId, 0, SETVAL, 1);

    }
    // 
    // Setup the memory for the shared input registers.
    // 
    if ( useSharedInput ) {
        if (verbose)
            printf("Share the Input regsisters R/W\n");

        localInputRegisters = local_mb_mapping->tab_input_registers; // Save the current pointer

        inputShmKey = iniparser_getint(ini, "system:input_reg_key",110);
        inputSemKey = iniparser_getint(ini, "system:input_reg_sema",111);

        inputShmId = shmget(inputShmKey, localInputRegistersSize, IPC_CREAT | 0600 );  // change to 0400

        if (inputShmId < 0) {
            perror("Input Reg shmget");
            exit(3);
        }
        local_mb_mapping->tab_input_registers = (uint16_t *)shmat(inputShmId,NULL,0);

        if ( local_mb_mapping->tab_input_registers < 0) {
            perror("Input Reg shmat");
            exit(1);
        }

        /* 
         * create the seamphore 
         */
        inputSemId = semget( inputSemKey, 1, IPC_CREAT | 0600 );
        if (-1 == inputSemId) {
            perror("Input reg semaphore");
            exit(2);
        }
        /*
         *         Shared memory created, semaphore created (default is locked)
         *         release the seamphore !
         */
        rc = semctl(inputSemId, 0, SETVAL, 1);

    }

    memset( localInputRegisters,0x00, LOCAL_INPUT_REGISTERS);
    memset( localHoldingRegisters,0x00, LOCAL_HOLDING_REGISTERS);

    local_mb_mapping->tab_input_registers[0]=0xaa55;

    ctx_tcp = modbus_new_tcp(ip, port);

    if ( tty != (char *)NULL ) {
        ctx_serial = modbus_new_rtu(tty, baud_rate, parity, length, stop_bits);
    }

    while ( !exitFlag ) {
        header_length = modbus_get_header_length(ctx_tcp);

        if(verbose) {
            modbus_set_debug(ctx_tcp, TRUE);
            //            modbus_set_debug(ctx_serial, TRUE);
        }

        do {
            socket = modbus_tcp_listen(ctx_tcp, 5);
            if (socket < 0) {
                printf("%04d: Socket in use.\n",cnt++);
                sleep(1);
            }
        } while(socket < 0);
        //        printf("modbus_tcp_listen   =%d\n",socket);
        if (socket == -1) {
            printf("\t: %s\n", modbus_strerror(errno));
        }

        rc=modbus_tcp_accept(ctx_tcp, &socket);

        //        printf("modbus_tcp_accept rc=%d\n",rc);
        if (rc == -1) {
            fprintf(stderr,"FATAL ERROR: %s\n", modbus_strerror(errno));
            exit(-1);
        }

        while ( 1 ) {
            do {
                if(verbose) {
                    printf("Recieve.\n");
                }
                rc = modbus_receive(ctx_tcp, query);
                //                printf("rc=%d\n",rc);
                /* Filtered queries return 0 */
            } while (rc == 0);

            if (-1 == rc) {
                if( verbose ) {
                    printf("Client disconnected.\n");
                }
                break;
            }
            RTU=query[header_length - 1];
            io_address= ( query[header_length+1] << 8) + (query[header_length+2] & 0xff);

            printf("IO Address=%04x\n",io_address);

            if (verbose) {
                if( localRTU == RTU ) {
                    printf("It's for me !\n");
                } else {
                    printf("RTU Id: %02d\n",RTU);
                }

                printf("IO Address=0x%04x\n",io_address);
                printf("Seq Number=0x%04x\n", query[1]);
            }   

            if( localRTU == RTU ) {
                //
                // Local mapping
                //
                int len;
                uint16_t data;
                int cnt=0;

                // Need to add address range checks, and return an error 
                // for an invalid function.

                switch (query[header_length]) {
                    case 0x03:  // read holding registers
                    case 0x04:  // read input registers
                        rc = modbus_reply(ctx_tcp, query, rc, local_mb_mapping);
                        break;
                    case 0x06: //write single register
                        swab(&query[8],&len,2);
                        swab(&query[10],&data,2);
                        printf("Data=%02d\n",data);

                        local_mb_mapping->tab_registers[io_address]=data;
                        rc = modbus_reply(ctx_tcp, query, rc, local_mb_mapping);
                        break;
                    case 0x10: // Write multiple registers
                        //                        len=query[7];
                        swab(&query[10],&len,2);
                        printf("Length=%02d\n",len);

                        for (i=13; i< (13 + len ); i +=2) {
                            data= (query[i] << 8) + (query[i+1] & 0xff);
                            printf("%04x\n",data);

                            local_mb_mapping->tab_registers[ io_address + cnt++ ] = data;
                        }
                        rc = modbus_reply(ctx_tcp, query, rc, local_mb_mapping);
                        break;

                    default:
                        break;
                }
            } else {
                if(serialRTUenabled)  {

                    modbus_set_slave( ctx_serial,RTU);
                    if( -1 == modbus_connect( ctx_serial)) {
                        fprintf(stderr, "Connection failed: %s\n",modbus_strerror(errno));
                    }
                    switch( query[header_length] ) {
                        case 0x06:
                            printf("Write Single Register %d\n",RTU);
                            memcpy(raw_query,&query[header_length-1], 6);
                            rc=modbus_send_raw_request( ctx_serial,raw_query,6);
                            modbus_receive_confirmation(ctx_serial, raw_reply);
                            rc = modbus_reply(ctx_tcp, query, rc, mb_mapping);

                            break;
                        case 0x03:
                        case 0x04:
                            printf("Read Registers %d\n",RTU);
                            break;
                        case 0x10:
                            {
                                int len=0;
                                //	      modbus_set_debug(ctx_serial, TRUE);

                                printf("Write multiple registers %d\n",RTU);
                                len=query[header_length - 2];
                                printf("Length %d\n",len);
                                memcpy(raw_query,&query[header_length-1], len);

                                rc=modbus_send_raw_request( ctx_serial,raw_query,len);
                                if( -1 == rc) {
                                    printf("modbus_send_raw_request: %s\n",modbus_strerror(errno));
                                } else {
                                    modbus_receive_confirmation(ctx_serial, raw_reply);
                                    rc = modbus_reply(ctx_tcp, query, rc, mb_mapping);
                                    printf("rc =  %d\n",rc);
                                    //		modbus_set_debug(ctx_serial, FALSE);
                                }
                            } 
                            break;
                    }

                    if( (0x03 == query[header_length]) || (0x04 == query[header_length])) {
                        int len;

                        if(verbose) {
                            switch (query[header_length]) {
                                case 0x03:
                                    printf("Read Holding Registers.\n");
                                    break;
                                case 0x04:
                                    printf("Read Input Registers.\n");
                                    break;
                                default:
                                    break;
                            }
                        }

                        memcpy(raw_query,&query[header_length-1], 6);

                        modbus_set_slave( ctx_serial, query[header_length-1]);

                        if( -1 == modbus_connect( ctx_serial)) {
                            fprintf(stderr, "Connection failed: %s\n",modbus_strerror(errno));
                        }

                        rc=modbus_send_raw_request( ctx_serial,raw_query,6);

                        if( -1 == rc ) {
                            printf("modbus_send_raw_request: %s\n",modbus_strerror(errno));
                        }

                        modbus_receive_confirmation(ctx_serial, raw_reply);
                        /*
                         * This next loop swaps bytes in words.
                         * If this is built and running on a little endian 
                         * machine (most machines are these days)
                         * Then this needs to be done.  
                         * If on a big endian machine (M68000 family) just 
                         * comment this out.
                         *                     
                         * It might be worth having a command line switch,
                         * detect endianness.
                         *                     
                         */

                        switch (query[header_length]) {
                            case 0x03:
                                printf("Read Holding Registers.\n");
                                tmp=(unsigned char *)mb_mapping->tab_registers;
                                break;
                            case 0x04:
                                printf("Read Input Registers.\n");
                                tmp=(unsigned char *)mb_mapping->tab_input_registers;
                                break;
                            default:
                                break;
                        }

                        tmp_address = (io_address*2);

                        for(i=0;i<raw_reply[2];i=i+2 ) {
                            tmp[tmp_address+i]=raw_reply[i+4];
                            tmp[tmp_address+i+1]=raw_reply[i+3];
                        }

                        for(i=0;i<10;i++) {
                            if (verbose) {
                                printf("!%02x!",raw_reply[i]);
                            }
                        }
                        printf("\n");

                        for(i=0;i<10;i++) {
                            if (verbose) {
                                printf("+%02x+",tmp[i]);
                            }
                        }
                        printf("\n");

                        //
                        // compute packet length by getting the data length 
                        // and then add 2.
                        // Then overwrite the outbound data.
                        //
                        len=raw_reply[2] + 3;
                        rc = modbus_reply(ctx_tcp, query, rc, mb_mapping);

                        //    memcpy(  mb_mapping->tab_input_registers, &raw_reply[3], raw_reply[2]);

                        //    MODBUS_SET_INT16_TO_INT8(query, header_length + 3, UT_REGISTERS_NB_SPECIAL - 1);

                    }
                }
            }

            //    printf("Reply with an invalid TID or slave\n");
            //    rc=modbus_reply_exception(ctx_tcp, query, 1);
            //    rc=modbus_send_raw_request(ctx_tcp, raw_reply, 10 * sizeof(uint8_t));


            //      rc = modbus_reply(ctx_tcp, query, rc, mb_mapping);
        }

        if( verbose ) {
            printf("Tidying up\n");
        }
        close(socket);

        usleep( 1000 ); // wait 1 ms

    }
    printf("simpleServer exiting.\n");
    modbus_close(ctx_tcp);
    modbus_free(ctx_tcp);
    free(query);
    modbus_mapping_free(mb_mapping);
    return(0);
}
예제 #12
0
int main(void)
{
    int master_socket;
    int rc;
    fd_set refset;
    fd_set rdset;

    /* Maximum file descriptor number */
    int fdmax;

    ctx = modbus_new_tcp("127.0.0.1", 1502);

    mb_mapping = modbus_mapping_new(MODBUS_MAX_READ_BITS, 0,
                                    MODBUS_MAX_READ_REGISTERS, 0);
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    server_socket = modbus_listen(ctx, NB_CONNECTION);

    signal(SIGINT, close_sigint);

    /* Clear the reference set of socket */
    FD_ZERO(&refset);
    /* Add the server socket */
    FD_SET(server_socket, &refset);

    /* Keep track of the max file descriptor */
    fdmax = server_socket;

    for (;;) {
        rdset = refset;
        if (select(fdmax+1, &rdset, NULL, NULL, NULL) == -1) {
            perror("Server select() failure.");
            close_sigint(1);
        }

        /* Run through the existing connections looking for data to be
         * read */
        for (master_socket = 0; master_socket <= fdmax; master_socket++) {

            if (FD_ISSET(master_socket, &rdset)) {
                if (master_socket == server_socket) {
                    /* A client is asking a new connection */
                    socklen_t addrlen;
                    struct sockaddr_in clientaddr;
                    int newfd;

                    /* Handle new connections */
                    addrlen = sizeof(clientaddr);
                    memset(&clientaddr, 0, sizeof(clientaddr));
                    newfd = accept(server_socket, (struct sockaddr *)&clientaddr, &addrlen);
                    if (newfd == -1) {
                        perror("Server accept() error");
                    } else {
                        FD_SET(newfd, &refset);

                        if (newfd > fdmax) {
                            /* Keep track of the maximum */
                            fdmax = newfd;
                        }
                        printf("New connection from %s:%d on socket %d\n",
                               inet_ntoa(clientaddr.sin_addr), clientaddr.sin_port, newfd);
                    }
                } else {
                    /* An already connected master has sent a new query */
                    uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];

                    rc = modbus_receive(ctx, master_socket, query);
                    if (rc != -1) {
                        modbus_reply(ctx, query, rc, mb_mapping);
                    } else {
                        /* Connection closed by the client, end of server */
                        printf("Connection closed on socket %d\n", master_socket);
                        close(master_socket);

                        /* Remove from reference set */
                        FD_CLR(master_socket, &refset);

                        if (master_socket == fdmax) {
                            fdmax--;
                        }
                    }
                }
            }
        }
    }

    return 0;
}
예제 #13
0
int main(void)
{
	int rc;
	iolib_init();
	iolib_setdir(9,12, DIR_OUT);

    int socket;
    modbus_t *ctx;
    modbus_mapping_t *mb_mapping;

    ctx = modbus_new_tcp("192.168.1.100", 502);
    modbus_set_debug(ctx, TRUE);

    mb_mapping = modbus_mapping_new(10, 10, 10, 10);
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    socket = modbus_tcp_listen(ctx, 1);
    modbus_tcp_accept(ctx, &socket);

    for (;;) {
        uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
        int rc;

        rc = modbus_receive(ctx, query);



	if (rc != -1) {
            /* rc is the query size */
            modbus_reply(ctx, query, rc, mb_mapping);

    if((*mb_mapping).tab_bits[1] == 1) {
		 pin_high(9,12);
    }

    else if((*mb_mapping).tab_bits[1] == 0) {
		 pin_low(9,12);
   }





	} else {
            /* Connection closed by the client or error */
            break;
        }
    }



    printf("Quit the loop: %s\n", modbus_strerror(errno));

    modbus_mapping_free(mb_mapping);
    close(socket);
    modbus_free(ctx);

    return 0;
}
예제 #14
0
int main(int argc, char *argv[])
{
	int addr;
	modbus_mapping_t *mb_mapping;
	modbus_t *ctx;
	int s;
	int ret;

	/*
	 * Check command line
	 */

	optind = check_common_opts(argc, argv);
	if (argc - optind < 1)
		usage();

	addr = parse_addr(argv[optind + 0]);
	if (addr < 1) {
		err("invalid address \"%s\"", argv[optind + 0]);
		exit(-1);
	}

	/*
	 * Modbus stuff
	 */

	mb_mapping = modbus_mapping_new(10, 10, 10, 10);
	if (mb_mapping == NULL) {
		err("failed to allocate the mapping: %s\n",
			modbus_strerror(errno));
		exit(-1);
	}

	ctx = modbus_server_new(addr, &s);
	if (!ctx) {
		err("cannot create server: %s", modbus_strerror(errno));
		exit(1);
	}

	for (;;) {
		ret = modbus_server_connect(ctx, &s);
		if (ret < 0) {
			err("connection failed: %s", modbus_strerror(errno));
			exit(1);
		}
		dbg("got new connection");

		for (;;) {
			uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
	
			ret = modbus_receive(ctx, query);
			dbg("got new query");
			if (ret >= 0) {
				modbus_reply(ctx, query, ret, mb_mapping);
				dbg("query answered");
			} else {
				/* Connection closed by the client or server */
				break;
			}
		}
	}

	printf("Quit the loop: %s\n", modbus_strerror(errno));

	modbus_mapping_free(mb_mapping);

	modbus_close(ctx);
	modbus_free(ctx);

	return 0;
}
예제 #15
0
int main(void)
{
    int s = -1;
    modbus_t *ctx;
    modbus_t *ctx_rtu;

    ctx = modbus_new_tcp("127.0.0.1", 1502);
    /* modbus_set_debug(ctx, TRUE); */

    ctx_rtu = modbus_new_rtu("/dev/ttyUSB1", 115200, 'N', 8, 1);

    if (modbus_connect(ctx_rtu) == -1) {
        fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
        modbus_free(ctx_rtu);
        modbus_free(ctx);
        return -1;
    }

    s = modbus_tcp_listen(ctx, 1);
    modbus_tcp_accept(ctx, &s);

    for (;;) {
        uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];
        int query_length;
        uint8_t response[MODBUS_RTU_MAX_ADU_LENGTH];
        int rc;
        int data_start_index;
        int raw_length;
        int exception;
        int slave;

        rc = modbus_receive(ctx, query);
        if (rc > 0) {
            exception = 0;
            query_length = rc;
            data_start_index = modbus_get_header_length(ctx) - 1;
            slave = query[data_start_index];
            raw_length = rc - data_start_index - modbus_get_checksum_length(ctx);
            modbus_flush(ctx_rtu);
            modbus_set_slave(ctx_rtu, slave);
            if (modbus_send_raw_request(ctx_rtu, query + data_start_index, raw_length) != -1) {
                rc = modbus_receive_confirmation(ctx_rtu, response);
                if ( rc != -1) {
                    /* rc is the response size */
                    data_start_index = modbus_get_header_length(ctx_rtu) - 1;
                    raw_length = rc - data_start_index - modbus_get_checksum_length(ctx_rtu);
                    modbus_reply_raw_response(ctx, query, query_length, response + data_start_index, raw_length);
                } else {
                    exception = errno;
                }
            } else {
                exception = errno;
            }

            if (exception != 0) {
                if (exception > MODBUS_ENOBASE && MODBUS_ENOBASE < (MODBUS_ENOBASE + MODBUS_EXCEPTION_MAX)) {
                    exception -= MODBUS_ENOBASE;
                } else {
                    exception = EMBXSFAIL;
                }
                modbus_reply_exception(ctx, query, exception);
            }
        } else if (rc == -1) {
            /* Connection closed by the client or error */
            break;
        }
    }

    printf("Quit the loop: %s\n", modbus_strerror(errno));

    if (s != -1) {
        close(s);
    }

    modbus_close(ctx_rtu);
    modbus_free(ctx_rtu);

    modbus_close(ctx);
    modbus_free(ctx);

    return 0;
}
예제 #16
0
int main(int argc, char*argv[])
{
    int s = -1;
    modbus_t *ctx;
    modbus_mapping_t *mb_mapping;
    int rc;
    int i;
    int use_backend;
    uint8_t *query;
    int header_length;

    if (argc > 1) {
        if (strcmp(argv[1], "tcp") == 0) {
            use_backend = TCP;
        } else if (strcmp(argv[1], "tcppi") == 0) {
            use_backend = TCP_PI;
        } else if (strcmp(argv[1], "rtu") == 0) {
            use_backend = RTU;
        } else {
            printf("Usage:\n  %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv[0]);
            return -1;
        }
    } else {
        /* By default */
        use_backend = TCP;
    }

    if (use_backend == TCP) {
        ctx = modbus_new_tcp("127.0.0.1", 1502);
        query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
    } else if (use_backend == TCP_PI) {
        ctx = modbus_new_tcp_pi("::0", "1502");
        query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
    } else {
        ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
        modbus_set_slave(ctx, SERVER_ID);
        query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
    }
    header_length = modbus_get_header_length(ctx);

    modbus_set_debug(ctx, TRUE);

    mb_mapping = modbus_mapping_new(
        UT_BITS_ADDRESS + UT_BITS_NB,
        UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
        UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
        UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    /* Unit tests of modbus_mapping_new (tests would not be sufficient if two
       nb_* were identical) */
    if (mb_mapping->nb_bits != UT_BITS_ADDRESS + UT_BITS_NB) {
        printf("Invalid nb bits (%d != %d)\n", UT_BITS_ADDRESS + UT_BITS_NB, mb_mapping->nb_bits);
        modbus_free(ctx);
        return -1;
    }

    if (mb_mapping->nb_input_bits != UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB) {
        printf("Invalid nb input bits: %d\n", UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB);
        modbus_free(ctx);
        return -1;
    }

    if (mb_mapping->nb_registers != UT_REGISTERS_ADDRESS + UT_REGISTERS_NB) {
        printf("Invalid nb registers: %d\n", UT_REGISTERS_ADDRESS + UT_REGISTERS_NB);
        modbus_free(ctx);
        return -1;
    }

    if (mb_mapping->nb_input_registers != UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB) {
        printf("Invalid nb input registers: %d\n", UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
        modbus_free(ctx);
        return -1;
    }

    /* Examples from PI_MODBUS_300.pdf.
       Only the read-only input values are assigned. */

    /** INPUT STATUS **/
    modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
                               UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
                               UT_INPUT_BITS_TAB);

    /** INPUT REGISTERS **/
    for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
        mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
            UT_INPUT_REGISTERS_TAB[i];;
    }

    if (use_backend == TCP) {
        s = modbus_tcp_listen(ctx, 1);
        modbus_tcp_accept(ctx, &s);
    } else if (use_backend == TCP_PI) {
        s = modbus_tcp_pi_listen(ctx, 1);
        modbus_tcp_pi_accept(ctx, &s);
    } else {
        rc = modbus_connect(ctx);
        if (rc == -1) {
            fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
            modbus_free(ctx);
            return -1;
        }
    }

    for (;;) {
        do {
            rc = modbus_receive(ctx, query);
            /* Filtered queries return 0 */
        } while (rc == 0);

        /* The connection is not closed on errors which require on reply such as
           bad CRC in RTU. */
        if (rc == -1 && errno != EMBBADCRC) {
            /* Quit */
            break;
        }

        /* Special server behavior to test client */
        if (query[header_length] == 0x03) {
            /* Read holding registers */

            if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
                == UT_REGISTERS_NB_SPECIAL) {
                printf("Set an incorrect number of values\n");
                MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
                                         UT_REGISTERS_NB_SPECIAL - 1);
            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
                       == UT_REGISTERS_ADDRESS_SPECIAL) {
                printf("Reply to this special register address by an exception\n");
                modbus_reply_exception(ctx, query,
                                       MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
                continue;
            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
                       == UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE) {
                const int RAW_REQ_LENGTH = 5;
                uint8_t raw_req[] = {
                    (use_backend == RTU) ? INVALID_SERVER_ID : 0xFF,
                    0x03,
                    0x02, 0x00, 0x00
                };

                printf("Reply with an invalid TID or slave\n");
                modbus_send_raw_request(ctx, raw_req, RAW_REQ_LENGTH * sizeof(uint8_t));
                continue;
            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
                       == UT_REGISTERS_ADDRESS_SLEEP_500_MS) {
                printf("Sleep 0.5 s before replying\n");
                usleep(500000);
            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
                       == UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS) {
                /* Test low level only available in TCP mode */
                /* Catch the reply and send reply byte a byte */
                uint8_t req[] = "\x00\x1C\x00\x00\x00\x05\xFF\x03\x02\x00\x00";
                int req_length = 11;
                int w_s = modbus_get_socket(ctx);

                /* Copy TID */
                req[1] = query[1];
                for (i=0; i < req_length; i++) {
                    printf("(%.2X)", req[i]);
                    usleep(5000);
                    send(w_s, (const char*)(req + i), 1, MSG_NOSIGNAL);
                }
                continue;
            }
        }

        rc = modbus_reply(ctx, query, rc, mb_mapping);
        if (rc == -1) {
            break;
        }
    }

    printf("Quit the loop: %s\n", modbus_strerror(errno));

    if (use_backend == TCP) {
        if (s != -1) {
            close(s);
        }
    }
    modbus_mapping_free(mb_mapping);
    free(query);
    /* For RTU */
    modbus_close(ctx);
    modbus_free(ctx);

    return 0;
}
예제 #17
0
        mb_mapping = modbus_mapping_new(NULL, 						// no coils
										NULL,						// no coils
										MODBUS_MAX_READ_REGISTERS, 	// holding reg
										NULL);						// no ir's
                                    
        if (mb_mapping == NULL) {
            \\fprintf(stderr, "Failed to allocate the mapping: %s\n", modbus_strerror(errno));
            modbus_free(ctx);
            return;
        }
    }
    }

    uint8_t query[MODBUS_TCP_MAX_ADU_LENGTH];

    rc = modbus_receive(ctx, query);
    if (rc >= 0) {
        modbus_reply(ctx, query, rc, mb_mapping);
    } else {
        modbus_mapping_free(mb_mapping);
        close(socket);
        modbus_free(ctx);
    }
}

int main(int argc, char *argv[])
{
    
    
    setup();
예제 #18
0
int main(int argc, char*argv[])
{
    int socket,client_socket;
	
    modbus_t *ctx;
    modbus_mapping_t *mb_mapping;
    int rc;
    int i;
    int use_backend;
    uint8_t *query;
    int header_length;

    if (argc > 1) {
        if (strcmp(argv[1], "tcp") == 0) {
            use_backend = TCP;
        } else if (strcmp(argv[1], "utcp") == 0 ) {
			use_backend = UNIX_TCP;
		} else if (strcmp(argv[1], "tcppi") == 0) {
            use_backend = TCP_PI;
        } else if (strcmp(argv[1], "rtu") == 0) {
            use_backend = RTU;
        } else {
            printf("Usage:\n  %s [tcp|tcppi|rtu] - Modbus server for unit testing\n\n", argv[0]);
            return -1;
        }
    } else {
        /* By default */
        use_backend = UNIX_TCP;
    }

	//signal(SIGINT, signal_handler);
	
    if (use_backend == TCP) {
        ctx = modbus_new_tcp("127.0.0.1", 502);
        query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
    } else if (use_backend == UNIX_TCP) {
		ctx = modbus_new_unix_tcp("/tmp/modbus.sock");
        query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
	} else if (use_backend == TCP_PI) {
        ctx = modbus_new_tcp_pi("::0", "1502");
        query = malloc(MODBUS_TCP_MAX_ADU_LENGTH);
    } else {
        ctx = modbus_new_rtu("/dev/ttyUSB0", 115200, 'N', 8, 1);
        modbus_set_slave(ctx, SERVER_ID);
        query = malloc(MODBUS_RTU_MAX_ADU_LENGTH);
    }
	
	header_length = modbus_get_header_length(ctx);
	modbus_set_debug(ctx, TRUE);
	//by jesse
	modbus_init_cb(ctx, &cb);
	//end

    mb_mapping = modbus_mapping_new(
        UT_BITS_ADDRESS + UT_BITS_NB,	// read buffer : start address + number
        UT_INPUT_BITS_ADDRESS + UT_INPUT_BITS_NB,
        UT_REGISTERS_ADDRESS + UT_REGISTERS_NB,
        UT_INPUT_REGISTERS_ADDRESS + UT_INPUT_REGISTERS_NB);
		
		
    if (mb_mapping == NULL) {
        fprintf(stderr, "Failed to allocate the mapping: %s\n",
                modbus_strerror(errno));
        modbus_free(ctx);
        return -1;
    }

    /* Examples from PI_MODBUS_300.pdf.
       Only the read-only input values are assigned. */

    /** INPUT STATUS **/	
    modbus_set_bits_from_bytes(mb_mapping->tab_input_bits,
                               UT_INPUT_BITS_ADDRESS, UT_INPUT_BITS_NB,
                               UT_INPUT_BITS_TAB);

    /** INPUT REGISTERS **/
    for (i=0; i < UT_INPUT_REGISTERS_NB; i++) {
        mb_mapping->tab_input_registers[UT_INPUT_REGISTERS_ADDRESS+i] =
            UT_INPUT_REGISTERS_TAB[i];;
    }

    if (use_backend == TCP) {
        socket = modbus_tcp_listen(ctx, 1);
    } else if (use_backend == UNIX_TCP) {
		socket = modbus_unix_tcp_listen(ctx, 1);
	} else if (use_backend == TCP_PI) {
        socket = modbus_tcp_pi_listen(ctx, 1);
    } else {
        client_socket = modbus_connect(ctx);
        if (rc == -1) {
            fprintf(stderr, "Unable to connect %s\n", modbus_strerror(errno));
            modbus_free(ctx);
            return -1;
        }
    }
	
	// for unix sock
    while (running) {
		if (use_backend == TCP) {
			client_socket = modbus_tcp_accept(ctx, &socket);
		} else if (use_backend == UNIX_TCP) {
			client_socket = modbus_unix_tcp_accept(ctx, &socket);
		} else if (use_backend == TCP_PI) {
			client_socket = modbus_tcp_pi_accept(ctx, &socket);
		} else {
			// RTU : nothing
		}
	
        rc = modbus_receive(ctx, query);
        if (rc == -1) {
            /* Connection closed by the client or error */
			if ( use_backend != RTU)
				close(client_socket);
            continue;
        }

        /* Read holding registers */
        if (query[header_length] == 0x03) {
            if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 3)
                == UT_REGISTERS_NB_SPECIAL) {
                printf("Set an incorrect number of values\n");
                MODBUS_SET_INT16_TO_INT8(query, header_length + 3,
                                         UT_REGISTERS_NB_SPECIAL - 1);
            } else if (MODBUS_GET_INT16_FROM_INT8(query, header_length + 1)
                == UT_REGISTERS_ADDRESS_SPECIAL) {
                printf("Reply to this special register address by an exception\n");
                modbus_reply_exception(ctx, query,
                                       MODBUS_EXCEPTION_SLAVE_OR_SERVER_BUSY);
                continue;
            }
        }

        rc = modbus_reply(ctx, query, rc, mb_mapping);
		
		if ( use_backend != RTU)
			close(client_socket);
    }

    printf("Quit the loop: %s\n", modbus_strerror(errno));

    if (use_backend == TCP) {
        close(socket);
    }
	
	modbus_uninit_cb(ctx, &cb);
    modbus_mapping_free(mb_mapping);
    free(query);
    modbus_free(ctx);

    return 0;
}