#include "dncommthread.h" #include #include #include #include #include #include #include #include #include "yt_unit_shm.h" #include "realvalue.h" extern YT_UNIT_SHM *ytShm; int speed_arr[] = { B115200, B57600, B38400, B19200, B9600, B4800, B2400, B1200}; int name_arr[] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200}; DNCommThread::DNCommThread(QObject *parent, quint8 id, bool enabled, QString commpath) : QThread(parent) { Id = id; Enabled = enabled; CommOpened = false; CommPath = commpath; reopen = false; } void DNCommThread::setparam(QString path, bool enabled) { Enabled = enabled; CommPath = path; if(CommOpened) { reopen = true; } } void DNCommThread::set_speed(int speed) { uint 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 DNCommThread::set_Parity(int databits, int stopbits, int parity) { struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return -1; } 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 -1; } 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 -1; } switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bits\n"); return -1; } 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 -1; } return 0; } int DNCommThread::open_comm() { fd = open(CommPath.toUtf8().data(),O_RDWR); if(fd==-1) return -1; set_speed(9600); if(set_Parity(8,1,'N')==-1){ close(fd); return -1; } return 0; } QByteArray DNCommThread::comm_read() { QByteArray Rtn; fd_set reads; struct timeval timeout; unsigned char rtn[1024]; FD_ZERO(&reads); FD_SET(fd,&reads); timeout.tv_sec = 1; timeout.tv_usec = 500000; if(select(fd+1,&reads,NULL,NULL,&timeout)>=0) { if(FD_ISSET(fd,&reads)) { int len = read(fd,rtn,1024); if(len>0){ Rtn.resize(len); for(int i=0;i=0) { if(FD_ISSET(fd,&reads)) { int len = read(fd,rtn,1024); if(len>0){ Rtn.resize(len); for(int i=0;i=4){ for(int i=0;isPointList.sPoint[Id][64+i].ENABLED==0x01) { if((d.at(i)&0xff)==0x00) set_realtime_value(Id,64+i,0,t); else set_realtime_value(Id,64+i,1,t); } } } } void DNCommThread::chk_e3(QByteArray d, uint t) { for(int i=0;isPointList.sPoint[Id][1+i].ENABLED==0x01) { if((d.at(i)&0xff)==0x00) set_realtime_value(Id,1+i,0,t); else set_realtime_value(Id,1+i,1,t); } } } void DNCommThread::run() { if(Enabled) { ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id; ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30; ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01; emit log(QString("[%1] dncomm[%2] thread start\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(Id)); } else { ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id; ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 0; ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x00; emit log(QString("[%1] dncomm[%2] thread disabled\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(Id)); } while(1) { ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id; ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30; ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01; ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED = QDateTime::currentDateTime().toTime_t(); if(!Enabled) { if(CommOpened){ close(fd); emit log(QString("[%1] dncomm[%2] thread stop\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(Id)); return; } }else{ if(!CommOpened){ if(open_comm()==-1) { emit log(QString("[%1] dncomm[%2] open failed\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(CommPath)); return; }else{ emit log(QString("[%1] dncomm[%2] opened\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(CommPath)); CommOpened = true; } }else if(reopen){ if(CommOpened){ close(fd); emit log(QString("[%1] dncomm[%2] thread stop\r\n") .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")) .arg(Id)); CommOpened = false; } reopen = false; }else{//read and write QByteArray Rtn = comm_read(); if(Rtn.length()>0){ QDateTime dt = QDateTime::currentDateTime(); uint t = dt.toTime_t(); if(((Rtn.at(0)&0xff)==0xdc) &&((Rtn.at(1)&0xff)==0xcd) &&((Rtn.at(2)&0xff)==0xaa) &&((Rtn.at(3)&0xff)==0xaa) &&((Rtn.at(4)&0xff)==0xe0) &&((Rtn.at(Rtn.length()-2)&0xff)==0xcd) &&((Rtn.at(Rtn.length()-1)&0xff)==0xdc)) { if((Rtn.at(5)&0xff)==0xc1){//定时上报 int cure2 = Rtn.indexOf((char)(0xe2),0); int cur55 = Rtn.indexOf((char)(0x55),0); int cure3 = Rtn.indexOf((char)(0xe3),0); int cure6 = Rtn.indexOf((char)(0xe6),0); if(((Rtn.at(cure2+1)&0xff)==0x01) &&((cur55-cure2)>=6)){ chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t); } if(((Rtn.at(cure3+1)&0xff)==0x01) &&((cure6-cure3)>=8) &&((cure6-cure3)<=16)) { chk_e3(Rtn.mid(cure3+2,cure6-cure3-1),t); } QByteArray e6 = Rtn.mid(cure6); int base=0; while(e6.length()>=7) { if(((e6.at(1)&0xff)>=1) &&((e6.at(1)&0xff)<=16) &&((e6.at(2)&0xff)==0x01) &&((e6.at(5)&0xff)==0x02) &&((e6.at(6)&0xff)==0x01)) { if(ytShm->sPointList.sPoint[Id][128+base].ENABLED==0x01) { float v = ((e6.at(3)&0xff)*256.0+(e6.at(4)&0xff)*1.0)/150.0; set_realtime_value(Id,128+base,v,t); } base++; cure6 = e6.indexOf((char)(0xe6),6); if(cure6>0) e6 = e6.mid(cure6); else break; }else{ break; } } }else if((Rtn.at(5)&0xff)==0xf1){//主动上报 int cure2 = Rtn.indexOf((char)(0xe2),0); int cur55 = Rtn.indexOf((char)(0x55),0); int cure3 = Rtn.indexOf((char)(0xe3),0); int cure6 = Rtn.indexOf((char)(0xe6),0); if(((Rtn.at(cure2+1)&0xff)==0x01) &&((cur55-cure2)>=6)){ chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t); } if(((Rtn.at(cure3+1)&0xff)==0x01) &&((cure6-cure3)==4)) { int e3id = ((Rtn.at(cure3+2)&0xf0)>>4)*10+(Rtn.at(cure3+2)&0x0f); if((e3id>=1)&&(e3id<=16)) { if(ytShm->sPointList.sPoint[Id][e3id].ENABLED==0x01) { if((Rtn.at(cure3+3)&0xff)==0x00) set_realtime_value(Id,e3id,0,t); else set_realtime_value(Id,e3id,1,t); } } } QByteArray e6 = Rtn.mid(cure6); int base=0; while(e6.length()>=7) { if(((e6.at(1)&0xff)>=1) &&((e6.at(1)&0xff)<=16) &&((e6.at(2)&0xff)==0x01) &&((e6.at(5)&0xff)==0x02) &&((e6.at(6)&0xff)==0x01)) { if(ytShm->sPointList.sPoint[Id][128+base].ENABLED==0x01) { float v = ((e6.at(3)&0xff)*256.0+(e6.at(4)&0xff)*1.0)/150.0; set_realtime_value(Id,128+base,v,t); } base++; cure6 = e6.indexOf((char)(0xe6),6); if(cure6>0) e6 = e6.mid(cure6); else break; }else{ break; } } } } } } } usleep(500000); } }