//------------------------------------------------------------------------------
// send a data request packet to from master to slave
//------------------------------------------------------------------------------
static gboolean
send_packet_timeout(gpointer data)
{
    static gchar *func_name = "send_packet_timer";
    u_char buff[256];
    int len, ret;

    mbus_frame *frame;
    
    frame = mbus_frame_new(MBUS_FRAME_TYPE_SHORT);
    
    frame->control  = MBUS_CONTROL_MASK_SND_NKE | MBUS_CONTROL_MASK_DIR_M2S; // data request from master to slave
    frame->address  = addr; // address

    //if ((ret = mbus_frame_print(frame)) < 0)
    //{
    //    printf("%s: mbus_frame_print -> %d\n", __PRETTY_FUNCTION__, ret);
    //    return -1;
    //}
   
    if ((len = mbus_frame_pack(frame, buff, sizeof(buff))) == -1)
    {
        printf("%s: mbus_frame_pack -> %d\n", __PRETTY_FUNCTION__, ret);
        mbus_frame_free(frame);
        return -1;
    }    

    if (fs_net_send(gio_server, buff, len) < len)
    { 
        g_message("%s: failed to write data to server", func_name);
    }    
	
    mbus_frame_free(frame);
    return FALSE; 
}
Esempio n. 2
0
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_tcp_send_frame(mbus_tcp_handle *handle, mbus_frame *frame)
{
    u_char buff[PACKET_BUFF_SIZE];
    int len, ret;

    if (handle == NULL || frame == NULL)
    {
        return -1;
    }

    if ((len = mbus_frame_pack(frame, buff, sizeof(buff))) == -1)
    {
        char error_str[128];
        snprintf(error_str, sizeof(error_str), "%s: mbus_frame_pack failed\n", __PRETTY_FUNCTION__);
        mbus_error_str_set(error_str);
        return -1;
    }

    if ((ret = write(handle->sock, buff, len)) != len)
    {   
        char error_str[128];
        snprintf(error_str, sizeof(error_str), "%s: Failed to write frame to socket (ret = %d)\n", __PRETTY_FUNCTION__, ret);
        mbus_error_str_set(error_str);
        return -1;
    }

    return 0;
}
Esempio n. 3
0
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_send_frame(mbus_handle *handle, mbus_frame *frame)
{
    unsigned char buff[PACKET_BUFF_SIZE];
    int len, ret;

    if (handle == NULL || frame == NULL)
    {
        return -1;
    }
    
    // Make sure serial connection is open
    if (isatty(handle->fd) == 0)
    {
        return -1;
    }

    if ((len = mbus_frame_pack(frame, buff, sizeof(buff))) == -1)
    {
        fprintf(stderr, "%s: mbus_frame_pack failed\n", __PRETTY_FUNCTION__);
        return -1;
    }
    
#ifdef MBUS_SERIAL_DEBUG
    // if debug, dump in HEX form to stdout what we write to the serial port
    printf("%s: Dumping M-Bus frame [%d bytes]: ", __PRETTY_FUNCTION__, len);
    int i;
    for (i = 0; i < len; i++)
    {
       printf("%.2X ", buff[i]);
    }
    printf("\n");
#endif

    if ((ret = write(handle->fd, buff, len)) == len)
    {
        //
        // call the send event function, if the callback function is registered
        // 
        if (_mbus_send_event)
                _mbus_send_event(MBUS_HANDLE_TYPE_SERIAL, buff, len);
    }
    else
    {   
        fprintf(stderr, "%s: Failed to write frame to socket (ret = %d: %s)\n", __PRETTY_FUNCTION__, ret, strerror(errno));
        return -1;
    }
    
    //
    // wait until complete frame has been transmitted
    //
    tcdrain(handle->fd);

    return 0;
}
Esempio n. 4
0
//------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------
int
mbus_serial_send_frame(SoftwareSerial *handle, mbus_frame *frame)
{
    uint8_t buff[PACKET_BUFF_SIZE];
    int len, ret;

    IF_SERIAL_DEBUG(printf_P(PSTR("%s: Entered \n"), "mbus_serial_send_frame"));

    if (handle == NULL || frame == NULL)
    {
        return -1;
    }

    if ((len = mbus_frame_pack(frame, buff, sizeof(buff))) == -1)
    {
    	IF_SERIAL_DEBUG(printf_P(PSTR("%s: mbus_frame_pack failed\n"), "mbus_serial_send_frame"));
        return -1;
    }

//#ifdef MBUS_SERIAL_DEBUG
    // if debug, dump in HEX form to stdout what we write to the serial port
    printf_P(PSTR("%s: Dumping M-Bus frame [%d bytes]: "), __PRETTY_FUNCTION__, len);
    for (int i = 0; i < len; i++)
    {
       printf_P(PSTR("%.2X "), buff[i]);
    }
    printf_P(PSTR("\n"));

//#endif

    if ((ret = handle->write(buff, len)) == len)
    {
        //
        // call the send event function, if the callback function is registered
        // 
        if (_mbus_send_event)
                _mbus_send_event(MBUS_HANDLE_TYPE_SERIAL, buff, len);
    }
    else
    {   
    	IF_SERIAL_DEBUG(printf_P(PSTR("%s: Failed to write frame to socket (ret = %d: )\n"), "mbus_serial_send_frame", ret));
        return -1;
    }
    
    //
    // wait until complete frame has been transmitted
    //
    handle->flush();

    return 0;
}