#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}; unsigned char ef_cmd_0[] = {0x01, 0x03, 0x10, 0x00, 0x00, 0x64, 0x00, 0x00}; unsigned char ef_cmd_1[] = {0x01, 0x03, 0x12, 0x00, 0x00, 0x3e, 0x00, 0x00}; unsigned char ef_cmd_2[] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}; DNCommThread::DNCommThread(QObject *parent, quint8 id, bool enabled, QString commpath) : QThread(parent) { Id = id; ef_idx = 0; noEcho0 = 0; noEcho1 = 0; Enabled = enabled; CommOpened = false; CommPath = commpath; CommType = 1; reopen = false; // printf("from thread ID:%d\t ENABLED:%d\tPATH:%s\n",Id,Enabled,CommPath.toUtf8().data()); } void DNCommThread::setCommType(quint16 type) { CommType = type; } 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() { // printf("open comm %d %s\n",Id,CommPath.toUtf8().data()); 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; } void DNCommThread::chk_rtn(QByteArray Rtn) { printf("Rtn length:%d\n",Rtn.length()); if(Rtn.length()>0){ QDateTime dt = QDateTime::currentDateTime(); QString str = QString("[%1] %2 recv: ").arg(dt.toString("yyyy-MM-dd HH:mm:ss")).arg(Id); for(int i=0;i=6)){ chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t); } if(((Rtn.at(cure3+1)&0xff)==0x01) &&((cure6-cure3)>=10) &&((cure6-cure3)<=18)) { 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)) { quint8 e3id = ((Rtn.at(cure3+2)&0xf0)>>4)*10+(Rtn.at(cure3+2)&0x0f); printf("e3id:%d\n",e3id); 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; } } } } } } quint16 DNCommThread::chk_crcc(quint8 *d, int len) { int j,i; quint16 default_data = 0xa001; quint16 crc = 0xffff; for(j=0;j<(len-2);j++){ crc ^= (quint16)(d[j]&0x00ff); for(i=0;i<8;i++){ if(crc&0x01){ crc >>= 1; crc ^= default_data;//a001,1021 }else crc >>= 1; } } return crc; } void DNCommThread::ef_comm_read() { int t_len,len; uint t = QDateTime::currentDateTime().toTime_t(); fd_set reads; struct timeval timeout; unsigned char tmp[1024],cmd[1024]; unsigned char rtn[1024]; FD_ZERO(&reads); FD_SET(fd,&reads); timeout.tv_sec = 1; timeout.tv_usec = 500000; if(ef_idx==0){ for(int i=0;i<8;i++) cmd[i] = ef_cmd_0[i]; noEcho0++; }else if(ef_idx==1){ for(int i=0;i<8;i++) cmd[i] = ef_cmd_1[i]; noEcho0++; }else if(ef_idx==2){ for(int i=0;i<8;i++) cmd[i] = ef_cmd_2[i]; noEcho1++; } quint16 crc = chk_crcc((quint8 *)cmd,8); cmd[6] = crc&0xff; cmd[7] = (crc>>8)&0xff; // printf("%s >>> ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); // for(int i=0;i<8;i++) // printf("%02x ",cmd[i]); // printf("\n"); len = write(fd,cmd,8); if(len==8) { if(select(fd+1,&reads,NULL,NULL,&timeout)>=0) { if(FD_ISSET(fd,&reads)) { len= read(fd,tmp,1024); // len=t_len; if((len>=5)&&(len<((int)(tmp[2]&0x00ff)+5))){ // printf("recv again\n"); usleep(50000); t_len = read(fd,rtn,1024); for(int i=0;i=5)&&(len>=((int)(tmp[2]&0x00ff)+5))){ // t_len = (int)(tmp[2]&0x00ff+5); // printf("len: %d %d\n",len,((int)(tmp[2]&0x00ff)+5)); // printf("%s <<< ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); for(int i=0;i>8)&0xff))&&(tmp[len-2]==(crc&0xff))){ // printf("crc ok\n"); if((tmp[0]==0x01)&&(tmp[2]==0xc8)){ noEcho0 = 0; if(noEcho1==0) set_realtime_value(Id,7,0,t); quint16 loop_idx = ((tmp[3]&0x00ff)<<8)|(tmp[4]&0x00ff); if((loop_idx&0x0001)==0x0000){ quint16 tmp_v = ((tmp[13]&0x00ff)<<8)|(tmp[14]&0x00ff); float v = ((qint16)tmp_v)*0.1; set_realtime_value(Id,39,v,t); tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff); if((tmp_v&0x0001)==0x0001) set_realtime_value(Id,4,1.0,t); else set_realtime_value(Id,4,0.0,t); } if((loop_idx&0x0002)==0x0002){ quint16 tmp_v = ((tmp[15]&0x00ff)<<8)|(tmp[16]&0x00ff); float v = ((qint16)tmp_v)*0.1; set_realtime_value(Id,40,v,t); // tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff); // if((tmp_v&0x0002)==0x0002) // set_realtime_value(Id,5,1.0,t); // else // set_realtime_value(Id,5,0.0,t); } if((loop_idx&0x0004)==0x0004){ quint16 tmp_v = ((tmp[17]&0x00ff)<<8)|(tmp[18]&0x00ff); float v = ((qint16)tmp_v)*0.1; set_realtime_value(Id,41,v,t); } if((loop_idx&0x0008)==0x0008){ quint16 tmp_v = ((tmp[19]&0x00ff)<<8)|(tmp[20]&0x00ff); float v = ((qint16)tmp_v)*0.1; set_realtime_value(Id,42,v,t); } if((loop_idx&0x0010)==0x0010){ quint16 tmp_v = ((tmp[21]&0x00ff)<<8)|(tmp[22]&0x00ff); float v = ((qint16)tmp_v)*0.1; set_realtime_value(Id,43,v,t); } quint16 tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff); if(((tmp_v&0x0002)==0x0002)&&(ytShm->sPointList.sPoint[Id][40].ENABLED==0x01)) set_realtime_value(Id,5,1.0,t); else if(((tmp_v&0x0004)==0x0004)&&(ytShm->sPointList.sPoint[Id][41].ENABLED==0x01)) set_realtime_value(Id,5,1.0,t); else if(((tmp_v&0x0008)==0x0008)&&(ytShm->sPointList.sPoint[Id][42].ENABLED==0x01)) set_realtime_value(Id,5,1.0,t); else if(((tmp_v&0x0010)==0x0010)&&(ytShm->sPointList.sPoint[Id][43].ENABLED==0x01)) set_realtime_value(Id,5,1.0,t); else set_realtime_value(Id,5,0.0,t); }else if((tmp[0]==0x01)&&(tmp[2]==0x7c)){ noEcho0 = 0; if(noEcho1==0) set_realtime_value(Id,7,0,t); quint16 tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff); float v = tmp_v*0.01; set_realtime_value(Id,16,v,t); tmp_v = ((tmp[11]&0x00ff)<<8)|(tmp[12]&0x00ff); v = tmp_v*0.1; set_realtime_value(Id,17,v,t); if(tmp[29]==0x01) set_realtime_value(Id,1,1.0,t); else set_realtime_value(Id,1,0.0,t); if(tmp[30]==0x01) set_realtime_value(Id,2,1.0,t); else set_realtime_value(Id,2,0.0,t); tmp_v = ((tmp[43]&0x00ff)<<8)|(tmp[44]&0x00ff); v = tmp_v*0.001*10; set_realtime_value(Id,20,v,t); if(tmp[56]==0x01) set_realtime_value(Id,3,1.0,t); else set_realtime_value(Id,3,0.0,t); tmp_v = ((tmp[63]&0x00ff)<<8)|(tmp[64]&0x00ff); v = tmp_v*0.001; set_realtime_value(Id,23,v,t); tmp_v = ((tmp[69]&0x00ff)<<8)|(tmp[70]&0x00ff); v = tmp_v*0.001; set_realtime_value(Id,26,v,t); tmp_v = ((tmp[75]&0x00ff)<<8)|(tmp[76]&0x00ff); v = tmp_v*0.001; set_realtime_value(Id,29,v,t); tmp_v = ((tmp[81]&0x00ff)<<8)|(tmp[82]&0x00ff); v = tmp_v*0.001; set_realtime_value(Id,32,v,t); tmp_v = ((tmp[93]&0x00ff)<<8)|(tmp[94]&0x00ff); v = tmp_v*0.001; set_realtime_value(Id,35,v,t); tmp_v = ((tmp[95]&0x00ff)<<8)|(tmp[96]&0x00ff); v = tmp_v*0.001; set_realtime_value(Id,36,v,t); tmp_v = ((tmp[97]&0x00ff)<<8)|(tmp[98]&0x00ff); v = tmp_v*0.001; set_realtime_value(Id,37,v,t); tmp_v = ((tmp[99]&0x00ff)<<8)|(tmp[100]&0x00ff); v = tmp_v*0.001; set_realtime_value(Id,38,v,t); }else if((tmp[0]==0x02)&&(tmp[2]==0x02)){ noEcho1 = 0; if(noEcho0==0) set_realtime_value(Id,7,0,t); quint16 tmp_v = ((tmp[3]&0x00ff)<<8)|(tmp[4]&0x00ff); if(tmp_v==0x0001) set_realtime_value(Id,6,1.0,t); else set_realtime_value(Id,6,0.0,t); } } } } } } if(noEcho0>18){ noEcho0=18; set_realtime_value(Id,7,1.0,t); } if(noEcho1>9){ noEcho1=9; set_realtime_value(Id,7,1.0,t); } ef_idx++; if(ef_idx>2) ef_idx=0; } void DNCommThread::comm_read() { int t_len,len; fd_set reads; struct timeval timeout; unsigned char tmp[1024]; unsigned char rtn[1024]; FD_ZERO(&reads); FD_SET(fd,&reads); timeout.tv_sec = 1; timeout.tv_usec = 500000; // printf("comm %d read\n",Id); if(select(fd+1,&reads,NULL,NULL,&timeout)>=0) { if(FD_ISSET(fd,&reads)) { // printf("comm %d reading\n",Id); t_len= read(fd,tmp,1024); len=t_len; // printf("comm %d length:%d\n",Id,len); if(t_len>0){ printf("%s ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data()); for(int i=0;i0){ 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(); // printf("dncomm %d last feed %d\n",Id,ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED); 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 // comm_read(&len,&rtn); if(CommType==1) comm_read(); else if(CommType==2) ef_comm_read(); } } usleep(1000000); } }