2012-07-21 16:56:21 +02:00
|
|
|
/*
|
2012-07-21 18:18:16 +02:00
|
|
|
* FreeModbus Libary: NuttX Port
|
|
|
|
* Based on the FreeModbus Linux port by:
|
|
|
|
*
|
|
|
|
* Copyright (C) 2006 Christian Walter <wolti@sil.at>
|
2012-07-21 16:56:21 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
|
|
|
* File: $Id: portserial.c,v 1.3 2006/10/12 08:35:34 wolti Exp $
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------------- Standard includes --------------------------------*/
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
2012-07-21 18:18:16 +02:00
|
|
|
#include <assert.h>
|
2012-07-21 16:56:21 +02:00
|
|
|
|
|
|
|
#include "port.h"
|
|
|
|
|
|
|
|
/* ----------------------- Modbus includes ----------------------------------*/
|
2012-07-21 18:18:16 +02:00
|
|
|
#include <apps/modbus/mb.h>
|
|
|
|
#include <apps/modbus/mbport.h>
|
2012-07-21 16:56:21 +02:00
|
|
|
|
|
|
|
/* ----------------------- Defines -----------------------------------------*/
|
|
|
|
#ifdef CONFIG_MB_ASCII_ENABLED
|
|
|
|
#define BUF_SIZE 513 /* must hold a complete ASCII frame. */
|
|
|
|
#else
|
|
|
|
#define BUF_SIZE 256 /* must hold a complete RTU frame. */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* ----------------------- Static variables ---------------------------------*/
|
|
|
|
static int iSerialFd = -1;
|
2012-07-21 23:23:18 +02:00
|
|
|
static bool bRxEnabled;
|
|
|
|
static bool bTxEnabled;
|
2012-07-21 16:56:21 +02:00
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
static uint32_t ulTimeoutMs;
|
|
|
|
static uint8_t ucBuffer[BUF_SIZE];
|
2012-07-21 16:56:21 +02:00
|
|
|
static int uiRxBufferPos;
|
|
|
|
static int uiTxBufferPos;
|
|
|
|
|
|
|
|
static struct termios xOldTIO;
|
|
|
|
|
|
|
|
/* ----------------------- Function prototypes ------------------------------*/
|
2012-07-21 23:23:18 +02:00
|
|
|
static bool prvbMBPortSerialRead( uint8_t * pucBuffer, uint16_t usNBytes, uint16_t * usNBytesRead );
|
|
|
|
static bool prvbMBPortSerialWrite( uint8_t * pucBuffer, uint16_t usNBytes );
|
2012-07-21 16:56:21 +02:00
|
|
|
|
|
|
|
/* ----------------------- Begin implementation -----------------------------*/
|
|
|
|
void
|
2012-07-21 23:23:18 +02:00
|
|
|
vMBPortSerialEnable( bool bEnableRx, bool bEnableTx )
|
2012-07-21 16:56:21 +02:00
|
|
|
{
|
|
|
|
/* it is not allowed that both receiver and transmitter are enabled. */
|
2012-07-21 18:18:16 +02:00
|
|
|
ASSERT( !bEnableRx || !bEnableTx );
|
2012-07-21 16:56:21 +02:00
|
|
|
|
|
|
|
if( bEnableRx )
|
|
|
|
{
|
|
|
|
( void )tcflush( iSerialFd, TCIFLUSH );
|
|
|
|
uiRxBufferPos = 0;
|
2012-07-21 23:23:18 +02:00
|
|
|
bRxEnabled = true;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
bRxEnabled = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
if( bEnableTx )
|
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
bTxEnabled = true;
|
2012-07-21 16:56:21 +02:00
|
|
|
uiTxBufferPos = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
bTxEnabled = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
bool
|
|
|
|
xMBPortSerialInit( uint8_t ucPort, uint32_t ulBaudRate, uint8_t ucDataBits, eMBParity eParity )
|
2012-07-21 16:56:21 +02:00
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
char szDevice[16];
|
|
|
|
bool bStatus = true;
|
2012-07-21 16:56:21 +02:00
|
|
|
|
|
|
|
struct termios xNewTIO;
|
|
|
|
speed_t xNewSpeed;
|
|
|
|
|
|
|
|
snprintf( szDevice, 16, "/dev/ttyS%d", ucPort );
|
|
|
|
|
|
|
|
if( ( iSerialFd = open( szDevice, O_RDWR | O_NOCTTY ) ) < 0 )
|
|
|
|
{
|
|
|
|
vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't open serial port %s: %s\n", szDevice,
|
|
|
|
strerror( errno ) );
|
|
|
|
}
|
|
|
|
else if( tcgetattr( iSerialFd, &xOldTIO ) != 0 )
|
|
|
|
{
|
|
|
|
vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't get settings from port %s: %s\n", szDevice,
|
|
|
|
strerror( errno ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bzero( &xNewTIO, sizeof( struct termios ) );
|
|
|
|
|
|
|
|
xNewTIO.c_iflag |= IGNBRK | INPCK;
|
|
|
|
xNewTIO.c_cflag |= CREAD | CLOCAL;
|
|
|
|
switch ( eParity )
|
|
|
|
{
|
|
|
|
case MB_PAR_NONE:
|
|
|
|
break;
|
|
|
|
case MB_PAR_EVEN:
|
|
|
|
xNewTIO.c_cflag |= PARENB;
|
|
|
|
break;
|
|
|
|
case MB_PAR_ODD:
|
|
|
|
xNewTIO.c_cflag |= PARENB | PARODD;
|
|
|
|
break;
|
|
|
|
default:
|
2012-07-21 23:23:18 +02:00
|
|
|
bStatus = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
switch ( ucDataBits )
|
|
|
|
{
|
|
|
|
case 8:
|
|
|
|
xNewTIO.c_cflag |= CS8;
|
|
|
|
break;
|
|
|
|
case 7:
|
|
|
|
xNewTIO.c_cflag |= CS7;
|
|
|
|
break;
|
|
|
|
default:
|
2012-07-21 23:23:18 +02:00
|
|
|
bStatus = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
switch ( ulBaudRate )
|
|
|
|
{
|
|
|
|
case 9600:
|
|
|
|
xNewSpeed = B9600;
|
|
|
|
break;
|
|
|
|
case 19200:
|
|
|
|
xNewSpeed = B19200;
|
|
|
|
break;
|
|
|
|
case 38400:
|
|
|
|
xNewSpeed = B38400;
|
|
|
|
break;
|
|
|
|
case 57600:
|
|
|
|
xNewSpeed = B57600;
|
|
|
|
break;
|
|
|
|
case 115200:
|
|
|
|
xNewSpeed = B115200;
|
|
|
|
break;
|
|
|
|
default:
|
2012-07-21 23:23:18 +02:00
|
|
|
bStatus = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
if( bStatus )
|
|
|
|
{
|
|
|
|
if( cfsetispeed( &xNewTIO, xNewSpeed ) != 0 )
|
|
|
|
{
|
|
|
|
vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't set baud rate %ld for port %s: %s\n",
|
|
|
|
ulBaudRate, strerror( errno ) );
|
|
|
|
}
|
|
|
|
else if( cfsetospeed( &xNewTIO, xNewSpeed ) != 0 )
|
|
|
|
{
|
|
|
|
vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't set baud rate %ld for port %s: %s\n",
|
|
|
|
ulBaudRate, szDevice, strerror( errno ) );
|
|
|
|
}
|
|
|
|
else if( tcsetattr( iSerialFd, TCSANOW, &xNewTIO ) != 0 )
|
|
|
|
{
|
|
|
|
vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't set settings for port %s: %s\n",
|
|
|
|
szDevice, strerror( errno ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
vMBPortSerialEnable( false, false );
|
|
|
|
bStatus = true;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bStatus;
|
|
|
|
}
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
bool
|
|
|
|
xMBPortSerialSetTimeout( uint32_t ulNewTimeoutMs )
|
2012-07-21 16:56:21 +02:00
|
|
|
{
|
|
|
|
if( ulNewTimeoutMs > 0 )
|
|
|
|
{
|
|
|
|
ulTimeoutMs = ulNewTimeoutMs;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ulTimeoutMs = 1;
|
|
|
|
}
|
2012-07-21 23:23:18 +02:00
|
|
|
return true;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
vMBPortClose( void )
|
|
|
|
{
|
|
|
|
if( iSerialFd != -1 )
|
|
|
|
{
|
|
|
|
( void )tcsetattr( iSerialFd, TCSANOW, &xOldTIO );
|
|
|
|
( void )close( iSerialFd );
|
|
|
|
iSerialFd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
bool
|
|
|
|
prvbMBPortSerialRead( uint8_t * pucBuffer, uint16_t usNBytes, uint16_t * usNBytesRead )
|
2012-07-21 16:56:21 +02:00
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
bool bResult = true;
|
2012-07-21 16:56:21 +02:00
|
|
|
ssize_t res;
|
|
|
|
fd_set rfds;
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 50000;
|
|
|
|
FD_ZERO( &rfds );
|
|
|
|
FD_SET( iSerialFd, &rfds );
|
|
|
|
|
|
|
|
/* Wait until character received or timeout. Recover in case of an
|
|
|
|
* interrupted read system call. */
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( select( iSerialFd + 1, &rfds, NULL, NULL, &tv ) == -1 )
|
|
|
|
{
|
|
|
|
if( errno != EINTR )
|
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
bResult = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( FD_ISSET( iSerialFd, &rfds ) )
|
|
|
|
{
|
|
|
|
if( ( res = read( iSerialFd, pucBuffer, usNBytes ) ) == -1 )
|
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
bResult = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
*usNBytesRead = ( uint16_t ) res;
|
2012-07-21 16:56:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*usNBytesRead = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-07-21 23:23:18 +02:00
|
|
|
while( bResult == true );
|
2012-07-21 16:56:21 +02:00
|
|
|
return bResult;
|
|
|
|
}
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
bool
|
|
|
|
prvbMBPortSerialWrite( uint8_t * pucBuffer, uint16_t usNBytes )
|
2012-07-21 16:56:21 +02:00
|
|
|
{
|
|
|
|
ssize_t res;
|
|
|
|
size_t left = ( size_t ) usNBytes;
|
|
|
|
size_t done = 0;
|
|
|
|
|
|
|
|
while( left > 0 )
|
|
|
|
{
|
|
|
|
if( ( res = write( iSerialFd, pucBuffer + done, left ) ) == -1 )
|
|
|
|
{
|
|
|
|
if( errno != EINTR )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* call write again because of interrupted system call. */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
done += res;
|
|
|
|
left -= res;
|
|
|
|
}
|
2012-07-21 23:23:18 +02:00
|
|
|
return left == 0 ? true : false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
bool
|
2012-07-21 16:56:21 +02:00
|
|
|
xMBPortSerialPoll( )
|
|
|
|
{
|
2012-07-21 23:23:18 +02:00
|
|
|
bool bStatus = true;
|
|
|
|
uint16_t usBytesRead;
|
2012-07-21 16:56:21 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
while( bRxEnabled )
|
|
|
|
{
|
|
|
|
if( prvbMBPortSerialRead( &ucBuffer[0], BUF_SIZE, &usBytesRead ) )
|
|
|
|
{
|
|
|
|
if( usBytesRead == 0 )
|
|
|
|
{
|
|
|
|
/* timeout with no bytes. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if( usBytesRead > 0 )
|
|
|
|
{
|
|
|
|
for( i = 0; i < usBytesRead; i++ )
|
|
|
|
{
|
|
|
|
/* Call the modbus stack and let him fill the buffers. */
|
|
|
|
( void )pxMBFrameCBByteReceived( );
|
|
|
|
}
|
|
|
|
uiRxBufferPos = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vMBPortLog( MB_LOG_ERROR, "SER-POLL", "read failed on serial device: %s\n",
|
|
|
|
strerror( errno ) );
|
2012-07-21 23:23:18 +02:00
|
|
|
bStatus = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if( bTxEnabled )
|
|
|
|
{
|
|
|
|
while( bTxEnabled )
|
|
|
|
{
|
|
|
|
( void )pxMBFrameCBTransmitterEmpty( );
|
|
|
|
/* Call the modbus stack to let him fill the buffer. */
|
|
|
|
}
|
|
|
|
if( !prvbMBPortSerialWrite( &ucBuffer[0], uiTxBufferPos ) )
|
|
|
|
{
|
|
|
|
vMBPortLog( MB_LOG_ERROR, "SER-POLL", "write failed on serial device: %s\n",
|
|
|
|
strerror( errno ) );
|
2012-07-21 23:23:18 +02:00
|
|
|
bStatus = false;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return bStatus;
|
|
|
|
}
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
bool
|
|
|
|
xMBPortSerialPutByte( int8_t ucByte )
|
2012-07-21 16:56:21 +02:00
|
|
|
{
|
2012-07-21 18:18:16 +02:00
|
|
|
ASSERT( uiTxBufferPos < BUF_SIZE );
|
2012-07-21 16:56:21 +02:00
|
|
|
ucBuffer[uiTxBufferPos] = ucByte;
|
|
|
|
uiTxBufferPos++;
|
2012-07-21 23:23:18 +02:00
|
|
|
return true;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|
|
|
|
|
2012-07-21 23:23:18 +02:00
|
|
|
bool
|
|
|
|
xMBPortSerialGetByte( int8_t * pucByte )
|
2012-07-21 16:56:21 +02:00
|
|
|
{
|
2012-07-21 18:18:16 +02:00
|
|
|
ASSERT( uiRxBufferPos < BUF_SIZE );
|
2012-07-21 16:56:21 +02:00
|
|
|
*pucByte = ucBuffer[uiRxBufferPos];
|
|
|
|
uiRxBufferPos++;
|
2012-07-21 23:23:18 +02:00
|
|
|
return true;
|
2012-07-21 16:56:21 +02:00
|
|
|
}
|