Omron CS1W-ETN11 Network Card User Manual


 
100
Sending Commands From a Host Computer Section 5-4
14 /*
15 *
FINS COMMUNICATIONS SAMPLE PROGRAM
16 */
17 main(argc,argv)
18 int argc;
19 char *argv[];
20 {
21 int sockfd;
22 struct sockaddr_in ws_addr,cs_addr;
23 char fins_cmnd[MAX_MSG],fins_resp[MAX_MSG];
24 int sendlen,recvlen,addrlen;
25 char sid=0;
26 extern recv_fail();
27
28 /*
GENERATE UDP SOCKET*/
29 if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<<0)
30 err_exit(”can’t open datagram socket”);
31
32 /*
ALLOCATE IP ADDRESS AND PORT # TO SOCKET*/
33 bzero((char*)&ws_addr,sizeof(ws_addr));
34 ws_addr.sin_family=AF_INET;
35 ws_addr.sin_addr.s_addr=htonl(INADDR_ANY);
36 ws_addr.sin_port=htons(FINS_UDP_PORT);
37 if(bind(sockfd,(struct sockaddr*)&ws_addr,sizeof(ws_addr))<0)
38 err_exit(”can’t bind local address”);
39
40 /*
41 *
GENERATE MEMORY AREA READ COMMAND
42 *(READ 150 WORDS FROM D00100.)
43 /*
44 fins_cmnd[0]=0x80; /*ICF*/
45 fins_cmnd[1]=0x00; /*RSV*/
46 fins_cmnd[2]=0x02; /*GCT*/
47 fins_cmnd[3]=0x01; /*DNA*/
48 fins_cmnd[4]=0x64; /*DA1*/ /*
Ethernet Unit FINS NODE NUMBER*/
49 fins_cmnd[5]=0x00; /*DA2*/
50 fins_cmnd[6]=0x01; /*SNA*/
51 fins_cmnd[7]=0x32; /*SA1*/ /*
WS FINS NODE NUMBER*/
52 fins_cmnd[8]=0x00; /*SA2*/
53 fins_cmnd[9]=++sid; /*SID*/
54 fins_cmnd[10]=0x01; /*MRC*/
55 fins_cmnd[11]=0x01; /*SRC*/
56 fins_cmnd[12]=0x82; /*
VARIABLE TYPE: DM*/
57 fins_cmnd[13]=0x00; /*
READ STARTADDRESS: 100*/
58 fins_cmnd[14]=0x64;
59 fins_cmnd[15]=0x00;
60 fins_cmnd[16]=0x00; /*
WORDS READ: 150*/
61 fins_cmnd[17]=0x96;
62
63
64 /*
SEND FINS COMMAND*/
65 bzero((char*)&cs_addr,sizeof(cs_addr));
66 cs_addr.sin_family=AF_INET;
67 cs_addr.sin_addr.s_addr=inet_addr(SERV_IP_ADDR);
68 cs_addr.sin_port=htons(FINS_UDP_PORT);
69
70 signal(SIGALRM,recv_fail);
71
72 CMND_SEND:
73 sendlen = 18;
74 if(sendto(sockfd,fins_cmnd,sendlen,0,&cs_addr,sizeof(cs_addr))
==sendlen){
75 alarm(RESP_TIMEOUT); /*
START RESPONSE MONITOR TIMER*/
76 printf(”send length %d¥n”,sendlen);