EasyManua.ls Logo

Mitsubishi Electric FX3U-ENET-ADP - Page 141

Mitsubishi Electric FX3U-ENET-ADP
162 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: Program Examples
135
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
/ ******************************************************************** /
/ ** ** /
/ ** Sample program ** /
/ ** ** /
/ ** This program is a sample program to conduct a ** /
/ ** connection test between the Ethernet adapter and ** /
/ ** target device. ** /
/ ** This program accesses the data register (D) of ** /
/ ** the PLC
main unit installed together with the ** /
/ ** Ethernet adapter. ** /
/ ** ** /
/ ** Copyright(C) 2011 Mitsubishi Electric Corporation ** /
/ ** All Rights Reserved ** /
/ ** ** /
/ ******************************************************************** /
#include <stdio.h>
#include <winsock.h>
#define FLAG_OFF 0 // Completion flag OFF
#define FLAG_ON 1 // Completion flag ON
#define SOCK_OK 0 // Normal completion
#define SOCK_NG -1 // Abnormal completion
#define BUF_SIZE 4096 // Receive buffer size
#define ERROR_INITIAL 0 // Initial error
#define ERROR_SOCKET 1 // Socket creation error
#define ERROR_BIND 2 // Bind error
#define ERROR_CONNECT 3 // Connection error
#define ERROR_SEND 4 // Send error
#define ERROR_RECEIVE 5 // Receive error
#define ERROR_SHUTDOWN 6 // Shutdown error
#define ERROR_CLOSE 7 // Line close error
//Definitions for checking the receiving sizes
//#define RECV_ANS_1 4 // Receiving size of response message in reply to device write (1E frame)
//#define RECV_ANS_2 24 // Receiving size of response message in reply to device read (1E frame)
typedef struct sck_inf{
struct in_addr my_addr;
unsigned short my_port;
struct in_addr FX_IP_addr;
unsigned short FX_port;
};
int nErrorStatus; // Error information storage variable
int Dmykeyin; // Dummy key input
int Closeflag; // Connection completion flag
SOCKET socketno;
int main()
{
WORD wVersionRequested=MAKEWORD(1,1); // Winsock Ver 1.1 request
WSADATA wsaData;
int length; // Communication data length
unsigned char s_buf[BUF_SIZE]; // Send buffer
unsigned char r_buf[BUF_SIZE]; // Receive buffer
int rbuf_idx; // Receive data storage head index
int recv_size; // Number of receive data
struct sck_inf sc;
struct sockaddr_in hostdata; // External device side data
struct sockaddr_in FX3UENETADP; // Ethernet adapter side data
void Sockerror(int); // Error handling function
unsigned long ulCmdArg ; // Non-blocking mode setting flag

Table of Contents

Related product manuals