Test

Dependencies:   mbed Modbus

Revision:
0:bc6e22bac566
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 22 09:20:58 2019 +0000
@@ -0,0 +1,146 @@
+#include "mbed.h"
+
+/* ----------------------- Modbus includes ----------------------------------*/
+#include "mb.h"
+#include "mbport.h"
+ 
+/* ----------------------- Defines ------------------------------------------*/
+#define REG_INPUT_START 1000
+#define REG_INPUT_NREGS 4
+#define SLAVE_ID 0x0A
+
+//------------------------------------
+// Hyperterminal configuration
+// 9600 bauds, 8-bit data, no parity
+//------------------------------------
+
+Serial pc_port(SERIAL_TX, SERIAL_RX);
+
+DigitalOut myled(LED1);
+/*
+int main()
+{
+    int i = 1;
+    pc_port.printf("Hello World !\n");
+    while(1) {
+        wait(1);
+        pc_port.printf("This program runs since %d seconds.\n", i++);
+        myled = !myled;
+    }
+}
+*/
+ 
+/* ----------------------- Static variables ---------------------------------*/
+static USHORT   usRegInputStart = REG_INPUT_START;
+static USHORT   usRegInputBuf[REG_INPUT_NREGS];
+ 
+/* ----------------------- Start implementation -----------------------------*/
+int
+main( void )
+{
+    eMBErrorCode    eStatus;
+ 
+    eStatus = eMBInit( MB_RTU, SLAVE_ID, 0, 9600, MB_PAR_NONE );
+ 
+    /* Enable the Modbus Protocol Stack. */
+    eStatus = eMBEnable(  );
+    
+    // Initialise some registers
+    usRegInputBuf[1] = 0x1234;
+    usRegInputBuf[2] = 0x5678;
+    usRegInputBuf[3] = 0x9abc;        
+ 
+    for( ;; )
+    {
+        ( void )eMBPoll(  );
+                
+        /* Here we simply count the number of poll cycles. */
+        usRegInputBuf[0]++;
+    }
+}
+ 
+eMBErrorCode
+eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
+{
+    eMBErrorCode    eStatus = MB_ENOERR;
+    int             iRegIndex;
+ 
+    if( ( usAddress >= REG_INPUT_START )
+        && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
+    {
+        iRegIndex = ( int )( usAddress - usRegInputStart );
+        while( usNRegs > 0 )
+        {
+            *pucRegBuffer++ =
+                ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
+            *pucRegBuffer++ =
+                ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
+            iRegIndex++;
+            usNRegs--;
+        }
+    }
+    else
+    {
+        eStatus = MB_ENOREG;
+    }
+ 
+    return eStatus;
+}
+ 
+eMBErrorCode
+eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
+{
+    eMBErrorCode    eStatus = MB_ENOERR;
+    int             iRegIndex;
+ 
+    if (eMode == MB_REG_READ)
+    {
+        if( ( usAddress >= REG_INPUT_START )
+            && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
+        {
+            iRegIndex = ( int )( usAddress - usRegInputStart );
+            while( usNRegs > 0 )
+            {
+                *pucRegBuffer++ =
+                    ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
+                *pucRegBuffer++ =
+                    ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
+                iRegIndex++;
+                usNRegs--;
+            }
+        }
+    }
+ 
+    if (eMode == MB_REG_WRITE)
+    {
+        if( ( usAddress >= REG_INPUT_START )
+            && ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
+        {
+            iRegIndex = ( int )( usAddress - usRegInputStart );
+            while( usNRegs > 0 )
+            {
+                usRegInputBuf[iRegIndex] =  ((unsigned int) *pucRegBuffer << 8) | ((unsigned int) *(pucRegBuffer+1));
+                pucRegBuffer+=2;
+                iRegIndex++;
+                usNRegs--;
+            }
+        }
+    }
+ 
+    return eStatus;
+}
+ 
+ 
+eMBErrorCode
+eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils,
+               eMBRegisterMode eMode )
+{
+    return MB_ENOREG;
+}
+ 
+eMBErrorCode
+eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
+{
+    return MB_ENOREG;
+}
+