#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <termios.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>
#include <linux/sockios.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/ioctl.h>
#include <linux/watchdog.h>
#include <signal.h>
#include <mysql.h>
#define FALSE -1
#define TRUE 0
#define uint16_t (unsigned short int)
#define uint32_t (unsigned int)
int wd,fd;
MYSQL mysql;
MYSQL_RES *res = NULL;
MYSQL_ROW row;
MYSQL_FIELD *fieldname= NULL;
char query_str[1024],ins_str[1024],mymac[20],myip[30];
char my485[] = "/dev/ttyO2";
int plc_no =3; //slave is number 3
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, };
/*
unsigned char req_vb[] = {0x68,0x1B,0x1B,0x68,0x2,0x0,0x6C,
0x32,0x1,0x0,0x0,0x0,0x0,0x0,
0xE,0x0,0x0,0x4,0x1,0x12,0xA,0x10,0x2,0x0,0x14,0x0,0x1,0x84,0x0,0x0,0x0,0x7b,0x16};
*/
unsigned char req_vw2[] = {0x68,0x1B,0x1B,0x68,0x2,0x0,0x6C,
0x32,0x1,0x0,0x0,0x0,0x0,0x0,
0xE,0x0,0x0,0x4,0x1,0x12,0xA,0x10,0x4,0x0,0x7,0x0,0x1,0x84,0x0,0x0,0x0,0x70,0x16};
unsigned char req_vd6[] = {0x68,0x1B,0x1B,0x68,0x2,0x0,0x6C,
0x32,0x1,0x0,0x0,0x0,0x0,0x0,
0xE,0x0,0x0,0x4,0x1,0x12,0xA,0x10,0x6,0x0,0x2,0x0,0x1,0x84,0x0,0x0,0x30,0x9d,0x16};
unsigned char req_vd10[] = {0x68,0x1B,0x1B,0x68,0x2,0x0,0x6C,
0x32,0x1,0x0,0x0,0x0,0x0,0x0,
0xE,0x0,0x0,0x4,0x1,0x12,0xA,0x10,0x6,0x0,0x2,0x0,0x1,0x84,0x0,0x00,0x50,0xbd,0x16};
unsigned char req_vd14[] = {0x68,0x1B,0x1B,0x68,0x2,0x0,0x6C,
0x32,0x1,0x0,0x0,0x0,0x0,0x0,
0xE,0x0,0x0,0x4,0x1,0x12,0xA,0x10,0x6,0x0,0x2,0x0,0x1,0x84,0x0,0x0,0x70,0xdd,0x16};
unsigned char ack_ppi[] = {0x10,2,0,0x5C,0x5E,0x16};
float vd2f(unsigned char *src)
{
float f;
unsigned int i;
i = ntohl((unsigned int)(src[1]*16777216+src[0]*65536+src[3]*256+src[2]));
// i= src[2]*16777216+src[3]*65536+src[0]*256+src[1];
memcpy(&f, &i, sizeof(float));
return f;
}
void set_speed(int fd, int speed){
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
if (speed == name_arr[i]) {
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0) {
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
}
int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}
switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB;
options.c_iflag &= ~INPCK;
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK;
break;
case 'e':
case 'E':
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK;
break;
case 'S':
case 's':
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}
unsigned char checksum(char *reqstr,int count)
{
int i=0,j,sum=0;
while(i<5){if(reqstr[i]==0x68) break;i++;}
for(j=i;j<count;j++)
{
sum+=(unsigned char)reqstr[j];
//printf("j=%d,%x ,sum %2x\n",j,reqstr[j],sum);
}
return (sum &0xff);
}
int readvb(int start,int len,unsigned char *myvb)
{
int x;
unsigned char blk[512];
unsigned char req_str[] = {0x68,0x1B,0x1B,0x68,0x2,0x0,0x6C,
0x32,0x1,0x0,0x0,0x0,0x0,0x0,
0xE,0x0,0x0,0x4,0x1,0x12,0xA,0x10,0x2,0x0,0x12,0x0,0x1,0x84,0x0,0x0,0x0,0x7b,0x16};
req_str[22]=0x02; //vb:02 VW:04 VD:06...
req_str[24]=len; //number of data
req_str[29]=((start *8)>>8) & 0xff; //offset high
req_str[30]=(start*8)&0xff; //offset low, number of bytes from 0 * 8
int checksum=0;
for(x=4;x<=30;x++) checksum+=req_str[x];
req_str[31]=checksum & 0xFF;
//for(x=0;x<33;x++) printf(" %02X",req_str[x]&0xff);printf("\n");
//printf(" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2\n");
////////// ready /////////////
int n = write(fd,req_str,33); //send req pack to PLC
// printf("VBreq:%02d\n",n);
usleep(1000*100);
int nread = read(fd, blk, 1);
if(nread<=0)
{
printf("error\n");return -1;
}
if ((unsigned char)blk[0]==0xE5) // E5 PLC confirm got req pack
{
n = write(fd,ack_ppi,6); // make suree do request command ...
// printf("REQ has RECV, %02d\n",n);
}
// usleep(1000*100); //wait more if get more data
int hasread=0;
while(hasread<27+len)
{
nread = read(fd, blk+hasread, 1);
if(nread<=0)
{
printf("error\n");break;
}
hasread+=nread;
}
// for( x=0;x<hasread;x++) myvb[x]=blk[x];
// printf("%d==",hasread);
// printf(" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 \n");
// for(ix=0;x<hasread;x++) printf("%02X ",blk[x]&0xff);printf("\n");
if ((blk[19]==04)&&(blk[20]==01)&&(blk[21]==0xff)&&(blk[22]==04))
for( x=25;x<hasread-2;x++) {myvb[x-25]=blk[x];}
usleep(1000*200); //wait more if get more data
/*
unsigned char F[4];
F[0]=(unsigned char)blk[33];
F[1]=(unsigned char)blk[34];
F[2]=(unsigned char)blk[35];
F[3]=(unsigned char)blk[36];
printf("+++++++%02x %02x %02x %02x +++++++++++%f++++++++++++++++\n",F[0],F[1],F[2],F[3],vd2f((unsigned char*)F));
*/
}
int readvw(int start,int len,unsigned char *myvw)
{
int x;
unsigned char blk[512];
////////////////////////////////////////////////////////////////////////////////////////////////
unsigned char req_str[] = {0x68,0x1B,0x1B,0x68,0x2,0x0,0x6C,
0x32,0x1,0x0,0x0,0x0,0x0,0x0,
0xE,0x0,0x0,0x4,0x1,0x12,0xA,0x10,0x2,0x0,0x12,0x0,0x1,0x84,0x0,0x0,0x0,0x7b,0x16};
req_str[22]=0x04; //vb:02 VW:04 VDW:06...
req_str[24]=len; //number of data
req_str[29]=((start *8)>>8) & 0xff; //offset high
req_str[30]=(start*8)&0xff; //offset low, number of bytes from 0 * 8
int checksum=0;
for( x=4;x<=30;x++) checksum+=req_str[x];
req_str[31]=checksum & 0xFF;
//for( x=0;x<33;x++) printf("%02X ",req_str[x]&0xff);printf("\n");
////////// ready /////////////
int n = write(fd,req_str,33); //send req pack to PLC
// printf("VWreq:%02d\n",n);
usleep(1000*100);
int nread = read(fd, blk, 1);
if(nread<=0)
{
print