Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

# simple-mbgateway.c

Go to the documentation of this file.

    00001 /*---------------------------------------------------------------------------*/
    00002 /*
    00003  *      Demo program of modbus example library
    00004  *
    00005  *      History:
    00006  *      Date            Author                  Comment
    00007  *      08-17-2005      AceLan Kao.             Create it.
    00008  */
    00009 /*---------------------------------------------------------------------------*/
    00010
    00011 #define DEBUG
    00012
    00013 #include <pthread.h>
    00014 #include <stdarg.h>
    00015
    00016 #include "mbserial.h"
    00017 #include "mbtcp.h"
    00018
    00019 #define         MAX_BUFFER_SIZE         256
    00020 #define         SLAVE_TCP_ADDRESS       "192.168.127.100"
    00021 #define         TCP_PORT                502
    00022
    00023 #define         MASTER_PORT             PORT1
    00024 #define         SLAVE_PORT              PORT2
    00025
    00026 typedef struct
    00027 {
    00028         int socketfd;
    00029         int port;
    00030         char *adu;
    00031         int adu_len;
    00032 } Gateway;
    00033
    00034 void *serial_master_child( void *ptr)
    00035 {
    00036         Gateway *arg= (Gateway *)ptr;
    00037         int port, socketfd;
    00038
    00039         char adu[ MAX_BUFFER_SIZE], pdu[ MAX_BUFFER_SIZE];
    00040         int adu_len, pdu_len;
    00041         pMBAPStruct mbap= (pMBAPStruct)malloc(sizeof(MBAPStruct));
    00042         char address;
    00043         int times= 10;
    00044
    00045         socketfd= arg->socketfd;
    00046         port= arg->port;
    00047         free( arg);
    00048
    00049         adu_len= MBSerialBlockRead( port, adu, MAX_BUFFER_SIZE);
    00050         if( adu_len == 0)               // it should not be happened
    00051                 goto master_exit;
    00052         if( adu_len < 0)
    00053         {
    00054                 printf("some error happened : %d\n", adu_len);
    00055                 goto master_exit;
    00056         }
    00057         address= MBSerialGetAddress( adu);
    00058         pdu_len= MBSerialGetPDU( pdu, adu, adu_len);
    00059
    00060         MBTCPMakeMBAP( mbap, 0, pdu_len+ 1, address);
    00061         // TCP may timeout here...
    00062         adu_len= MBTCPSendAndWaitResponse( socketfd, adu, pdu, pdu_len, mbap);
    00063         if( adu_len < 0)
    00064         {
    00065                 printf("TCP slave is not connected, trying to connecting...\n");
    00066                 MBTCPClientInit( &socketfd);
    00067                 times= 10;
    00068                 while( times > 0)
    00069                 {
    00070                         if( MBTCPClientConnect( socketfd, SLAVE_TCP_ADDRESS, TCP_PORT) == 0)
    00071                                 break;
    00072                         times--;
    00073                         sleep( 1);
    00074                 }
    00075                 if( times == 0)
    00076                         goto master_exit;
    00077                 printf("TCP slave connected..\n");
    00078                 adu_len= MBTCPSendAndWaitResponse( socketfd, adu, pdu, pdu_len, mbap);
    00079         }
    00080
    00081         MBTCPGetMBAP( adu, mbap);
    00082         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00083
    00084         MBSerialWrite( port, pdu, pdu_len, mbap->UnitIdentifier);
    00085
    00086 master_exit:
    00087         free( mbap);
    00088         pthread_exit( NULL);
    00089 }
    00090
    00091 void *serial_master( void *ptr)
    00092 {
    00093         int socketfd, serialfd;
    00094         char adu[ MAX_BUFFER_SIZE], pdu[ MAX_BUFFER_SIZE];
    00095         int adu_len, pdu_len;
    00096         pMBAPStruct mbap= (pMBAPStruct)malloc(sizeof(MBAPStruct));
    00097         char address;
    00098         Gateway* gw;
    00099         pthread_t threads;
    00100
    00101         printf("try to open serial port%d\n", MASTER_PORT+ 1);
    00102         if( MBSerialOpen( MASTER_PORT, MB_RTU_PROTOCOL) < 0)
    00103         {
    00104                 perror("serial");
    00105                 exit( -1);
    00106         }
    00107         SerialSetSpeed( MASTER_PORT, 9600);
    00108         printf("opened..\n");
    00109
    00110         MBTCPClientInit( &socketfd);
    00111         while( 1)
    00112         {
    00113                 printf("Master: trying to connect to slave..\n");
    00114                 if( MBTCPClientConnect( socketfd, SLAVE_TCP_ADDRESS, TCP_PORT) < 0)
    00115                 {
    00116                         perror("TCP client");
    00117                         continue;
    00118                 }
    00119                 else
    00120                 {
    00121                         printf("Master: connected..\n");
    00122                         break;
    00123                 }
    00124         }
    00125
    00126         printf("Master: serial fd = %d , socket fd = %d\n", FindFD( MASTER_PORT), socketfd);
    00127         while( 1)
    00128         {
    00129                 if( SerialDataInInputQueue( MASTER_PORT) > 1)
    00130                 {
    00131                         gw= (Gateway *)malloc( sizeof( Gateway));
    00132                         gw->socketfd= socketfd;
    00133                         gw->port= MASTER_PORT;
    00134
    00135                         pthread_create( &threads, NULL, serial_master_child, (void *)gw);
    00136                 }
    00137                 usleep( 1000);
    00138         }
    00139
    00140         free( mbap);
    00141         pthread_exit( NULL);
    00142 }
    00143
    00144 void *serial_slave_child( void *ptr)
    00145 {
    00146         Gateway *arg= (Gateway *)ptr;
    00147         int port, socketfd;
    00148         char adu[ MAX_BUFFER_SIZE], pdu[ MAX_BUFFER_SIZE];
    00149         char rsp_adu[ MAX_BUFFER_SIZE], rsp_pdu[ MAX_BUFFER_SIZE];
    00150         int adu_len, pdu_len, rsp_adu_len, rsp_pdu_len;
    00151         pMBAPStruct mbap= (pMBAPStruct)malloc(sizeof(MBAPStruct));
    00152         char address;
    00153         int times= 10;
    00154
    00155         socketfd= arg->socketfd;
    00156         port= arg->port;
    00157         adu_len= arg->adu_len;
    00158         memcpy( adu, arg->adu, adu_len);
    00159         free( arg->adu);
    00160         free( arg);
    00161
    00162         MBTCPGetMBAP( adu, mbap);
    00163         pdu_len= MBTCPGetPDU( pdu, adu, adu_len);
    00164
    00165         address= mbap->UnitIdentifier;
    00166         print_s( pdu, pdu_len);
    00167         printf("\n");
    00168
    00169         rsp_adu_len= MBSerialSendAndWaitResponse( port, rsp_adu, pdu, pdu_len, address);
    00170         if( rsp_adu_len == 0)
    00171                 goto slave_exit;
    00172
    00173         printf("%d - ", rsp_adu_len);
    00174         print_s( rsp_adu, rsp_adu_len);
    00175         printf("\n");
    00176
    00177         if( MBSerialGetAddress( rsp_adu) != address)
    00178         {
    00179                 printf("response address doesn't match to the request address\n");
    00180                 goto slave_exit;
    00181         }
    00182         rsp_pdu_len= MBSerialGetPDU( rsp_pdu, rsp_adu, rsp_adu_len);
    00183
    00184         MBTCPMakeMBAP( mbap, mbap->TransactionIdentifier, rsp_pdu_len+ 1, address);
    00185         MBTCPWrite( socketfd, rsp_pdu, rsp_pdu_len, mbap);
    00186
    00187 slave_exit:
    00188         free( mbap);
    00189         pthread_exit( NULL);
    00190 }
    00191
    00192 void *serial_slave( void *ptr)
    00193 {
    00194         int serverfd, clientfd;
    00195         int adu_len;
    00196         char adu[ MAX_BUFFER_SIZE], client_addr[ MAX_BUFFER_SIZE];
    00197         pMBAPStruct mbap= (pMBAPStruct)malloc(sizeof(MBAPStruct));
    00198         char address;
    00199         Gateway* gw;
    00200         pthread_t threads;
    00201
    00202         printf("try to open serial port2\n");
    00203         if( MBSerialOpen( SLAVE_PORT, MB_RTU_PROTOCOL) < 0)
    00204         {
    00205                 perror("serial");
    00206                 exit( -1);
    00207         }
    00208         printf("opened..\n");
    00209         SerialSetSpeed( SLAVE_PORT, 9600);
    00210
    00211         if( MBTCPServerInit( TCP_PORT, &serverfd) < 0)
    00212         {
    00213                 perror("socket");
    00214                 exit( -1);
    00215         }
    00216         while( 1)
    00217         {
    00218                 printf("Slave: waiting client connect..\n");
    00219                 if( MBTCPServerWaitConnection( serverfd, &clientfd, client_addr) < 0)
    00220                 {
    00221                         perror("socket");
    00222                         exit( -1);
    00223                 }
    00224                 printf("Slave: client %s connedted..\n", client_addr);
    00225
    00226                 while( 1 )
    00227                 {
    00228                         adu_len= MBTCPBlockRead( clientfd, adu, MAX_BUFFER_SIZE);
    00229                         // disconnect
    00230                         if( adu_len == 0)
    00231                                 break;
    00232
    00233                         gw= (Gateway *)malloc( sizeof( Gateway));
    00234                         gw->socketfd= clientfd;
    00235                         gw->port= SLAVE_PORT;
    00236                         gw->adu_len= adu_len;
    00237                         gw->adu=(char*)malloc( adu_len);
    00238                         memcpy( gw->adu, adu, adu_len);
    00239
    00240                         pthread_create( &threads, NULL, serial_slave_child, (void *)gw);
    00241                 }
    00242         }
    00243
    00244         free( mbap);
    00245         pthread_exit( NULL);
    00246 }
    00247
    00248 int main()
    00249 {
    00250         pthread_t threads[ 2];
    00251         void* thread_result[ 2];
    00252
    00253         MBSerialInit();
    00254
    00255         pthread_create( &threads[ 0], NULL, serial_master, NULL);
    00256         pthread_create( &threads[ 1], NULL, serial_slave, NULL);
    00257
    00258         pthread_join( threads[ 0], &thread_result[ 0]);
    00259         pthread_join( threads[ 1], &thread_result[ 1]);
    00260         return 0;
    00261 }
    00262