Esempio n. 1
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. 2
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;
}