d8a7503c87c78c3f06df81bdb5c5627dac09c80f.svn-base 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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. unsigned char ef_cmd_si[] = {0x01, 0x10, 0x10, 0x63, 0x00, 0x01, 0x02, 0x72, 0x59, 0x5a, 0x98};
  20. unsigned char ef_cmd_cl[] = {0x01, 0x10, 0x10, 0x63, 0x00, 0x01, 0x02, 0x12, 0x34, 0xb3, 0x75};
  21. unsigned char ef_cmd_st[] = {0x01, 0x10, 0x10, 0x34, 0x00, 0x01, 0x02, 0x00, 0x3c, 0x00, 0x00};
  22. unsigned char ef_cmd_sc[] = {0x01, 0x10, 0x12, 0x3c, 0x00, 0x01, 0x02, 0x00, 0x64, 0x00, 0x00};
  23. unsigned short T0 = 0x0000;//线缆1温度上限
  24. unsigned short T0W = 0x0000;//线缆1温度上限过滤时间
  25. unsigned short T1 = 0x0000;//线缆2温度上限
  26. unsigned short T1W = 0x0000;//线缆2温度上限过滤时间
  27. unsigned short T2 = 0x0000;//电流上限
  28. unsigned short T2W = 0x0000;//电流上限过滤时间
  29. bool need_st0, need_st1, need_st2, need_cls, isWaitting;
  30. typedef union {
  31. float f;
  32. unsigned short s[2];
  33. }UNFI;
  34. DNCommThread::DNCommThread(QObject *parent, quint8 id, bool enabled, QString commpath) :
  35. QThread(parent)
  36. {
  37. Id = id;
  38. isWaitting = false;
  39. ef_idx = 0;
  40. noEcho0 = 0;
  41. noEcho1 = 0;
  42. Enabled = enabled;
  43. CommOpened = false;
  44. CommPath = commpath;
  45. CommType = 1;
  46. reopen = false;
  47. need_st0 = false;
  48. need_st1 = false;
  49. need_st2 = false;
  50. // printf("from thread ID:%d\t ENABLED:%d\tPATH:%s\n",Id,Enabled,CommPath.toUtf8().data());
  51. }
  52. void DNCommThread::setCommType(quint16 type)
  53. {
  54. CommType = type;
  55. }
  56. void DNCommThread::setparam(QString path, bool enabled)
  57. {
  58. Enabled = enabled;
  59. CommPath = path;
  60. if(CommOpened)
  61. {
  62. reopen = true;
  63. }
  64. }
  65. void DNCommThread::set_speed(int speed)
  66. {
  67. uint i;
  68. int status;
  69. struct termios Opt;
  70. tcgetattr(fd, &Opt);
  71. for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
  72. {
  73. if (speed == name_arr[i])
  74. {
  75. tcflush(fd, TCIOFLUSH);
  76. cfsetispeed(&Opt, speed_arr[i]);
  77. cfsetospeed(&Opt, speed_arr[i]);
  78. status = tcsetattr(fd, TCSANOW, &Opt);
  79. if (status != 0)
  80. perror("tcsetattr fd1");
  81. return;
  82. }
  83. tcflush(fd,TCIOFLUSH);
  84. }
  85. }
  86. int DNCommThread::set_Parity(int databits, int stopbits, int parity)
  87. {
  88. struct termios options;
  89. if ( tcgetattr( fd,&options) != 0)
  90. {
  91. perror("SetupSerial 1");
  92. return -1;
  93. }
  94. options.c_cflag &= ~CSIZE;
  95. switch (databits)
  96. {
  97. case 7:
  98. options.c_cflag |= CS7;
  99. break;
  100. case 8:
  101. options.c_cflag |= CS8;
  102. break;
  103. default:
  104. fprintf(stderr,"Unsupported data size\n");
  105. return -1;
  106. }
  107. switch (parity)
  108. {
  109. case 'n':
  110. case 'N':
  111. options.c_cflag &= ~PARENB;
  112. options.c_iflag &= ~INPCK;
  113. break;
  114. case 'o':
  115. case 'O':
  116. options.c_cflag |= (PARODD | PARENB);
  117. options.c_iflag |= INPCK;
  118. break;
  119. case 'e':
  120. case 'E':
  121. options.c_cflag |= PARENB;
  122. options.c_cflag &= ~PARODD;
  123. options.c_iflag |= INPCK;
  124. break;
  125. case 'S':
  126. case 's':
  127. options.c_cflag &= ~PARENB;
  128. options.c_cflag &= ~CSTOPB;
  129. break;
  130. default:
  131. fprintf(stderr,"Unsupported parity\n");
  132. return -1;
  133. }
  134. switch (stopbits)
  135. {
  136. case 1:
  137. options.c_cflag &= ~CSTOPB;
  138. break;
  139. case 2:
  140. options.c_cflag |= CSTOPB;
  141. break;
  142. default:
  143. fprintf(stderr,"Unsupported stop bits\n");
  144. return -1;
  145. }
  146. options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  147. options.c_oflag &= ~OPOST;
  148. options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  149. /* Set input parity option */
  150. if (parity != 'n')
  151. options.c_iflag |= INPCK;
  152. options.c_cc[VTIME] = 150; // 15 seconds
  153. options.c_cc[VMIN] = 0;
  154. tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  155. if (tcsetattr(fd,TCSANOW,&options) != 0)
  156. {
  157. perror("SetupSerial 3");
  158. return -1;
  159. }
  160. return 0;
  161. }
  162. int DNCommThread::open_comm()
  163. {
  164. // printf("open comm %d %s\n",Id,CommPath.toUtf8().data());
  165. fd = open(CommPath.toUtf8().data(),O_RDWR);
  166. if(fd==-1)
  167. return -1;
  168. set_speed(9600);
  169. if(set_Parity(8,1,'N')==-1){
  170. close(fd);
  171. return -1;
  172. }
  173. return 0;
  174. }
  175. void DNCommThread::chk_rtn(QByteArray Rtn)
  176. {
  177. printf("Rtn length:%d\n",Rtn.length());
  178. if(Rtn.length()>0){
  179. QDateTime dt = QDateTime::currentDateTime();
  180. QString str = QString("[%1] %2 recv: ").arg(dt.toString("yyyy-MM-dd HH:mm:ss")).arg(Id);
  181. for(int i=0;i<Rtn.length();i++)
  182. str.append(QString("%1 ").arg(Rtn[i]&0xff,2,16,QChar('0')));
  183. emit log(str);
  184. uint t = dt.toTime_t();
  185. if(((Rtn[0]&0xff)==0xdc)
  186. &&((Rtn[1]&0xff)==0xcd)
  187. &&((Rtn[2]&0xff)==0xaa)
  188. &&((Rtn[3]&0xff)==0xaa)
  189. &&((Rtn[4]&0xff)==0xe0)
  190. &&((Rtn[Rtn.length()-2]&0xff)==0xcd)
  191. &&((Rtn[Rtn.length()-1]&0xff)==0xdc))
  192. {
  193. // QByteArray Rtn = QByteArray((char *)rtn,len);
  194. if((Rtn.at(5)&0xff)==0xc1){//定时上报
  195. int cure2 = Rtn.indexOf((char)(0xe2),0);
  196. int cur55 = Rtn.indexOf((char)(0x55),0);
  197. int cure3 = Rtn.indexOf((char)(0xe3),0);
  198. int cure6 = Rtn.indexOf((char)(0xe6),0);
  199. if(((Rtn.at(cure2+1)&0xff)==0x01)
  200. &&((cur55-cure2)>=6)){
  201. chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t);
  202. }
  203. if(((Rtn.at(cure3+1)&0xff)==0x01)
  204. &&((cure6-cure3)>=10)
  205. &&((cure6-cure3)<=18))
  206. {
  207. chk_e3(Rtn.mid(cure3+2,cure6-cure3-1),t);
  208. }
  209. QByteArray e6 = Rtn.mid(cure6);
  210. int base=0;
  211. while(e6.length()>=7)
  212. {
  213. if(((e6.at(1)&0xff)>=1)
  214. &&((e6.at(1)&0xff)<=16)
  215. &&((e6.at(2)&0xff)==0x01)
  216. &&((e6.at(5)&0xff)==0x02)
  217. &&((e6.at(6)&0xff)==0x01))
  218. {
  219. if(ytShm->sPointList.sPoint[Id][128+base].ENABLED==0x01)
  220. {
  221. float v = ((e6.at(3)&0xff)*256.0+(e6.at(4)&0xff)*1.0)/150.0;
  222. set_realtime_value(Id,128+base,v,t);
  223. }
  224. base++;
  225. cure6 = e6.indexOf((char)(0xe6),6);
  226. if(cure6>0)
  227. e6 = e6.mid(cure6);
  228. else
  229. break;
  230. }else{
  231. break;
  232. }
  233. }
  234. }else if((Rtn.at(5)&0xff)==0xf1){//主动上报
  235. int cure2 = Rtn.indexOf((char)(0xe2),0);
  236. int cur55 = Rtn.indexOf((char)(0x55),0);
  237. int cure3 = Rtn.indexOf((char)(0xe3),0);
  238. int cure6 = Rtn.indexOf((char)(0xe6),0);
  239. if(((Rtn.at(cure2+1)&0xff)==0x01)
  240. &&((cur55-cure2)>=6)){
  241. chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t);
  242. }
  243. if(((Rtn.at(cure3+1)&0xff)==0x01)
  244. &&((cure6-cure3)==4))
  245. {
  246. quint8 e3id = ((Rtn.at(cure3+2)&0xf0)>>4)*10+(Rtn.at(cure3+2)&0x0f);
  247. printf("e3id:%d\n",e3id);
  248. if((e3id>=1)&&(e3id<=16))
  249. {
  250. if(ytShm->sPointList.sPoint[Id][e3id].ENABLED==0x01)
  251. {
  252. if((Rtn.at(cure3+3)&0xff)==0x00)
  253. set_realtime_value(Id,e3id,0,t);
  254. else
  255. set_realtime_value(Id,e3id,1,t);
  256. }
  257. }
  258. }
  259. QByteArray e6 = Rtn.mid(cure6);
  260. int base=0;
  261. while(e6.length()>=7)
  262. {
  263. if(((e6.at(1)&0xff)>=1)
  264. &&((e6.at(1)&0xff)<=16)
  265. &&((e6.at(2)&0xff)==0x01)
  266. &&((e6.at(5)&0xff)==0x02)
  267. &&((e6.at(6)&0xff)==0x01))
  268. {
  269. if(ytShm->sPointList.sPoint[Id][128+base].ENABLED==0x01)
  270. {
  271. float v = ((e6.at(3)&0xff)*256.0+(e6.at(4)&0xff)*1.0)/150.0;
  272. set_realtime_value(Id,128+base,v,t);
  273. }
  274. base++;
  275. cure6 = e6.indexOf((char)(0xe6),6);
  276. if(cure6>0)
  277. e6 = e6.mid(cure6);
  278. else
  279. break;
  280. }else{
  281. break;
  282. }
  283. }
  284. }
  285. }
  286. }
  287. }
  288. quint16 DNCommThread::chk_crcc(quint8 *d, int len)
  289. {
  290. int j,i;
  291. quint16 default_data = 0xa001;
  292. quint16 crc = 0xffff;
  293. for(j=0;j<(len-2);j++){
  294. crc ^= (quint16)(d[j]&0x00ff);
  295. for(i=0;i<8;i++){
  296. if(crc&0x01){
  297. crc >>= 1;
  298. crc ^= default_data;//a001,1021
  299. }else
  300. crc >>= 1;
  301. }
  302. }
  303. return crc;
  304. }
  305. void DNCommThread::ef_comm_read()
  306. {
  307. int t_len,len,cmd_len;
  308. uint t = QDateTime::currentDateTime().toTime_t();
  309. fd_set reads;
  310. struct timeval timeout;
  311. unsigned char tmp[1024],cmd[1024];
  312. unsigned char rtn[1024];
  313. UNFI unfi;
  314. FD_ZERO(&reads);
  315. FD_SET(fd,&reads);
  316. timeout.tv_sec = 1;
  317. timeout.tv_usec = 500000;
  318. if(isWaitting)
  319. return;
  320. isWaitting=true;
  321. if(ytShm->sPointList.sPoint[Id][40].UNSAVED==0x10){
  322. ytShm->sPointList.sPoint[Id][40].UNSAVED=0x00;
  323. unfi.f = ytShm->sPointList.sPoint[Id][40].ALARM_H;
  324. for(int i=0;i<11;i++)
  325. cmd[i] = ef_cmd_st[i];
  326. cmd[7] = ((unfi.s[0]>>8)&0xff);
  327. cmd[8] = (unfi.s[0]&0xff);
  328. quint16 crc = chk_crcc((quint8 *)cmd,11);
  329. cmd[9] = crc&0xff;
  330. cmd[10] = (crc>>8)&0xff;
  331. cmd_len = 11;
  332. // printf(">>> ");
  333. // for(int i=0;i<11;i++)
  334. // printf("%02x ",cmd[i]&0xff);
  335. // printf("\n");
  336. len = write(fd,cmd,11);
  337. }else if(ytShm->sPointList.sPoint[Id][41].UNSAVED==0x10){
  338. ytShm->sPointList.sPoint[Id][41].UNSAVED=0x00;
  339. unfi.f = ytShm->sPointList.sPoint[Id][41].ALARM_H;
  340. for(int i=0;i<11;i++)
  341. cmd[i] = ef_cmd_st[i];
  342. cmd[3] = 0x35;
  343. cmd[7] = ((unfi.s[0]>>8)&0xff);
  344. cmd[8] = (unfi.s[0]&0xff);
  345. quint16 crc = chk_crcc((quint8 *)cmd,11);
  346. cmd[9] = crc&0xff;
  347. cmd[10] = (crc>>8)&0xff;
  348. cmd_len = 11;
  349. // printf(">>> ");
  350. // for(int i=0;i<11;i++)
  351. // printf("%02x ",cmd[i]&0xff);
  352. // printf("\n");
  353. len = write(fd,cmd,11);
  354. }else if(ytShm->sPointList.sPoint[Id][20].UNSAVED==0x10){
  355. ytShm->sPointList.sPoint[Id][20].UNSAVED=0x00;
  356. unfi.f = ytShm->sPointList.sPoint[Id][20].ALARM_H;
  357. for(int i=0;i<11;i++)
  358. cmd[i] = ef_cmd_sc[i];
  359. cmd[7] = ((unfi.s[0]>>8)&0xff);
  360. cmd[8] = (unfi.s[0]&0xff);
  361. quint16 crc = chk_crcc((quint8 *)cmd,11);
  362. cmd[9] = crc&0xff;
  363. cmd[10] = (crc>>8)&0xff;
  364. // printf(">>> ");
  365. // for(int i=0;i<11;i++)
  366. // printf("%02x ",cmd[i]&0xff);
  367. // printf("\n");
  368. cmd_len = 11;
  369. len = write(fd,cmd,11);
  370. }else if((need_cls)||(ytShm->sPointList.sPoint[Id][0].UNSAVED==0x12)){
  371. ytShm->sPointList.sPoint[Id][0].UNSAVED=0x00;
  372. for(int i=0;i<11;i++)
  373. cmd[i] = ef_cmd_cl[i];
  374. quint16 crc = chk_crcc((quint8 *)cmd,11);
  375. cmd[9] = crc&0xff;
  376. cmd[10] = (crc>>8)&0xff;
  377. cmd_len = 11;
  378. len = write(fd,cmd,11);
  379. need_cls=false;
  380. }else if(ytShm->sPointList.sPoint[Id][0].UNSAVED==0x13){
  381. ytShm->sPointList.sPoint[Id][0].UNSAVED=0x00;
  382. for(int i=0;i<11;i++)
  383. cmd[i] = ef_cmd_si[i];
  384. quint16 crc = chk_crcc((quint8 *)cmd,11);
  385. cmd[9] = crc&0xff;
  386. cmd[10] = (crc>>8)&0xff;
  387. cmd_len = 11;
  388. len = write(fd,cmd,11);
  389. }else{
  390. if(ef_idx==0){
  391. for(int i=0;i<8;i++)
  392. cmd[i] = ef_cmd_0[i];
  393. noEcho0++;
  394. }else if(ef_idx==1){
  395. for(int i=0;i<8;i++)
  396. cmd[i] = ef_cmd_1[i];
  397. noEcho0++;
  398. }else if(ef_idx==2){
  399. for(int i=0;i<8;i++)
  400. cmd[i] = ef_cmd_2[i];
  401. noEcho1++;
  402. }
  403. quint16 crc = chk_crcc((quint8 *)cmd,8);
  404. cmd[6] = crc&0xff;
  405. cmd[7] = (crc>>8)&0xff;
  406. cmd_len = 8;
  407. len = write(fd,cmd,8);
  408. }
  409. need_cls=false;
  410. if(len==cmd_len)
  411. {
  412. if(select(fd+1,&reads,NULL,NULL,&timeout)>=0)
  413. {
  414. if(FD_ISSET(fd,&reads))
  415. {
  416. len= read(fd,tmp,1024);
  417. while((len>=5)&&(len<((int)(tmp[2]&0x00ff)+5))&&(tmp[2]!=0x10)){
  418. usleep(50000);
  419. t_len = read(fd,rtn,1024);
  420. if(t_len==0)
  421. break;
  422. for(int i=0;i<t_len;i++)
  423. tmp[len+i] = rtn[i];
  424. len += t_len;
  425. }
  426. if(tmp[1]==0x10){
  427. if(need_st0||need_st1||need_st2||need_cls){
  428. if(need_st0)
  429. need_st0=false;
  430. else if(need_st1)
  431. need_st1=false;
  432. else if(need_st2)
  433. need_st2=false;
  434. else
  435. need_cls=false;
  436. }
  437. }
  438. if((len>=5)&&(len>=((int)(tmp[2]&0x00ff)+5))){
  439. for(int i=0;i<len;i++)
  440. {
  441. rtn[i] = tmp[i]&0xff;
  442. }
  443. quint16 crc = chk_crcc((quint8 *)tmp, len);
  444. if((tmp[len-1]==((crc>>8)&0xff))&&(tmp[len-2]==(crc&0xff))){
  445. if((tmp[0]==0x01)&&(tmp[2]==0xc8)){
  446. noEcho0 = 0;
  447. if(noEcho1==0)
  448. set_realtime_value(Id,7,0,t);
  449. quint16 loop_idx = ((tmp[3]&0x00ff)<<8)|(tmp[4]&0x00ff);
  450. // if((loop_idx&0x0001)==0x0001)
  451. {
  452. quint16 tmp_v = ((tmp[13]&0x00ff)<<8)|(tmp[14]&0x00ff);
  453. float v = ((qint16)tmp_v)*0.1;
  454. set_realtime_value(Id,39,v,t);
  455. tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff);
  456. if((tmp_v&0x0001)==0x0001)
  457. set_realtime_value(Id,4,1.0,t);
  458. else
  459. set_realtime_value(Id,4,0.0,t);
  460. }
  461. // if((loop_idx&0x0002)==0x0002)
  462. {
  463. quint16 tmp_v = ((tmp[15]&0x00ff)<<8)|(tmp[16]&0x00ff);
  464. float v = ((qint16)tmp_v)*0.1;
  465. set_realtime_value(Id,40,v,t);
  466. // tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff);
  467. // printf("alarm_sta:%04x\n",tmp_v);
  468. // if((tmp_v&0x0002)==2)
  469. // set_realtime_value(Id,5,1.0,t);
  470. // else{
  471. // if(ytShm->spStatusList.spStatus[Id][5].ALARM_STATUS!=0x00)
  472. // need_cls = true;
  473. // set_realtime_value(Id,5,0.0,t);
  474. // }
  475. }
  476. // if((loop_idx&0x0004)==0x0004)
  477. {
  478. quint16 tmp_v = ((tmp[17]&0x00ff)<<8)|(tmp[18]&0x00ff);
  479. float v = ((qint16)tmp_v)*0.1;
  480. set_realtime_value(Id,41,v,t);
  481. // tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff);
  482. // if((tmp_v&0x0004)==4)
  483. // set_realtime_value(Id,5,1.0,t);
  484. // else{
  485. // if(ytShm->spStatusList.spStatus[Id][5].ALARM_STATUS!=0x00)
  486. // need_cls = true;
  487. // set_realtime_value(Id,5,0.0,t);
  488. // }
  489. }
  490. if((loop_idx&0x0008)==0x0008){
  491. quint16 tmp_v = ((tmp[19]&0x00ff)<<8)|(tmp[20]&0x00ff);
  492. float v = ((qint16)tmp_v)*0.1;
  493. set_realtime_value(Id,42,v,t);
  494. }
  495. if((loop_idx&0x0010)==0x0010){
  496. quint16 tmp_v = ((tmp[21]&0x00ff)<<8)|(tmp[22]&0x00ff);
  497. float v = ((qint16)tmp_v)*0.1;
  498. set_realtime_value(Id,43,v,t);
  499. }
  500. quint16 tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff);
  501. // printf("alarm_sta:%04x\n",tmp_v);
  502. if(((tmp_v&0x0002)==0x0002)&&(ytShm->sPointList.sPoint[Id][40].ENABLED==0x01))
  503. set_realtime_value(Id,5,1.0,t);
  504. else if(((tmp_v&0x0004)==0x0004)&&(ytShm->sPointList.sPoint[Id][41].ENABLED==0x01))
  505. set_realtime_value(Id,5,1.0,t);
  506. else if(((tmp_v&0x0008)==0x0008)&&(ytShm->sPointList.sPoint[Id][42].ENABLED==0x01))
  507. set_realtime_value(Id,5,1.0,t);
  508. else if(((tmp_v&0x0010)==0x0010)&&(ytShm->sPointList.sPoint[Id][43].ENABLED==0x01))
  509. set_realtime_value(Id,5,1.0,t);
  510. else{
  511. if(ytShm->spStatusList.spStatus[Id][5].ALARM_STATUS!=0x00)
  512. need_cls = true;
  513. set_realtime_value(Id,5,0.0,t);
  514. }
  515. if(T0==0x0000){
  516. T0 = ((tmp[107]&0x00ff)<<8)|(tmp[108]&0x00ff);
  517. need_st0 = false;
  518. }else{
  519. if(ytShm->sPointList.sPoint[Id][40].UNSAVED==0x10)
  520. {
  521. unfi.f = ytShm->sPointList.sPoint[Id][40].ALARM_H;
  522. // printf("temp_1[%d][%d]:%d\t(%d)\t",Id,40,T0,unfi.s[0]);
  523. if(unfi.s[0]==0x0000){
  524. need_st0 = false;
  525. // ytShm->sPointList.sPoint[Id][40].UNSAVED=0x00;
  526. // printf("need_st0:false\n");
  527. }else{
  528. need_st0 = true;
  529. // printf("need_st0:true\n");
  530. }
  531. // ytShm->sPointList.sPoint[Id][40].UNSAVED=0x00;
  532. }
  533. }
  534. if(T1==0x0000){
  535. T1 = ((tmp[109]&0x00ff)<<8)|(tmp[110]&0x00ff);
  536. need_st1 = false;
  537. }else{
  538. if(ytShm->sPointList.sPoint[Id][41].UNSAVED==0x10)
  539. {
  540. unfi.f = ytShm->sPointList.sPoint[Id][41].ALARM_H;
  541. // printf("temp_2[%d][%d]:%d\t(%d)\t",Id,41,T1,unfi.s[0]);
  542. if(unfi.s[0]==0x0000){
  543. // ytShm->sPointList.sPoint[Id][41].UNSAVED=0x00;
  544. // printf("need_st1:false\n");
  545. need_st1 = false;
  546. }else{
  547. need_st1 = true;
  548. // printf("need_st1:true\n");
  549. }
  550. // ytShm->sPointList.sPoint[Id][41].UNSAVED=0x00;
  551. }
  552. }
  553. }else if((tmp[0]==0x01)&&(tmp[2]==0x7c)){
  554. noEcho0 = 0;
  555. if(noEcho1==0)
  556. set_realtime_value(Id,7,0,t);
  557. quint16 tmp_v = ((tmp[9]&0x00ff)<<8)|(tmp[10]&0x00ff);
  558. float v = tmp_v*0.01;
  559. set_realtime_value(Id,16,v,t);
  560. tmp_v = ((tmp[11]&0x00ff)<<8)|(tmp[12]&0x00ff);
  561. v = tmp_v*0.1;
  562. set_realtime_value(Id,17,v,t);
  563. if(tmp[29]==0x01)
  564. set_realtime_value(Id,1,1.0,t);
  565. else
  566. set_realtime_value(Id,1,0.0,t);
  567. if(tmp[30]==0x01)
  568. set_realtime_value(Id,2,1.0,t);
  569. else
  570. set_realtime_value(Id,2,0.0,t);
  571. tmp_v = ((tmp[43]&0x00ff)<<8)|(tmp[44]&0x00ff);
  572. v = tmp_v*0.001*10;
  573. set_realtime_value(Id,20,v,t);
  574. if(tmp[56]==0x01)
  575. set_realtime_value(Id,3,1.0,t);
  576. else{
  577. if(ytShm->spStatusList.spStatus[Id][3].ALARM_STATUS!=0x00)
  578. need_cls = true;
  579. set_realtime_value(Id,3,0.0,t);
  580. }
  581. tmp_v = ((tmp[63]&0x00ff)<<8)|(tmp[64]&0x00ff);
  582. v = tmp_v*0.001;
  583. set_realtime_value(Id,23,v,t);
  584. tmp_v = ((tmp[69]&0x00ff)<<8)|(tmp[70]&0x00ff);
  585. v = tmp_v*0.001;
  586. set_realtime_value(Id,26,v,t);
  587. tmp_v = ((tmp[75]&0x00ff)<<8)|(tmp[76]&0x00ff);
  588. v = tmp_v*0.001;
  589. set_realtime_value(Id,29,v,t);
  590. tmp_v = ((tmp[81]&0x00ff)<<8)|(tmp[82]&0x00ff);
  591. v = tmp_v*0.001;
  592. set_realtime_value(Id,32,v,t);
  593. tmp_v = ((tmp[93]&0x00ff)<<8)|(tmp[94]&0x00ff);
  594. v = tmp_v*0.001;
  595. set_realtime_value(Id,35,v,t);
  596. tmp_v = ((tmp[95]&0x00ff)<<8)|(tmp[96]&0x00ff);
  597. v = tmp_v*0.001;
  598. set_realtime_value(Id,36,v,t);
  599. tmp_v = ((tmp[97]&0x00ff)<<8)|(tmp[98]&0x00ff);
  600. v = tmp_v*0.001;
  601. set_realtime_value(Id,37,v,t);
  602. tmp_v = ((tmp[99]&0x00ff)<<8)|(tmp[100]&0x00ff);
  603. v = tmp_v*0.001;
  604. set_realtime_value(Id,38,v,t);
  605. if(T2==0x0000){
  606. T2 = ((tmp[123]&0x00ff)<<8)|(tmp[124]&0x00ff);
  607. need_st2 = false;
  608. }else{
  609. if(ytShm->sPointList.sPoint[Id][20].UNSAVED==0x10)
  610. {
  611. unfi.f = ytShm->sPointList.sPoint[Id][20].ALARM_H;
  612. // printf("current[%d][%d]:%d\t(%d)\t",Id,20,T2,unfi.s[0]);
  613. if(unfi.s[0]==0x0000){
  614. // ytShm->sPointList.sPoint[Id][20].UNSAVED=0x00;
  615. // printf("need_st2:false\n");
  616. need_st2=false;
  617. }else{
  618. need_st2 = true;
  619. // printf("need_st2:false\n");
  620. }
  621. // ytShm->sPointList.sPoint[Id][20].UNSAVED=0x00;
  622. }
  623. }
  624. }else if((tmp[0]==0x02)&&(tmp[2]==0x02)){
  625. noEcho1 = 0;
  626. if(noEcho0==0)
  627. set_realtime_value(Id,7,0,t);
  628. quint16 tmp_v = ((tmp[3]&0x00ff)<<8)|(tmp[4]&0x00ff);
  629. if(tmp_v==0x0001)
  630. set_realtime_value(Id,6,1.0,t);
  631. else
  632. set_realtime_value(Id,6,0.0,t);
  633. }
  634. // }else{
  635. // printf("chk err\n");
  636. // printf(">>> ");
  637. // for(int i=0;i<cmd_len;i++)
  638. // printf("%02x ",cmd[i]&0xff);
  639. // printf("\n");
  640. // printf("<<< ");
  641. // for(int i=0;i<len;i++)
  642. // printf("%02X ",rtn[i]&0xff);
  643. // printf("\n");
  644. // printf("crcc:%04x\n",crc);
  645. }
  646. }
  647. }
  648. }
  649. }
  650. if(noEcho0>3){
  651. noEcho0=3;
  652. set_realtime_value(Id,7,1.0,t);
  653. }
  654. if(noEcho1>6){
  655. noEcho1=6;
  656. set_realtime_value(Id,7,1.0,t);
  657. }
  658. ef_idx++;
  659. if(ef_idx>2)
  660. ef_idx=0;
  661. isWaitting=false;
  662. }
  663. void DNCommThread::comm_read()
  664. {
  665. int t_len,len;
  666. fd_set reads;
  667. struct timeval timeout;
  668. unsigned char tmp[1024];
  669. unsigned char rtn[1024];
  670. FD_ZERO(&reads);
  671. FD_SET(fd,&reads);
  672. timeout.tv_sec = 1;
  673. timeout.tv_usec = 500000;
  674. // printf("comm %d read\n",Id);
  675. if(select(fd+1,&reads,NULL,NULL,&timeout)>=0)
  676. {
  677. if(FD_ISSET(fd,&reads))
  678. {
  679. // printf("comm %d reading\n",Id);
  680. t_len= read(fd,tmp,1024);
  681. len=t_len;
  682. // printf("comm %d length:%d\n",Id,len);
  683. if(t_len>0){
  684. printf("%s ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  685. for(int i=0;i<t_len;i++)
  686. {
  687. rtn[i] = tmp[i]&0xff;
  688. printf("%02x ",rtn[i]);
  689. }
  690. if((rtn[t_len-2]!=0xcd)||(rtn[t_len-1]!=0xdc)){
  691. t_len=read(fd,tmp,1024);
  692. if(t_len>0){
  693. for(int i=0;i<t_len;i++)
  694. {
  695. rtn[i+len]=tmp[i]&0xff;
  696. printf("%02x ",rtn[i+len]);
  697. }
  698. len += t_len;
  699. }
  700. }
  701. printf("\n");
  702. QByteArray Rtn = QByteArray(len,0);
  703. for(int i=0;i<len;i++)
  704. Rtn[i]=rtn[i]&0xff;
  705. chk_rtn(Rtn);
  706. return;
  707. }
  708. }else{
  709. // printf("comm %d isset out\n",Id);
  710. }
  711. }else{
  712. // printf("comm %d select <0\n",Id);
  713. }
  714. return;
  715. }
  716. void DNCommThread::chk_e2(QByteArray d, uint t)
  717. {
  718. if(d.length()>=4){
  719. for(int i=0;i<d.length();i++)
  720. {
  721. if(ytShm->sPointList.sPoint[Id][64+i].ENABLED==0x01)
  722. {
  723. if((d.at(i)&0xff)==0x00)
  724. set_realtime_value(Id,64+i,0,t);
  725. else
  726. set_realtime_value(Id,64+i,1,t);
  727. }
  728. }
  729. }
  730. }
  731. void DNCommThread::chk_e3(QByteArray d, uint t)
  732. {
  733. for(int i=0;i<d.length();i++)
  734. {
  735. if(ytShm->sPointList.sPoint[Id][1+i].ENABLED==0x01)
  736. {
  737. if((d.at(i)&0xff)==0x00)
  738. set_realtime_value(Id,1+i,0,t);
  739. else
  740. set_realtime_value(Id,1+i,1,t);
  741. }
  742. }
  743. }
  744. void DNCommThread::run()
  745. {
  746. if(Enabled)
  747. {
  748. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  749. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30;
  750. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01;
  751. emit log(QString("[%1] dncomm[%2] thread start\r\n")
  752. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  753. .arg(Id));
  754. }
  755. else
  756. {
  757. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  758. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 0;
  759. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x00;
  760. emit log(QString("[%1] dncomm[%2] thread disabled\r\n")
  761. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  762. .arg(Id));
  763. }
  764. while(1)
  765. {
  766. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  767. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30;
  768. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01;
  769. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED
  770. = QDateTime::currentDateTime().toTime_t();
  771. // printf("dncomm %d last feed %d\n",Id,ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED);
  772. if(!Enabled)
  773. {
  774. if(CommOpened){
  775. close(fd);
  776. emit log(QString("[%1] dncomm[%2] thread stop\r\n")
  777. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  778. .arg(Id));
  779. return;
  780. }
  781. }else{
  782. if(!CommOpened){
  783. if(open_comm()==-1)
  784. {
  785. emit log(QString("[%1] dncomm[%2] open failed\r\n")
  786. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  787. .arg(CommPath));
  788. return;
  789. }else{
  790. emit log(QString("[%1] dncomm[%2] opened\r\n")
  791. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  792. .arg(CommPath));
  793. CommOpened = true;
  794. }
  795. }else if(reopen){
  796. if(CommOpened){
  797. close(fd);
  798. emit log(QString("[%1] dncomm[%2] thread stop\r\n")
  799. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  800. .arg(Id));
  801. CommOpened = false;
  802. }
  803. reopen = false;
  804. }else{//read and write
  805. // comm_read(&len,&rtn);
  806. if(CommType==1)
  807. comm_read();
  808. else if(CommType==2)
  809. ef_comm_read();
  810. }
  811. }
  812. usleep(300000);
  813. }
  814. }