int main(int argc, char **argv) {
    uint8_t rx_buf[bacnet_MAX_MPDU];
    uint16_t pdu_len;
    BACNET_ADDRESS src;
    pthread_t minute_tick_id, second_tick_id;

    bacnet_Device_Set_Object_Instance_Number(BACNET_INSTANCE_NO);
    bacnet_address_init();

    /* Setup device objects */
    bacnet_Device_Init(server_objects);
    BN_UNC(WHO_IS, who_is);
    BN_CON(READ_PROPERTY, read_property);

    bacnet_BIP_Debug = true;
    bacnet_bip_set_port(htons(BACNET_PORT));
    bacnet_datalink_set(BACNET_DATALINK_TYPE);
    bacnet_datalink_init(BACNET_INTERFACE);
    atexit(bacnet_datalink_cleanup);
    memset(&src, 0, sizeof(src));

    register_with_bbmd();

    bacnet_Send_I_Am(bacnet_Handler_Transmit_Buffer);

    pthread_create(&minute_tick_id, 0, minute_tick, NULL);
    pthread_create(&second_tick_id, 0, second_tick, NULL);
    
    /* Start another thread here to retrieve your allocated registers from the
     * modbus server. This thread should have the following structure (in a
     * separate function):
     *
     * Initialise:
     *	    Connect to the modbus server
     *
     * Loop:
     *	    Read the required number of registers from the modbus server
     *	    Store the register data into a linked list 
     */

    while (1) {
	pdu_len = bacnet_datalink_receive(
		    &src, rx_buf, bacnet_MAX_MPDU, BACNET_SELECT_TIMEOUT_MS);

	if (pdu_len) {
	    /* May call any registered handler.
	     * Thread safety: May block, however we still need to guarantee
	     * atomicity with the timers, so hold the lock anyway */
	    pthread_mutex_lock(&timer_lock);
	    bacnet_npdu_handler(&src, rx_buf, pdu_len);
	    pthread_mutex_unlock(&timer_lock);
	}

	ms_tick();
    }

    return 0;
}
Exemple #2
0
int main(int argc, char **argv) 
{
	uint8_t rx_buf[bacnet_MAX_MPDU];
    	uint16_t pdu_len;
    	BACNET_ADDRESS src;
    	pthread_t minute_tick_id, second_tick_id, modbus_side_id;  // Used for creation of threads

    	bacnet_Device_Set_Object_Instance_Number(BACNET_INSTANCE_NO);
    	bacnet_address_init();

    	/* Setup device objects */
    	bacnet_Device_Init(server_objects);
    	BN_UNC(WHO_IS, who_is);
    	BN_CON(READ_PROPERTY, read_property);

   	bacnet_BIP_Debug = true;
    	bacnet_bip_set_port(htons(BACNET_PORT));
    	bacnet_datalink_set(BACNET_DATALINK_TYPE);
    	bacnet_datalink_init(BACNET_INTERFACE);
    	atexit(bacnet_datalink_cleanup);
    	memset(&src, 0, sizeof(src));

    	register_with_bbmd();

    	bacnet_Send_I_Am(bacnet_Handler_Transmit_Buffer);

    	pthread_create(&minute_tick_id, 0, minute_tick, NULL);  // Create thread for minute_tick function
    	pthread_create(&second_tick_id, 0, second_tick, NULL);  // Create thread for second_tick function
    	pthread_create(&modbus_side_id, 0, modbus_side, NULL);  // Create thread for modbus_side function 

    	while (1) 
    	{
		pdu_len = bacnet_datalink_receive(
		&src, rx_buf, bacnet_MAX_MPDU, BACNET_SELECT_TIMEOUT_MS);

		if (pdu_len) 
		{
	    		/* May call any registered handler.
	     		* Thread safety: May block, however we still need to guarantee
	     		* atomicity with the timers, so hold the lock anyway */
	    		pthread_mutex_lock(&timer_lock);
	    		bacnet_npdu_handler(&src, rx_buf, pdu_len);
	    		pthread_mutex_unlock(&timer_lock);
		}

		ms_tick();
    	}
	
	return 0;
}
int main(int argc, char **argv) {
    uint8_t rx_buf[bacnet_MAX_MPDU];
    uint16_t pdu_len;
    BACNET_ADDRESS src;
    pthread_t minute_tick_id, second_tick_id;

    bacnet_Device_Set_Object_Instance_Number(BACNET_INSTANCE_NO);
    bacnet_address_init();

    /* Setup device objects */
    bacnet_Device_Init(server_objects);
    BN_UNC(WHO_IS, who_is);
    BN_CON(READ_PROPERTY, read_property);

    bacnet_BIP_Debug = true;
    bacnet_bip_set_port(htons(BACNET_PORT));
    bacnet_datalink_set(BACNET_DATALINK_TYPE);
    bacnet_datalink_init(BACNET_INTERFACE);
    atexit(bacnet_datalink_cleanup);
    memset(&src, 0, sizeof(src));

    register_with_bbmd();

    bacnet_Send_I_Am(bacnet_Handler_Transmit_Buffer);

    pthread_create(&minute_tick_id, 0, minute_tick, NULL);
    pthread_create(&second_tick_id, 0, second_tick, NULL);
    
    /* Start another thread here to retrieve your allocated registers from the
     * modbus server. This thread should have the following structure (in a
     * separate function):
     *
     * Initialise:
     *	    Connect to the modbus server
     *
     * Loop:
     *	    Read the required number of registers from the modbus server
     *	    Store the register data into the tail of a linked list 
     */

static int my_modbus_connect (void)
{
        modbus_t *ctx;
	uint16_t tab_reg[32];
	
	//ctx = modbus_new_tcp("127.0.0.1", 502 );
	 ctx = modbus_new_tcp(SERVER_ADDR, SERVER_PORT );
	if (modbus_connect(ctx) == -1) {
    	fprintf(stderr, "Connection failed: %s\n", modbus_strerror(errno));
   	 modbus_free(ctx);
   	 return -1;
	}

          /* Read 4 registers from the address 100 */
	 modbus_read_registers(ctx, 100, 2, tab_reg);

	 printf("Got value: %08X\n", tab_reg[0]);
	usleep(100000); // reads and sleeps for 100ms
	 modbus_close(ctx);
	 modbus_free(ctx);

	return 0;

}