YASKAWA MP920 User Manual page 278

Machine controller
Table of Contents

Advertisement

// Clears the sockaddr structure (IP address, port number, etc.) to zero.
memset( (char *)&my, 0, sizeof(struct sockaddr));
memset( (char *)&dst, 0, sizeof(struct sockaddr));
// Declaration of local IP address and port number
my.sin_family = AF_INET;
my.sin_addr.s_addr = htonl( MY_IP );
my.sin_port = htons( MY_PORT );
// Declaration of remote IP address and port number
dst.sin_family = AF_INET;
dst.sin_addr.s_addr = htonl( DST_IP );
dst.sin_port = htons( DST_PORT );
// Creates TCP socket.
sd = socket( AF_INET, SOCK_STREAM, 0 );
if ( sd <= 0 )
{
printf( "Error: Socket !!\n" );
exit(0);
}
// Execute a bind to allocate local port number.
rc = bind( sd, ( struct sockaddr *)&my, sizeof(struct sockaddr_in));
if ( rc == -1 )
{
closesocket( sd );
printf( "Error: bind !!\n" );
exit(0);
}
// Establishes the connection.
rc = connect( sd, ( struct sockaddr *)&dst, sizeof(struct sockaddr_in));
if ( rc == -1 )
{
closesocket( sd );
printf( "Error: Connect !!\n" );
exit(0);
}
// Prepares command data after the connection is established.
mk_cmd_data();
// Repeats sending command and receiving response.
while(1)
{
// Sends the command data.
// This processing will not end if the Master cannot send the data.
slen = send( sd, &sbuf[0], 22, 0 );// Sends the command (22 bytes).
if ( slen != 22 )// The number of bytes (22) that was sent will be returned if sending process is successful.
{
closesocket(sd);
printf( "Error: Send !! -> %d\n", slen );
exit(0);
}
// Receives the response data.
// This processing will not end if the Slave does not send the response data.
rlen = recv( sd, &rbuf[0], sizeof(rbuf), 0 ); // Receives the response data from the remote station.
if ( rlen <= 0 )//Value 0 or less will be returned if an error occurs in receiving data.
{
C.1 Sample Programs for Master Station
C-3
C

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents