56bd688d98511479d4c5245f7fced0032155ed68.svn-base 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. #include "dncommthread.h"
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/stat.h>
  7. #include <termios.h>
  8. #include <errno.h>
  9. #include <sys/ioctl.h>
  10. #include "yt_unit_shm.h"
  11. #include "realvalue.h"
  12. extern YT_UNIT_SHM *ytShm;
  13. int speed_arr[] = { B115200, B57600, B38400, B19200, B9600, B4800,
  14. B2400, B1200};
  15. int name_arr[] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200};
  16. unsigned char ef_cmd_0[] = {0x01, 0x03, 0x10, 0x00, 0x00, 0x64, 0x00, 0x00};
  17. unsigned char ef_cmd_1[] = {0x01, 0x03, 0x12, 0x00, 0x00, 0x3e, 0x00, 0x00};
  18. unsigned char ef_cmd_2[] = {0x02, 0x03, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00};
  19. DNCommThread::DNCommThread(QObject *parent, quint8 id, bool enabled, QString commpath) :
  20. QThread(parent)
  21. {
  22. Id = id;
  23. ef_idx = 0;
  24. noEcho0 = 0;
  25. noEcho1 = 0;
  26. Enabled = enabled;
  27. CommOpened = false;
  28. CommPath = commpath;
  29. CommType = 1;
  30. reopen = false;
  31. // printf("from thread ID:%d\t ENABLED:%d\tPATH:%s\n",Id,Enabled,CommPath.toUtf8().data());
  32. }
  33. void DNCommThread::setCommType(quint16 type)
  34. {
  35. CommType = type;
  36. }
  37. void DNCommThread::setparam(QString path, bool enabled)
  38. {
  39. Enabled = enabled;
  40. CommPath = path;
  41. if(CommOpened)
  42. {
  43. reopen = true;
  44. }
  45. }
  46. void DNCommThread::set_speed(int speed)
  47. {
  48. uint i;
  49. int status;
  50. struct termios Opt;
  51. tcgetattr(fd, &Opt);
  52. for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
  53. {
  54. if (speed == name_arr[i])
  55. {
  56. tcflush(fd, TCIOFLUSH);
  57. cfsetispeed(&Opt, speed_arr[i]);
  58. cfsetospeed(&Opt, speed_arr[i]);
  59. status = tcsetattr(fd, TCSANOW, &Opt);
  60. if (status != 0)
  61. perror("tcsetattr fd1");
  62. return;
  63. }
  64. tcflush(fd,TCIOFLUSH);
  65. }
  66. }
  67. int DNCommThread::set_Parity(int databits, int stopbits, int parity)
  68. {
  69. struct termios options;
  70. if ( tcgetattr( fd,&options) != 0)
  71. {
  72. perror("SetupSerial 1");
  73. return -1;
  74. }
  75. options.c_cflag &= ~CSIZE;
  76. switch (databits)
  77. {
  78. case 7:
  79. options.c_cflag |= CS7;
  80. break;
  81. case 8:
  82. options.c_cflag |= CS8;
  83. break;
  84. default:
  85. fprintf(stderr,"Unsupported data size\n");
  86. return -1;
  87. }
  88. switch (parity)
  89. {
  90. case 'n':
  91. case 'N':
  92. options.c_cflag &= ~PARENB;
  93. options.c_iflag &= ~INPCK;
  94. break;
  95. case 'o':
  96. case 'O':
  97. options.c_cflag |= (PARODD | PARENB);
  98. options.c_iflag |= INPCK;
  99. break;
  100. case 'e':
  101. case 'E':
  102. options.c_cflag |= PARENB;
  103. options.c_cflag &= ~PARODD;
  104. options.c_iflag |= INPCK;
  105. break;
  106. case 'S':
  107. case 's':
  108. options.c_cflag &= ~PARENB;
  109. options.c_cflag &= ~CSTOPB;
  110. break;
  111. default:
  112. fprintf(stderr,"Unsupported parity\n");
  113. return -1;
  114. }
  115. switch (stopbits)
  116. {
  117. case 1:
  118. options.c_cflag &= ~CSTOPB;
  119. break;
  120. case 2:
  121. options.c_cflag |= CSTOPB;
  122. break;
  123. default:
  124. fprintf(stderr,"Unsupported stop bits\n");
  125. return -1;
  126. }
  127. options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  128. options.c_oflag &= ~OPOST;
  129. options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  130. /* Set input parity option */
  131. if (parity != 'n')
  132. options.c_iflag |= INPCK;
  133. options.c_cc[VTIME] = 150; // 15 seconds
  134. options.c_cc[VMIN] = 0;
  135. tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  136. if (tcsetattr(fd,TCSANOW,&options) != 0)
  137. {
  138. perror("SetupSerial 3");
  139. return -1;
  140. }
  141. return 0;
  142. }
  143. int DNCommThread::open_comm()
  144. {
  145. // printf("open comm %d %s\n",Id,CommPath.toUtf8().data());
  146. fd = open(CommPath.toUtf8().data(),O_RDWR);
  147. if(fd==-1)
  148. return -1;
  149. set_speed(9600);
  150. if(set_Parity(8,1,'N')==-1){
  151. close(fd);
  152. return -1;
  153. }
  154. return 0;
  155. }
  156. void DNCommThread::chk_rtn(QByteArray Rtn)
  157. {
  158. printf("Rtn length:%d\n",Rtn.length());
  159. if(Rtn.length()>0){
  160. QDateTime dt = QDateTime::currentDateTime();
  161. QString str = QString("[%1] %2 recv: ").arg(dt.toString("yyyy-MM-dd HH:mm:ss")).arg(Id);
  162. for(int i=0;i<Rtn.length();i++)
  163. str.append(QString("%1 ").arg(Rtn[i]&0xff,2,16,QChar('0')));
  164. emit log(str);
  165. uint t = dt.toTime_t();
  166. if(((Rtn[0]&0xff)==0xdc)
  167. &&((Rtn[1]&0xff)==0xcd)
  168. &&((Rtn[2]&0xff)==0xaa)
  169. &&((Rtn[3]&0xff)==0xaa)
  170. &&((Rtn[4]&0xff)==0xe0)
  171. &&((Rtn[Rtn.length()-2]&0xff)==0xcd)
  172. &&((Rtn[Rtn.length()-1]&0xff)==0xdc))
  173. {
  174. // QByteArray Rtn = QByteArray((char *)rtn,len);
  175. if((Rtn.at(5)&0xff)==0xc1){//定时上报
  176. int cure2 = Rtn.indexOf((char)(0xe2),0);
  177. int cur55 = Rtn.indexOf((char)(0x55),0);
  178. int cure3 = Rtn.indexOf((char)(0xe3),0);
  179. int cure6 = Rtn.indexOf((char)(0xe6),0);
  180. if(((Rtn.at(cure2+1)&0xff)==0x01)
  181. &&((cur55-cure2)>=6)){
  182. chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t);
  183. }
  184. if(((Rtn.at(cure3+1)&0xff)==0x01)
  185. &&((cure6-cure3)>=10)
  186. &&((cure6-cure3)<=18))
  187. {
  188. chk_e3(Rtn.mid(cure3+2,cure6-cure3-1),t);
  189. }
  190. QByteArray e6 = Rtn.mid(cure6);
  191. int base=0;
  192. while(e6.length()>=7)
  193. {
  194. if(((e6.at(1)&0xff)>=1)
  195. &&((e6.at(1)&0xff)<=16)
  196. &&((e6.at(2)&0xff)==0x01)
  197. &&((e6.at(5)&0xff)==0x02)
  198. &&((e6.at(6)&0xff)==0x01))
  199. {
  200. if(ytShm->sPointList.sPoint[Id][128+base].ENABLED==0x01)
  201. {
  202. float v = ((e6.at(3)&0xff)*256.0+(e6.at(4)&0xff)*1.0)/150.0;
  203. set_realtime_value(Id,128+base,v,t);
  204. }
  205. base++;
  206. cure6 = e6.indexOf((char)(0xe6),6);
  207. if(cure6>0)
  208. e6 = e6.mid(cure6);
  209. else
  210. break;
  211. }else{
  212. break;
  213. }
  214. }
  215. }else if((Rtn.at(5)&0xff)==0xf1){//主动上报
  216. int cure2 = Rtn.indexOf((char)(0xe2),0);
  217. int cur55 = Rtn.indexOf((char)(0x55),0);
  218. int cure3 = Rtn.indexOf((char)(0xe3),0);
  219. int cure6 = Rtn.indexOf((char)(0xe6),0);
  220. if(((Rtn.at(cure2+1)&0xff)==0x01)
  221. &&((cur55-cure2)>=6)){
  222. chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t);
  223. }
  224. if(((Rtn.at(cure3+1)&0xff)==0x01)
  225. &&((cure6-cure3)==4))
  226. {
  227. quint8 e3id = ((Rtn.at(cure3+2)&0xf0)>>4)*10+(Rtn.at(cure3+2)&0x0f);
  228. printf("e3id:%d\n",e3id);
  229. if((e3id>=1)&&(e3id<=16))
  230. {
  231. if(ytShm->sPointList.sPoint[Id][e3id].ENABLED==0x01)
  232. {
  233. if((Rtn.at(cure3+3)&0xff)==0x00)
  234. set_realtime_value(Id,e3id,0,t);
  235. else
  236. set_realtime_value(Id,e3id,1,t);
  237. }
  238. }
  239. }
  240. QByteArray e6 = Rtn.mid(cure6);
  241. int base=0;
  242. while(e6.length()>=7)
  243. {
  244. if(((e6.at(1)&0xff)>=1)
  245. &&((e6.at(1)&0xff)<=16)
  246. &&((e6.at(2)&0xff)==0x01)
  247. &&((e6.at(5)&0xff)==0x02)
  248. &&((e6.at(6)&0xff)==0x01))
  249. {
  250. if(ytShm->sPointList.sPoint[Id][128+base].ENABLED==0x01)
  251. {
  252. float v = ((e6.at(3)&0xff)*256.0+(e6.at(4)&0xff)*1.0)/150.0;
  253. set_realtime_value(Id,128+base,v,t);
  254. }
  255. base++;
  256. cure6 = e6.indexOf((char)(0xe6),6);
  257. if(cure6>0)
  258. e6 = e6.mid(cure6);
  259. else
  260. break;
  261. }else{
  262. break;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. }
  269. quint16 DNCommThread::chk_crcc(quint8 *d, int len)
  270. {
  271. int j,i;
  272. quint16 default_data = 0xa001;
  273. quint16 crc = 0xffff;
  274. for(j=0;j<(len-2);j++){
  275. crc ^= (quint16)(d[j]&0x00ff);
  276. for(i=0;i<8;i++){
  277. if(crc&0x01){
  278. crc >>= 1;
  279. crc ^= default_data;//a001,1021
  280. }else
  281. crc >>= 1;
  282. }
  283. }
  284. return crc;
  285. }
  286. void DNCommThread::ef_comm_read()
  287. {
  288. int t_len,len;
  289. uint t = QDateTime::currentDateTime().toTime_t();
  290. fd_set reads;
  291. struct timeval timeout;
  292. unsigned char tmp[1024],cmd[1024];
  293. unsigned char rtn[1024];
  294. FD_ZERO(&reads);
  295. FD_SET(fd,&reads);
  296. timeout.tv_sec = 1;
  297. timeout.tv_usec = 500000;
  298. if(ef_idx==0){
  299. for(int i=0;i<8;i++)
  300. cmd[i] = ef_cmd_0[i];
  301. noEcho0++;
  302. }else if(ef_idx==1){
  303. for(int i=0;i<8;i++)
  304. cmd[i] = ef_cmd_1[i];
  305. noEcho0++;
  306. }else if(ef_idx==2){
  307. for(int i=0;i<8;i++)
  308. cmd[i] = ef_cmd_2[i];
  309. noEcho1++;
  310. }
  311. quint16 crc = chk_crcc((quint8 *)cmd,8);
  312. cmd[6] = crc&0xff;
  313. cmd[7] = (crc>>8)&0xff;
  314. // printf("%s >>> ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  315. // for(int i=0;i<8;i++)
  316. // printf("%02x ",cmd[i]);
  317. // printf("\n");
  318. len = write(fd,cmd,8);
  319. if(len==8)
  320. {
  321. if(select(fd+1,&reads,NULL,NULL,&timeout)>=0)
  322. {
  323. if(FD_ISSET(fd,&reads))
  324. {
  325. len= read(fd,tmp,1024);
  326. // len=t_len;
  327. if((len>=5)&&(len<((int)(tmp[2]&0x00ff)+5))){
  328. // printf("recv again\n");
  329. usleep(50000);
  330. t_len = read(fd,rtn,1024);
  331. for(int i=0;i<t_len;i++)
  332. tmp[len+i] = rtn[i];
  333. len += t_len;
  334. }
  335. if((len>=5)&&(len>=((int)(tmp[2]&0x00ff)+5))){
  336. // t_len = (int)(tmp[2]&0x00ff+5);
  337. // printf("len: %d %d\n",len,((int)(tmp[2]&0x00ff)+5));
  338. // printf("%s <<< ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  339. for(int i=0;i<len;i++)
  340. {
  341. rtn[i] = tmp[i]&0xff;
  342. // printf("%02x ",rtn[i]);
  343. }
  344. // printf("\n");
  345. crc = chk_crcc((quint8 *)tmp, len);
  346. if((tmp[len-1]==((crc>>8)&0xff))&&(tmp[len-2]==(crc&0xff))){
  347. // printf("crc ok\n");
  348. if((tmp[0]==0x01)&&(tmp[2]==0xc8)){
  349. noEcho0 = 0;
  350. if(noEcho1==0)
  351. set_realtime_value(Id,7,0,t);
  352. quint16 loop_idx = ((tmp[3]&0x00ff)<<8)|(tmp[4]&0x00ff);
  353. if((loop_idx&0x0001)==0x0000){
  354. quint16 tmp_v = ((tmp[13]&0x00ff)<<8)|(tmp[14]&0x00ff);
  355. float v = ((qint16)tmp_v)*0.1;
  356. set_realtime_value(Id,39,v,t);
  357. tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff);
  358. if((tmp_v&0x0001)==0x0001)
  359. set_realtime_value(Id,4,1.0,t);
  360. else
  361. set_realtime_value(Id,4,0.0,t);
  362. }
  363. if((loop_idx&0x0002)==0x0002){
  364. quint16 tmp_v = ((tmp[15]&0x00ff)<<8)|(tmp[16]&0x00ff);
  365. float v = ((qint16)tmp_v)*0.1;
  366. set_realtime_value(Id,40,v,t);
  367. tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff);
  368. if((tmp_v&0x0002)==0x0002)
  369. set_realtime_value(Id,5,1.0,t);
  370. else
  371. set_realtime_value(Id,5,0.0,t);
  372. }
  373. }else if((tmp[0]==0x01)&&(tmp[2]==0x7c)){
  374. noEcho0 = 0;
  375. if(noEcho1==0)
  376. set_realtime_value(Id,7,0,t);
  377. quint16 tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff);
  378. float v = tmp_v*0.01;
  379. set_realtime_value(Id,16,v,t);
  380. tmp_v = ((tmp[11]&0x00ff)<<8)|(tmp[12]&0x00ff);
  381. v = tmp_v*0.1;
  382. set_realtime_value(Id,17,v,t);
  383. if(tmp[29]==0x01)
  384. set_realtime_value(Id,1,1.0,t);
  385. else
  386. set_realtime_value(Id,1,0.0,t);
  387. if(tmp[30]==0x01)
  388. set_realtime_value(Id,2,1.0,t);
  389. else
  390. set_realtime_value(Id,2,0.0,t);
  391. tmp_v = ((tmp[43]&0x00ff)<<8)|(tmp[44]&0x00ff);
  392. v = tmp_v*0.001;
  393. set_realtime_value(Id,20,v,t);
  394. if(tmp[56]==0x01)
  395. set_realtime_value(Id,3,1.0,t);
  396. else
  397. set_realtime_value(Id,3,0.0,t);
  398. tmp_v = ((tmp[63]&0x00ff)<<8)|(tmp[64]&0x00ff);
  399. v = tmp_v*0.001;
  400. set_realtime_value(Id,23,v,t);
  401. tmp_v = ((tmp[69]&0x00ff)<<8)|(tmp[70]&0x00ff);
  402. v = tmp_v*0.001;
  403. set_realtime_value(Id,26,v,t);
  404. tmp_v = ((tmp[75]&0x00ff)<<8)|(tmp[76]&0x00ff);
  405. v = tmp_v*0.001;
  406. set_realtime_value(Id,29,v,t);
  407. tmp_v = ((tmp[81]&0x00ff)<<8)|(tmp[82]&0x00ff);
  408. v = tmp_v*0.001;
  409. set_realtime_value(Id,32,v,t);
  410. tmp_v = ((tmp[93]&0x00ff)<<8)|(tmp[94]&0x00ff);
  411. v = tmp_v*0.001;
  412. set_realtime_value(Id,35,v,t);
  413. tmp_v = ((tmp[95]&0x00ff)<<8)|(tmp[96]&0x00ff);
  414. v = tmp_v*0.001;
  415. set_realtime_value(Id,36,v,t);
  416. tmp_v = ((tmp[97]&0x00ff)<<8)|(tmp[98]&0x00ff);
  417. v = tmp_v*0.001;
  418. set_realtime_value(Id,37,v,t);
  419. tmp_v = ((tmp[99]&0x00ff)<<8)|(tmp[100]&0x00ff);
  420. v = tmp_v*0.001;
  421. set_realtime_value(Id,38,v,t);
  422. }else if((tmp[0]==0x02)&&(tmp[2]==0x02)){
  423. noEcho1 = 0;
  424. if(noEcho0==0)
  425. set_realtime_value(Id,7,0,t);
  426. quint16 tmp_v = ((tmp[3]&0x00ff)<<8)|(tmp[4]&0x00ff);
  427. if(tmp_v==0x0001)
  428. set_realtime_value(Id,6,1.0,t);
  429. else
  430. set_realtime_value(Id,6,0.0,t);
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. if(noEcho0>18){
  438. noEcho0=18;
  439. set_realtime_value(Id,7,1.0,t);
  440. }
  441. if(noEcho1>9){
  442. noEcho1=9;
  443. set_realtime_value(Id,7,1.0,t);
  444. }
  445. ef_idx++;
  446. if(ef_idx>2)
  447. ef_idx=0;
  448. }
  449. void DNCommThread::comm_read()
  450. {
  451. int t_len,len;
  452. fd_set reads;
  453. struct timeval timeout;
  454. unsigned char tmp[1024];
  455. unsigned char rtn[1024];
  456. FD_ZERO(&reads);
  457. FD_SET(fd,&reads);
  458. timeout.tv_sec = 1;
  459. timeout.tv_usec = 500000;
  460. // printf("comm %d read\n",Id);
  461. if(select(fd+1,&reads,NULL,NULL,&timeout)>=0)
  462. {
  463. if(FD_ISSET(fd,&reads))
  464. {
  465. // printf("comm %d reading\n",Id);
  466. t_len= read(fd,tmp,1024);
  467. len=t_len;
  468. // printf("comm %d length:%d\n",Id,len);
  469. if(t_len>0){
  470. printf("%s ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  471. for(int i=0;i<t_len;i++)
  472. {
  473. rtn[i] = tmp[i]&0xff;
  474. printf("%02x ",rtn[i]);
  475. }
  476. if((rtn[t_len-2]!=0xcd)||(rtn[t_len-1]!=0xdc)){
  477. t_len=read(fd,tmp,1024);
  478. if(t_len>0){
  479. for(int i=0;i<t_len;i++)
  480. {
  481. rtn[i+len]=tmp[i]&0xff;
  482. printf("%02x ",rtn[i+len]);
  483. }
  484. len += t_len;
  485. }
  486. }
  487. printf("\n");
  488. QByteArray Rtn = QByteArray(len,0);
  489. for(int i=0;i<len;i++)
  490. Rtn[i]=rtn[i]&0xff;
  491. chk_rtn(Rtn);
  492. return;
  493. }
  494. }else{
  495. // printf("comm %d isset out\n",Id);
  496. }
  497. }else{
  498. // printf("comm %d select <0\n",Id);
  499. }
  500. return;
  501. }
  502. void DNCommThread::chk_e2(QByteArray d, uint t)
  503. {
  504. if(d.length()>=4){
  505. for(int i=0;i<d.length();i++)
  506. {
  507. if(ytShm->sPointList.sPoint[Id][64+i].ENABLED==0x01)
  508. {
  509. if((d.at(i)&0xff)==0x00)
  510. set_realtime_value(Id,64+i,0,t);
  511. else
  512. set_realtime_value(Id,64+i,1,t);
  513. }
  514. }
  515. }
  516. }
  517. void DNCommThread::chk_e3(QByteArray d, uint t)
  518. {
  519. for(int i=0;i<d.length();i++)
  520. {
  521. if(ytShm->sPointList.sPoint[Id][1+i].ENABLED==0x01)
  522. {
  523. if((d.at(i)&0xff)==0x00)
  524. set_realtime_value(Id,1+i,0,t);
  525. else
  526. set_realtime_value(Id,1+i,1,t);
  527. }
  528. }
  529. }
  530. void DNCommThread::run()
  531. {
  532. if(Enabled)
  533. {
  534. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  535. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30;
  536. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01;
  537. emit log(QString("[%1] dncomm[%2] thread start\r\n")
  538. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  539. .arg(Id));
  540. }
  541. else
  542. {
  543. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  544. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 0;
  545. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x00;
  546. emit log(QString("[%1] dncomm[%2] thread disabled\r\n")
  547. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  548. .arg(Id));
  549. }
  550. while(1)
  551. {
  552. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  553. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30;
  554. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01;
  555. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED
  556. = QDateTime::currentDateTime().toTime_t();
  557. // printf("dncomm %d last feed %d\n",Id,ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED);
  558. if(!Enabled)
  559. {
  560. if(CommOpened){
  561. close(fd);
  562. emit log(QString("[%1] dncomm[%2] thread stop\r\n")
  563. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  564. .arg(Id));
  565. return;
  566. }
  567. }else{
  568. if(!CommOpened){
  569. if(open_comm()==-1)
  570. {
  571. emit log(QString("[%1] dncomm[%2] open failed\r\n")
  572. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  573. .arg(CommPath));
  574. return;
  575. }else{
  576. emit log(QString("[%1] dncomm[%2] opened\r\n")
  577. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  578. .arg(CommPath));
  579. CommOpened = true;
  580. }
  581. }else if(reopen){
  582. if(CommOpened){
  583. close(fd);
  584. emit log(QString("[%1] dncomm[%2] thread stop\r\n")
  585. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  586. .arg(Id));
  587. CommOpened = false;
  588. }
  589. reopen = false;
  590. }else{//read and write
  591. // comm_read(&len,&rtn);
  592. if(CommType==1)
  593. comm_read();
  594. else if(CommType==2)
  595. ef_comm_read();
  596. }
  597. }
  598. usleep(1000000);
  599. }
  600. }