porttimer.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * FreeModbus Libary: Linux Port
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  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: porttimer.c,v 1.1 2006/08/01 20:58:50 wolti Exp $
  20. */
  21. /* ----------------------- Standard includes --------------------------------*/
  22. #include <stdlib.h>
  23. #include <sys/time.h>
  24. #include "port.h"
  25. /* ----------------------- Modbus includes ----------------------------------*/
  26. #include "mb.h"
  27. #include "mbport.h"
  28. #include "mbctx.h"
  29. /* ----------------------- Defines ------------------------------------------*/
  30. /* ----------------------- Static variables ---------------------------------*/
  31. ULONG ulTimeOut;
  32. BOOL bTimeoutEnable;
  33. static struct timeval xTimeLast;
  34. /* ----------------------- Start implementation -----------------------------*/
  35. BOOL
  36. xMBPortTimersInit(fmodbus_t* ctx, USHORT usTim1Timerout50us )
  37. {
  38. ctx->ulTimeOut = usTim1Timerout50us / 20U;
  39. if( ctx->ulTimeOut == 0 )
  40. ctx->ulTimeOut = 1;
  41. return xMBPortSerialSetTimeout(ctx, ctx->ulTimeOut );
  42. }
  43. void
  44. xMBPortTimersClose( fmodbus_t* ctx )
  45. {
  46. /* Does not use any hardware resources. */
  47. }
  48. void
  49. vMBPortTimerPoll( fmodbus_t* ctx )
  50. {
  51. ULONG ulDeltaMS;
  52. struct timeval xTimeCur;
  53. //printf("vMBPortTimerPoll enter\n");//dbg wanggao
  54. /* Timers are called from the serial layer because we have no high
  55. * res timer in Win32. */
  56. if( ctx->bTimeoutEnable )
  57. {
  58. if( gettimeofday( &xTimeCur, NULL ) != 0 )
  59. {
  60. /* gettimeofday failed - retry next time. */
  61. }
  62. else
  63. {
  64. ulDeltaMS = ( xTimeCur.tv_sec - ctx->xTimeLast.tv_sec ) * 1000L +
  65. ( xTimeCur.tv_usec - ctx->xTimeLast.tv_usec ) * 1000L;
  66. if( ulDeltaMS > ctx->ulTimeOut )
  67. {
  68. ctx->bTimeoutEnable = FALSE;
  69. //printf("expired \n");//dbg wanggao
  70. ( void )ctx->pxMBPortCBTimerExpired( ctx );
  71. }
  72. }
  73. }
  74. }
  75. void
  76. vMBPortTimersEnable( fmodbus_t* ctx )
  77. {
  78. int res = gettimeofday( &ctx->xTimeLast, NULL );
  79. assert( res == 0 );
  80. ctx->bTimeoutEnable = TRUE;
  81. }
  82. void
  83. vMBPortTimersDisable( fmodbus_t* ctx )
  84. {
  85. ctx->bTimeoutEnable = FALSE;
  86. }