EasyManua.ls Logo

YASKAWA MP920 - Page 291

YASKAWA MP920
336 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Appendix C C Language Sample Programs
C.2.2 UDP (When Using Extended MEMOBUS Protocol)
C-16
// Creates UDP socket.
sd = socket( AF_INET, SOCK_DGRAM, 0 );
if ( sd <= 0 )
// Value 0 or less will be returned if an error occurs in processing.
{
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 ) //
-1 will be returned if an error occurs in processing.
{
closesocket( sd );
printf( "Error: bind !!\n" );
exit(0);
}
// Immediately waits for receive data since there is no other connection.
// Extended MEMOBUS data sending and receiving processing
// A response must be sent for the received command when using the extended MEMOBUS protocol.
// Repeats receiving the command data and sending the response data.
while(1)
{
// Receives the command data.
// This processing will not end if the Master does not send the command.
fromlen = sizeof(struct sockaddr);
rlen = recvfrom( sd, &rbuf[0], sizeof(rbuf), 0, (struct sockaddr *)&from, &fromlen );
if ( rlen <= 0 )/
/Value 0 or less will be returned if an error occurs in receiving data.
{
closesocket(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(sd);
exit(0);
}
// Prepares the response data.
mk_rsp_data( &send_len );
// Sends the response data.
// This processing will not end if the Slave cannot send the response data.
slen = sendto( sd, &sbuf[0], send_len, 0, (struct sockaddr *)&from, sizeof(struct sockaddr));
if ( slen != send_len )
// The number of bytes that was sent will be returned if the sending processing is
suc-
cessful.
{
closesocket(sd);
printf( "Error: Send !! -> %d\n", slen );
exit(0);
}
printf( "Hit Any Key !!\n" );
}
}

Table of Contents

Related product manuals