
(N) Operation via communication and its settings
126
Instructions for the program
• When data from the computer has any error, the converter unit does not accept that data. Hence, in the user program,
always insert a retry program for data error.
• All data communication, for example, monitoring, are started when the computer gives a communication request. The
converter unit does not return any data without the computer's request. Hence, design the program so that the computer
gives a data read request for monitoring, etc. as required.
• Program example) Writing "0" in
Pr.57
Microsoft
®
Visual C++
®
(Ver. 6.0) programming example
#include <stdio.h>
#include <windows.h>
void main(void){
HANDLE
hCom;
// Communication handle
DCB
hDcb;
// Structure for setting communication settings
COMMTIMEOUTS
hTim;
// Structure for setting timeouts
char
szTx[0x10];
// Send buffer
char
szRx[0x10];
// Receive buffer
char
szCommand[0x10];// Command
int
nTx,nRx;
// For storing buffer size
int
nSum;
// For calculating sum code
BOOL
bRet;
int
nRet;
int
i;
//
Open COM1 port
hCom = CreateFile("COM1", (GENERIC_READ | GENERIC_WRITE), 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hCom != NULL) {
//
Set COM1 port communication
GetCommState(hCom,&hDcb);
// Get current communication information
hDcb.DCBlength = sizeof(DCB);
// Structure size setting
hDcb.BaudRate = 19200;
// Communication speed = 19200 bps
hDcb.ByteSize = 8;
// Data length = 8 bits
hDcb.Parity = 2;
// Even parity
hDcb.StopBits = 2;
// Stop bit = 2 bits
bRet = SetCommState(hCom,&hDcb);
// Setting of changed communication information
if(bRet == TRUE) {
//
Set COM1 port timeout
GetCommTimeouts(hCom,&hTim);
// Get current timeout values
hTim.WriteTotalTimeoutConstant = 1000;
// Write timeout 1 second
hTim.ReadTotalTimeoutConstant = 1000;
// Read timeout 1 second
SetCommTimeouts(hCom,&hTim);
// Setting of changed timeout values
//
Setting a command to write "0" in
Pr.57
of the station number 1 converter unit
sprintf(szCommand,"01B910000");
// Send data (Parameter write)
nTx = strlen(szCommand);
// Send data size
//
Generate sum code
nSum = 0;
// Initialize sum data
for(i = 0;i < nTx;i++) {
nSum += szCommand[i];
// Calculate sum code
nSum &= (0xff);
// Mask data
}
//
Generate send data
memset(szTx,0,sizeof(szTx));
// Initialize send buffer
memset(szRx,0,sizeof(szRx));
// Initialize receive buffer
sprintf(szTx,"\5%s%02X",szCommand,nSum);// ENQ code + send data + sum code
nTx = 1 + nTx + 2;
// Number of ENQ codes + number of send data + number of sum codes
nRet = WriteFile(hCom,szTx,nTx,&nTx,NULL);
//
Send
if(nRet != 0) {
nRet = ReadFile(hCom,szRx,sizeof(szRx),&nRx,NULL);
//
Receive
if(nRet != 0) {
//
Display receive data
for(i = 0;i < nRx;i++) {
printf("%02X ",(BYTE)szRx[i]);// Output received data to console
// Display ASCII code in Hexadecimal' In case of 0', "30" is displayed.
}
printf("\n\r");
}
}
}
CloseHandle(hCom);
// Close communication port
}
}
Summary of Contents for FR-CC2-P
Page 59: ...MEMO 58 ...
Page 73: ...MEMO 72 ...
Page 137: ...MEMO 136 ...
Page 153: ...MEMO 152 ...
Page 165: ...MEMO 164 ...
Page 169: ...MEMO 168 ...