demo.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * FreeModbus Libary: BARE Demo Application
  3. * Copyright (C) 2006 Christian Walter <wolti@sil.at>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program 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
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. *
  19. * File: $Id$
  20. */
  21. /* ----------------------- Modbus includes ----------------------------------*/
  22. #include "mb.h"
  23. #include "mbport.h"
  24. /* ----------------------- Defines ------------------------------------------*/
  25. #define REG_INPUT_START 1000
  26. #define REG_INPUT_NREGS 4
  27. /* ----------------------- Static variables ---------------------------------*/
  28. static USHORT usRegInputStart = REG_INPUT_START;
  29. static USHORT usRegInputBuf[REG_INPUT_NREGS];
  30. /* ----------------------- Start implementation -----------------------------*/
  31. int
  32. main( void )
  33. {
  34. eMBErrorCode eStatus;
  35. eStatus = eMBInit( MB_RTU, 0x0A, 0, 38400, MB_PAR_EVEN );
  36. /* Enable the Modbus Protocol Stack. */
  37. eStatus = eMBEnable( );
  38. for( ;; )
  39. {
  40. ( void )eMBPoll( );
  41. /* Here we simply count the number of poll cycles. */
  42. usRegInputBuf[0]++;
  43. }
  44. }
  45. eMBErrorCode
  46. eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
  47. {
  48. eMBErrorCode eStatus = MB_ENOERR;
  49. int iRegIndex;
  50. if( ( usAddress >= REG_INPUT_START )
  51. && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
  52. {
  53. iRegIndex = ( int )( usAddress - usRegInputStart );
  54. while( usNRegs > 0 )
  55. {
  56. *pucRegBuffer++ =
  57. ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
  58. *pucRegBuffer++ =
  59. ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
  60. iRegIndex++;
  61. usNRegs--;
  62. }
  63. }
  64. else
  65. {
  66. eStatus = MB_ENOREG;
  67. }
  68. return eStatus;
  69. }
  70. eMBErrorCode
  71. eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs,
  72. eMBRegisterMode eMode )
  73. {
  74. return MB_ENOREG;
  75. }
  76. eMBErrorCode
  77. eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
  78. eMBRegisterMode eMode )
  79. {
  80. return MB_ENOREG;
  81. }
  82. eMBErrorCode
  83. eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
  84. {
  85. return MB_ENOREG;
  86. }