a09470351e6f35f0e0d172b053a748d55ffaf7e8.svn-base 19 KB

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