YASKAWA MP920 User Manual page 285

Machine controller
Table of Contents

Advertisement

Appendix C C Language Sample Programs
C.2.1 TCP (When Using Extended MEMOBUS Protocol)
{
}
// Execute a bind to allocate local port number.
rc = bind( sd, ( struct sockaddr *)&my, sizeof(struct sockaddr_in));
if ( rc == -1 ) //
{
}
// Preparation to wait for connection: Up to 5 connection requests can be held.
// (The number of connection requests is limited depending on the OS.)
rc = listen( sd, 5 );
if( rc == -1 ) //
{
}
client_addrlen = (int)(sizeof(struct sockaddr_in));
// Wait for connection
// This processing will not end if no connection request is sent from the Master.
new_sd = accept( sd, ( struct sockaddr * )&dst, &client_addrlen );
if( new_sd == -1 ) //
{
}
// sd is the socket to wait for connection. When connecting processing is executed, another socket (new_sd)
// will be created. The new_sd will be the socket number to be used for communication.
// The socket to wait for connection is closed here so that any more connection request will not be accepted.
closesocket( sd );
// Extended MEMOBUS data sending and receiving processing
// A response must be sent for the received command when using the extended MEMOBUS protocol.
// Repeats sending the command data and receiving the response data.
while(1)
{
printf( "Error: Socket !!\n" );
exit(0);
-
1 will be returned if an error occurs in processing.
closesocket( sd );
printf( "Error: bind !!\n" );
exit(0);
-
1 will be returned if an error occurs in processing.
closesocket( sd );
printf( "Error: listen !!\n" );
exit(0);
-
1 will be returned if an error occurs in processing.
closesocket( sd );
printf( "Error: accept !!\n" );
exit(0);
// Receives the command data.
// This processing will not end if the Master does not send the command data.
rlen = recv( new_sd, &rbuf[0], sizeof(rbuf), 0 ); // Receives data from the remote station.
if ( rlen <= 0 )// Value 0 or less will be returned if an error occurs in receiving data.
{
closesocket(new_sd);
printf( "Error: Recv !! -> %d\n", rlen );
exit(0);
}
// Checks the receive data.
rc = chk_cmd_data( rlen );
if ( rc != 0 )// Errors in the receive data
{
closesocket(new_sd);
C-10

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents