Appendix C: Program Examples
137
FX3U-ENET-ADP User's Manual
B
Version
Information
C
Program
Examples
D
Differences with
FX
3U
-ENET
E
Document,
others
Appendix C-1 Program example for communication using MC protocol -1
if(length == SOCKET_ERROR) {
nErrorStatus = WSAGetLastError();
if(nErrorStatus != WSAEWOULDBLOCK) {
Sockerror(ERROR_RECIEVE); // Error handling
return (SOCK_NG);
} else {
continue; // Repeat until messages are received
}
} else {
rbuf_idx += length; // Update the receive data storage position
recv_size += length; // Update the number of receive data
if(recv_size >= RECV_ANS_1) // Have all response messages been received?
break; // Stop repeating as messages have been received
}
}
r_buf[rbuf_idx] = '\0' ; // Set NULL at the end of receive data
printf("\n receive data\n%s\n",r_buf);
strcpy(s_buf, "01FF000A4420000000000500"); // D0 to D4 batch read request (1E frame)
length=(int)strlen(s_buf);
if(send(socketno,s_buf,length,0)==SOCKET_ERROR){ // Data sending
Sockerror(ERROR_SEND); // Error handling
return (SOCK_NG);
}
printf("\n send data\n%s\n",s_buf);
// Perform receiving size check and receiving processing simultaneously
rbuf_idx = 0; // Receive data storage head index initialization
recv_size = 0; // Initialize the number of receive data
while(1) {
length = recv(socketno, &r_buf[rbuf_idx], (BUF_SIZE - rbuf_idx), 0);
// Response data receiving
if(length == 0) { // Is connection cut off?
Sockerror(ERROR_RECIEVE); // Error handling
return (SOCK_NG);
}
if(length == SOCKET_ERROR) {
nErrorStatus = WSAGetLastError();
if(nErrorStatus != WSAEWOULDBLOCK) {
Sockerror(ERROR_RECIEVE); // Error handling
return (SOCK_NG);
} else {
continue; // Repeat until messages are received
}
} else {
rbuf_idx += length; // Update the receive data storage position
recv_size += length; // Update the number of receive data
if(recv_size >= RECV_ANS_2) // Have all response messages been received?
break; // Stop repeating as messages have been received
}
}
r_buf[rbuf_idx] = '\0' ; // Set NULL at the end of receive data
printf("\receive data\n%s\n", r_buf);