portserial_m.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * FreeModbus Libary: RT-Thread Port
  3. * Copyright (C) 2013 Armink <armink.ztl@gmail.com>
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id: portserial_m.c,v 1.60 2013/08/13 15:07:05 Armink add Master Functions $
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <sys/select.h>
  28. #include <fcntl.h>
  29. #include <termios.h>
  30. #include <unistd.h>
  31. #include "port.h"
  32. /* ----------------------- Modbus includes ----------------------------------*/
  33. #include "mb.h"
  34. #include "mbport.h"
  35. #include "mbconfig.h"
  36. #include "mbctx.h"
  37. #if MB_MASTER_RTU_ENABLED > 0 || MB_MASTER_ASCII_ENABLED > 0
  38. /* ----------------------- Static variables ---------------------------------*/
  39. /* ----------------------- static functions ---------------------------------*/
  40. void prvvMBMasterUARTTxReadyISR(void);
  41. void prvvMBMasterUARTRxISR(void);
  42. /* ----------------------- Start implementation -----------------------------*/
  43. BOOL xMBMasterPortSerialInit(fmodbus_t* ctx)
  44. {
  45. CHAR szDevice[32];
  46. BOOL bStatus = TRUE;
  47. struct termios xNewTIO;
  48. speed_t xNewSpeed;
  49. snprintf( szDevice, 32, "%s", ctx->szPort );
  50. if( ( ctx->iSerialFd = open( szDevice, O_RDWR | O_NOCTTY ) ) < 0 ){
  51. vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't open serial port %s: %s\n", szDevice, strerror( errno ) );
  52. }else if( tcgetattr( ctx->iSerialFd, &(ctx->xOldTIO) ) != 0 ){
  53. vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't get settings from port %s: %s\n", szDevice, strerror( errno ) );
  54. }else{
  55. vMBPortLog( MB_LOG_INFO, "SER-INIT", "init ok %s\n", szDevice );
  56. bzero( &xNewTIO, sizeof( struct termios ) );
  57. xNewTIO.c_iflag |= IGNBRK | INPCK;
  58. xNewTIO.c_cflag |= CREAD | CLOCAL;
  59. switch ( ctx->eParity )
  60. {
  61. case MB_PAR_NONE:
  62. break;
  63. case MB_PAR_EVEN:
  64. xNewTIO.c_cflag |= PARENB;
  65. break;
  66. case MB_PAR_ODD:
  67. xNewTIO.c_cflag |= PARENB | PARODD;
  68. break;
  69. default:
  70. bStatus = FALSE;
  71. }
  72. switch ( ctx->ucDataBits )
  73. {
  74. case 8:
  75. xNewTIO.c_cflag |= CS8;
  76. break;
  77. case 7:
  78. xNewTIO.c_cflag |= CS7;
  79. break;
  80. default:
  81. bStatus = FALSE;
  82. }
  83. switch ( ctx->ulBaudRate )
  84. {
  85. case 4800:
  86. xNewSpeed = B4800;
  87. break;
  88. case 9600:
  89. xNewSpeed = B9600;
  90. break;
  91. case 19200:
  92. xNewSpeed = B19200;
  93. break;
  94. case 38400:
  95. xNewSpeed = B38400;
  96. break;
  97. case 57600:
  98. xNewSpeed = B57600;
  99. break;
  100. case 115200:
  101. xNewSpeed = B115200;
  102. break;
  103. default:
  104. bStatus = FALSE;
  105. }
  106. if( bStatus )
  107. {
  108. if( cfsetispeed( &xNewTIO, xNewSpeed ) != 0 )
  109. {
  110. vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't set baud rate %ld for port %s: %s\n",
  111. ctx->ulBaudRate, strerror( errno ) );
  112. }
  113. else if( cfsetospeed( &xNewTIO, xNewSpeed ) != 0 )
  114. {
  115. vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't set baud rate %ld for port %s: %s\n",
  116. ctx->ulBaudRate, szDevice, strerror( errno ) );
  117. }
  118. else if( tcsetattr( ctx->iSerialFd, TCSANOW, &xNewTIO ) != 0 )
  119. {
  120. vMBPortLog( MB_LOG_ERROR, "SER-INIT", "Can't set settings for port %s: %s\n",
  121. szDevice, strerror( errno ) );
  122. }
  123. else
  124. {
  125. vMBPortSerialEnable(ctx, FALSE, FALSE );
  126. bStatus = TRUE;
  127. }
  128. }
  129. }
  130. return bStatus;
  131. }
  132. void vMBMasterPortSerialEnable(fmodbus_t* ctx, BOOL bEnableRx, BOOL bEnableTx )
  133. {
  134. /* it is not allowed that both receiver and transmitter are enabled. */
  135. assert( !bEnableRx || !bEnableTx );
  136. if( bEnableRx )
  137. {
  138. ( void )tcflush( ctx->iSerialFd, TCIFLUSH );
  139. ctx->uiRxBufferPos = 0;
  140. ctx->bRxEnabled = TRUE;
  141. }
  142. else
  143. {
  144. ctx->bRxEnabled = FALSE;
  145. }
  146. if( bEnableTx )
  147. {
  148. ctx->bTxEnabled = TRUE;
  149. ctx->uiTxBufferPos = 0;
  150. }
  151. else
  152. {
  153. ctx->bTxEnabled = FALSE;
  154. }
  155. }
  156. void vMBMasterPortClose( fmodbus_t* ctx )
  157. {
  158. if( ctx->iSerialFd != -1 )
  159. {
  160. ( void )tcsetattr( ctx->iSerialFd, TCSANOW, &ctx->xOldTIO );
  161. ( void )close( ctx->iSerialFd );
  162. ctx->iSerialFd = -1;
  163. }
  164. }
  165. BOOL xMBMasterPortSerialPutByte(fmodbus_t* ctx, CHAR ucByte )
  166. {
  167. assert( ctx->uiTxBufferPos < BUF_SIZE );
  168. ctx->ucBuffer[ctx->uiTxBufferPos] = ucByte;
  169. ctx->uiTxBufferPos++;
  170. return TRUE;
  171. }
  172. BOOL xMBMasterPortSerialGetByte(fmodbus_t* ctx, CHAR * pucByte )
  173. {
  174. assert( ctx->uiRxBufferPos < BUF_SIZE );
  175. *pucByte = ctx->ucBuffer[ctx->uiRxBufferPos];
  176. ctx->uiRxBufferPos++;
  177. return TRUE;
  178. }
  179. /*
  180. * Create an interrupt handler for the transmit buffer empty interrupt
  181. * (or an equivalent) for your target processor. This function should then
  182. * call pxMBFrameCBTransmitterEmpty( ) which tells the protocol stack that
  183. * a new character can be sent. The protocol stack will then call
  184. * xMBPortSerialPutByte( ) to send the character.
  185. */
  186. void prvvMBMasterUARTTxReadyISR(void)
  187. {
  188. pxMBMasterFrameCBTransmitterEmpty();
  189. }
  190. /*
  191. * Create an interrupt handler for the receive interrupt for your target
  192. * processor. This function should then call pxMBFrameCBByteReceived( ). The
  193. * protocol stack will then call xMBPortSerialGetByte( ) to retrieve the
  194. * character.
  195. */
  196. void prvvMBMasterUARTRxISR(void)
  197. {
  198. pxMBMasterFrameCBByteReceived();
  199. }
  200. #endif