123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- #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"
- 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;
- liveList.clear();
- deathList.clear();
- liveCur = 0;
- deathCur = 0;
- 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(19200);
- if(set_Parity(8,1,'N')==-1){
- close(fd);
- return -1;
- }
- return 0;
- }
- void DNCommThread::chk_list()
- {
- liveList.clear();
- deathList.clear();
- liveCur = 0;
- deathCur = 0;
- QString living="living:",death="death:";
- for(quint8 i=1;i<13;i++)
- {
- if(ytShm->sPointList.sPoint[Id][i].ENABLED==0x01)
- {
- }
- }
- emit log(living+"\r\n");
- emit log(death+"\r\n");
- }
- QByteArray DNCommThread::comm_work(QByteArray cmd, bool needrtn)
- {
- QByteArray Rtn;
- fd_set reads;
- struct timeval timeout;
- unsigned char rtn[1024];
- if(write(fd,cmd.data(),cmd.length())==cmd.length())
- {
- if(needrtn)
- {
- 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<len;i++)
- {
- Rtn[i] = rtn[i]&0xff;
- }
- return Rtn;
- }
- }
- }
- }
- }
- Rtn.resize(0);
- return Rtn;
- }
- 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;
- chk_list();
- }
- }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
- }
- }
- usleep(500000);
- }
- }
|