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

simple-mbgateway.c File Reference

#include <pthread.h>
#include <stdarg.h>
#include "mbserial.h"
#include "mbtcp.h"

Include dependency graph for simple-mbgateway.c:

Go to the source code of this file.

Data Structures

struct  Gateway

Defines

#define DEBUG
#define MASTER_PORT   PORT1
#define MAX_BUFFER_SIZE   256
#define SLAVE_PORT   PORT2
#define SLAVE_TCP_ADDRESS   "192.168.127.100"
#define TCP_PORT   502

Functions

int main ()
void * serial_master (void *ptr)
void * serial_master_child (void *ptr)
void * serial_slave (void *ptr)
void * serial_slave_child (void *ptr)


Define Documentation

#define DEBUG
 

Definition at line 11 of file simple-mbgateway.c.

#define MASTER_PORT   PORT1
 

Definition at line 23 of file simple-mbgateway.c.

Referenced by serial_master().

#define MAX_BUFFER_SIZE   256
 

Definition at line 19 of file simple-mbgateway.c.

#define SLAVE_PORT   PORT2
 

Definition at line 24 of file simple-mbgateway.c.

Referenced by serial_slave().

#define SLAVE_TCP_ADDRESS   "192.168.127.100"
 

Definition at line 20 of file simple-mbgateway.c.

Referenced by serial_master(), and serial_master_child().

#define TCP_PORT   502
 

Definition at line 21 of file simple-mbgateway.c.

Referenced by serial_master(), serial_master_child(), and serial_slave().


Function Documentation

int main  ) 
 

Definition at line 248 of file simple-mbgateway.c.

References MBSerialInit(), serial_master(), and serial_slave().

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 }

Here is the call graph for this function:

void* serial_master void *  ptr  ) 
 

Definition at line 91 of file simple-mbgateway.c.

References FindFD(), MASTER_PORT, MAX_BUFFER_SIZE, MB_RTU_PROTOCOL, MBSerialOpen(), MBTCPClientConnect(), MBTCPClientInit(), Gateway::port, serial_master_child(), SerialDataInInputQueue(), SerialSetSpeed(), SLAVE_TCP_ADDRESS, Gateway::socketfd, and TCP_PORT.

Referenced by main().

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 }

Here is the call graph for this function:

void* serial_master_child void *  ptr  ) 
 

Definition at line 34 of file simple-mbgateway.c.

References MAX_BUFFER_SIZE, MBSerialBlockRead(), MBSerialGetAddress(), MBSerialGetPDU(), MBSerialWrite(), MBTCPClientConnect(), MBTCPClientInit(), MBTCPGetMBAP(), MBTCPGetPDU(), MBTCPMakeMBAP(), MBTCPSendAndWaitResponse(), Gateway::port, SLAVE_TCP_ADDRESS, Gateway::socketfd, TCP_PORT, and _MBAP_Struct::UnitIdentifier.

Referenced by serial_master().

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 }

Here is the call graph for this function:

void* serial_slave void *  ptr  ) 
 

Definition at line 192 of file simple-mbgateway.c.

References Gateway::adu, Gateway::adu_len, MAX_BUFFER_SIZE, MB_RTU_PROTOCOL, MBSerialOpen(), MBTCPBlockRead(), MBTCPServerInit(), MBTCPServerWaitConnection(), Gateway::port, serial_slave_child(), SerialSetSpeed(), SLAVE_PORT, Gateway::socketfd, and TCP_PORT.

Referenced by main().

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 }

Here is the call graph for this function:

void* serial_slave_child void *  ptr  ) 
 

Definition at line 144 of file simple-mbgateway.c.

References Gateway::adu, Gateway::adu_len, MAX_BUFFER_SIZE, MBSerialGetAddress(), MBSerialGetPDU(), MBSerialSendAndWaitResponse(), MBTCPGetMBAP(), MBTCPGetPDU(), MBTCPMakeMBAP(), MBTCPWrite(), Gateway::port, Gateway::socketfd, _MBAP_Struct::TransactionIdentifier, and _MBAP_Struct::UnitIdentifier.

Referenced by serial_slave().

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 }

Here is the call graph for this function:


Generated on Thu Oct 6 09:14:12 2005 for Example Modbus Library by  doxygen 1.4.4