YASKAWA MP920 User Manual page 283

Machine controller
Table of Contents

Advertisement

Appendix C C Language Sample Programs
C.1.2 UDP (When Using Extended MEMOBUS Protocol (SFC = 09))
}
dst.sin_port = htons( DST_PORT );
// Creates UDP socket.
sd = socket( AF_INET, SOCK_DGRAM, 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);
}
// Prepares the command data after allocating the local port number.
mk_cmd_data();
// Repeats sending the command and receiving the response.
while(1)
{
// Sends the command data.
// This processing will not end if the Master cannot send data.
slen = sendto( sd, &sbuf[0], 22, 0, (struct sockaddr *)&dst, sizeof(struct sockaddr));
// 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.
fromlen = sizeof(struct sockaddr);
rlen = recvfrom( sd, &rbuf[0], sizeof(rbuf), 0, (struct sockaddr *)&from, &fromlen );
// Receives data from the remote station.
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 response data.
rc = chk_rsp_data( rlen );
if ( rc != 0 )// Errors in received data
{
closesocket(sd);
exit(0);
}
sbuf[1] ++;// Increments the serial number of 218 header.
printf( "Hit Any Key !!\n" );
}
C-8

Hide quick links:

Advertisement

Table of Contents
loading

Table of Contents