Appendix C: Program Examples
136
FX
3U
-ENET-ADP User's Manual
Appendix C-1 Program example for communication using MC protocol -1
sc.my_addr.s_addr=htonl(INADDR_ANY); // External device side IP address
sc.my_port=htons(0);
// External device side port number
sc.FX_IP_addr.s_addr=inet_addr("172.16.56.99");
// Ethernet adapter side IP address
// (AC103863h)
sc.FX_port=htons(10000);
// Ethernet adapter side port number
Closeflag=FLAG_OFF;
// Connection completion flag off
nErrorStatus=WSAStartup(wVersionRequested,&wsaData);
// Winsock Initial processing
if (nErrorStatus!=SOCK_OK) {
Sockerror(ERROR_INITIAL);
// Error handling
return (SOCK_NG);
}
printf ("Winsock Version is %ld.%ld\n",HIBYTE(wsaData.wVersion),LOBYTE(wsaData.wVersion));
printf ("FX3U-ENET-ADP Test Start\n");
socketno=socket(AF_INET,SOCK_STREAM,0);
// Create socket for TCP/IP
if (socketno==INVALID_SOCKET){
Sockerror (ERROR_SOCKET);
// Error handling
return(SOCK_NG);
}
hostdata.sin_family=AF_INET;
hostdata.sin_port=sc.my_port;
hostdata.sin_addr.s_addr=sc.my_addr.s_addr;
if(bind(socketno,(LPSOCKADDR)&hostdata,sizeof(hostdata))!=SOCK_OK){
// Bind
Sockerror(ERROR_BIND);
// Error handling
return(SOCK_NG);
}
FX3UENETADP.sin_family=AF_INET;
FX3UENETADP.sin_port=sc.FX_port;
FX3UENETADP.sin_addr.s_addr=sc.FX_IP_addr.s_addr;
if(connect(socketno,(LPSOCKADDR)&FX3UENETADP,sizeof(FX3UENETADP))!=SOCK_OK){
// Connection (Active open)
Sockerror(ERROR_CONNECT);
// Error handling
return(SOCK_NG);
}
Closeflag=FLAG_ON;
// Connection completion flag ON
// Go to non-blocking mode
ulCmdArg = 1;
ioctlsocket(socketno, FIONBIO, &ulCmdArg);
// Set to non-blocking mode
strcpy(s_buf, "03FF000A4420000000000500112233445566778899AA");
// D0 to D4 batch write 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);
}
Содержание FX3U-ENET-ADP
Страница 1: ...FX3U ENET ADP USER S MANUAL ...
Страница 2: ......
Страница 6: ... 4 Safety Precautions Read these precautions before use MEMO ...
Страница 160: ...154 FX3U ENET ADP User s Manual Revised History Revised History Date Revision Description 2 2012 A First Edition ...
Страница 161: ......