123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565 |
- #include "dncommthread.h"
- #include <stdio.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <termios.h>
- #include <errno.h>
- #include <sys/ioctl.h>
- #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;
- 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<Rtn.length();i++)
- str.append(QString("%1 ").arg(Rtn[i]&0xff,2,16,QChar('0')));
- emit log(str);
- uint t = dt.toTime_t();
- if(((Rtn[0]&0xff)==0xdc)
- &&((Rtn[1]&0xff)==0xcd)
- &&((Rtn[2]&0xff)==0xaa)
- &&((Rtn[3]&0xff)==0xaa)
- &&((Rtn[4]&0xff)==0xe0)
- &&((Rtn[Rtn.length()-2]&0xff)==0xcd)
- &&((Rtn[Rtn.length()-1]&0xff)==0xdc))
- {
- // QByteArray Rtn = QByteArray((char *)rtn,len);
- 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)>=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 = 5;
- timeout.tv_usec = 500000;
- if(ef_idx==0){
- for(int i=0;i<8;i++)
- cmd[i] = ef_cmd_0[i];
- }else if(ef_idx==1){
- for(int i=0;i<8;i++)
- cmd[i] = ef_cmd_1[i];
- }else if(ef_idx==2){
- for(int i=0;i<8;i++)
- cmd[i] = ef_cmd_2[i];
- }
- 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))){
- // t_len = (int)(tmp[2]&0x00ff+5);
- printf("%s <<< ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
- for(int i=0;i<len;i++)
- {
- rtn[i] = tmp[i]&0xff;
- printf("%02x ",rtn[i]);
- }
- printf("\n");
- crc = chk_crcc((quint8 *)tmp, len);
- if((tmp[len-1]==((crc>>8)&0xff))&&(tmp[len-2]==(crc&0xff))){
- printf("crc ok\n");
- if((tmp[0]==0x01)&&(tmp[2]=0xc8)){
- 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_real_value(Id,4,1.0,t);
- else
- set_realtime_real_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_real_value(Id,5,1.0,t);
- else
- set_realtime_real_value(Id,5,0.0,t);
- }
- }
- if((tmp[0]==0x01)&&(tmp[2]==0x7c)){
- 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);
- tmp_v = ((tmp[43]&0x00ff)<<8)|(tmp[44]&0x00ff);
- v = tmp_v*0.001;
- set_realtime_value(Id,20,v,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);
- }
- }
- }
- }
- }
- }
- 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;i<t_len;i++)
- {
- rtn[i] = tmp[i]&0xff;
- printf("%02x ",rtn[i]);
- }
- if((rtn[t_len-2]!=0xcd)||(rtn[t_len-1]!=0xdc)){
- t_len=read(fd,tmp,1024);
- if(t_len>0){
- for(int i=0;i<t_len;i++)
- {
- rtn[i+len]=tmp[i]&0xff;
- printf("%02x ",rtn[i+len]);
- }
- len += t_len;
- }
- }
- printf("\n");
- QByteArray Rtn = QByteArray(len,0);
- for(int i=0;i<len;i++)
- Rtn[i]=rtn[i]&0xff;
- chk_rtn(Rtn);
- return;
- }
- }else{
- // printf("comm %d isset out\n",Id);
- }
- }else{
- // printf("comm %d select <0\n",Id);
- }
- return;
- }
- void DNCommThread::chk_e2(QByteArray d, uint t)
- {
- if(d.length()>=4){
- for(int i=0;i<d.length();i++)
- {
- if(ytShm->sPointList.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;i<d.length();i++)
- {
- if(ytShm->sPointList.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(500000);
- }
- }
|