56a08df737c078919d1980672820a88b48be9b38.svn-base 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. DNCommThread::DNCommThread(QObject *parent, quint8 id, bool enabled, QString commpath) :
  17. QThread(parent)
  18. {
  19. Id = id;
  20. Enabled = enabled;
  21. CommOpened = false;
  22. CommPath = commpath;
  23. reopen = false;
  24. printf("from thread ID:%d\t ENABLED:%d\tPATH:%s\n",Id,Enabled,CommPath.toUtf8().data());
  25. }
  26. void DNCommThread::setparam(QString path, bool enabled)
  27. {
  28. Enabled = enabled;
  29. CommPath = path;
  30. if(CommOpened)
  31. {
  32. reopen = true;
  33. }
  34. }
  35. void DNCommThread::set_speed(int speed)
  36. {
  37. uint i;
  38. int status;
  39. struct termios Opt;
  40. tcgetattr(fd, &Opt);
  41. for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
  42. {
  43. if (speed == name_arr[i])
  44. {
  45. tcflush(fd, TCIOFLUSH);
  46. cfsetispeed(&Opt, speed_arr[i]);
  47. cfsetospeed(&Opt, speed_arr[i]);
  48. status = tcsetattr(fd, TCSANOW, &Opt);
  49. if (status != 0)
  50. perror("tcsetattr fd1");
  51. return;
  52. }
  53. tcflush(fd,TCIOFLUSH);
  54. }
  55. }
  56. int DNCommThread::set_Parity(int databits, int stopbits, int parity)
  57. {
  58. struct termios options;
  59. if ( tcgetattr( fd,&options) != 0)
  60. {
  61. perror("SetupSerial 1");
  62. return -1;
  63. }
  64. options.c_cflag &= ~CSIZE;
  65. switch (databits)
  66. {
  67. case 7:
  68. options.c_cflag |= CS7;
  69. break;
  70. case 8:
  71. options.c_cflag |= CS8;
  72. break;
  73. default:
  74. fprintf(stderr,"Unsupported data size\n");
  75. return -1;
  76. }
  77. switch (parity)
  78. {
  79. case 'n':
  80. case 'N':
  81. options.c_cflag &= ~PARENB;
  82. options.c_iflag &= ~INPCK;
  83. break;
  84. case 'o':
  85. case 'O':
  86. options.c_cflag |= (PARODD | PARENB);
  87. options.c_iflag |= INPCK;
  88. break;
  89. case 'e':
  90. case 'E':
  91. options.c_cflag |= PARENB;
  92. options.c_cflag &= ~PARODD;
  93. options.c_iflag |= INPCK;
  94. break;
  95. case 'S':
  96. case 's':
  97. options.c_cflag &= ~PARENB;
  98. options.c_cflag &= ~CSTOPB;
  99. break;
  100. default:
  101. fprintf(stderr,"Unsupported parity\n");
  102. return -1;
  103. }
  104. switch (stopbits)
  105. {
  106. case 1:
  107. options.c_cflag &= ~CSTOPB;
  108. break;
  109. case 2:
  110. options.c_cflag |= CSTOPB;
  111. break;
  112. default:
  113. fprintf(stderr,"Unsupported stop bits\n");
  114. return -1;
  115. }
  116. options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  117. options.c_oflag &= ~OPOST;
  118. options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  119. /* Set input parity option */
  120. if (parity != 'n')
  121. options.c_iflag |= INPCK;
  122. options.c_cc[VTIME] = 150; // 15 seconds
  123. options.c_cc[VMIN] = 0;
  124. tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  125. if (tcsetattr(fd,TCSANOW,&options) != 0)
  126. {
  127. perror("SetupSerial 3");
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. int DNCommThread::open_comm()
  133. {
  134. printf("open comm %d %s\n",Id,CommPath.toUtf8().data());
  135. fd = open(CommPath.toUtf8().data(),O_RDWR);
  136. if(fd==-1)
  137. return -1;
  138. set_speed(9600);
  139. if(set_Parity(8,1,'N')==-1){
  140. close(fd);
  141. return -1;
  142. }
  143. return 0;
  144. }
  145. void DNCommThread::chk_rtn(QByteArray Rtn)
  146. {
  147. printf("Rtn length:%d\n",Rtn.length());
  148. if(Rtn.length()>0){
  149. QDateTime dt = QDateTime::currentDateTime();
  150. QString str = QString("[%1] %2 recv: ").arg(dt.toString("yyyy-MM-dd HH:mm:ss")).arg(Id);
  151. for(int i=0;i<Rtn.length();i++)
  152. str.append(QString("%1 ").arg(Rtn[i]&0xff,2,16,QChar('0')));
  153. emit log(str);
  154. uint t = dt.toTime_t();
  155. if(((Rtn[0]&0xff)==0xdc)
  156. &&((Rtn[1]&0xff)==0xcd)
  157. &&((Rtn[2]&0xff)==0xaa)
  158. &&((Rtn[3]&0xff)==0xaa)
  159. &&((Rtn[4]&0xff)==0xe0)
  160. &&((Rtn[Rtn.length()-2]&0xff)==0xcd)
  161. &&((Rtn[Rtn.length()-1]&0xff)==0xdc))
  162. {
  163. // QByteArray Rtn = QByteArray((char *)rtn,len);
  164. if((Rtn.at(5)&0xff)==0xc1){//定时上报
  165. int cure2 = Rtn.indexOf((char)(0xe2),0);
  166. int cur55 = Rtn.indexOf((char)(0x55),0);
  167. int cure3 = Rtn.indexOf((char)(0xe3),0);
  168. int cure6 = Rtn.indexOf((char)(0xe6),0);
  169. if(((Rtn.at(cure2+1)&0xff)==0x01)
  170. &&((cur55-cure2)>=6)){
  171. chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t);
  172. }
  173. if(((Rtn.at(cure3+1)&0xff)==0x01)
  174. &&((cure6-cure3)>=10)
  175. &&((cure6-cure3)<=18))
  176. {
  177. chk_e3(Rtn.mid(cure3+2,cure6-cure3-1),t);
  178. }
  179. QByteArray e6 = Rtn.mid(cure6);
  180. int base=0;
  181. while(e6.length()>=7)
  182. {
  183. if(((e6.at(1)&0xff)>=1)
  184. &&((e6.at(1)&0xff)<=16)
  185. &&((e6.at(2)&0xff)==0x01)
  186. &&((e6.at(5)&0xff)==0x02)
  187. &&((e6.at(6)&0xff)==0x01))
  188. {
  189. if(ytShm->sPointList.sPoint[Id][128+base].ENABLED==0x01)
  190. {
  191. float v = ((e6.at(3)&0xff)*256.0+(e6.at(4)&0xff)*1.0)/150.0;
  192. set_realtime_value(Id,128+base,v,t);
  193. }
  194. base++;
  195. cure6 = e6.indexOf((char)(0xe6),6);
  196. if(cure6>0)
  197. e6 = e6.mid(cure6);
  198. else
  199. break;
  200. }else{
  201. break;
  202. }
  203. }
  204. }else if((Rtn.at(5)&0xff)==0xf1){//主动上报
  205. int cure2 = Rtn.indexOf((char)(0xe2),0);
  206. int cur55 = Rtn.indexOf((char)(0x55),0);
  207. int cure3 = Rtn.indexOf((char)(0xe3),0);
  208. int cure6 = Rtn.indexOf((char)(0xe6),0);
  209. if(((Rtn.at(cure2+1)&0xff)==0x01)
  210. &&((cur55-cure2)>=6)){
  211. chk_e2(Rtn.mid(cure2+2,cur55-cure2-2),t);
  212. }
  213. if(((Rtn.at(cure3+1)&0xff)==0x01)
  214. &&((cure6-cure3)==4))
  215. {
  216. quint8 e3id = ((Rtn.at(cure3+2)&0xf0)>>4)*10+(Rtn.at(cure3+2)&0x0f);
  217. printf("e3id:%d\n",e3id);
  218. if((e3id>=1)&&(e3id<=16))
  219. {
  220. if(ytShm->sPointList.sPoint[Id][e3id].ENABLED==0x01)
  221. {
  222. if((Rtn.at(cure3+3)&0xff)==0x00)
  223. set_realtime_value(Id,e3id,0,t);
  224. else
  225. set_realtime_value(Id,e3id,1,t);
  226. }
  227. }
  228. }
  229. QByteArray e6 = Rtn.mid(cure6);
  230. int base=0;
  231. while(e6.length()>=7)
  232. {
  233. if(((e6.at(1)&0xff)>=1)
  234. &&((e6.at(1)&0xff)<=16)
  235. &&((e6.at(2)&0xff)==0x01)
  236. &&((e6.at(5)&0xff)==0x02)
  237. &&((e6.at(6)&0xff)==0x01))
  238. {
  239. if(ytShm->sPointList.sPoint[Id][128+base].ENABLED==0x01)
  240. {
  241. float v = ((e6.at(3)&0xff)*256.0+(e6.at(4)&0xff)*1.0)/150.0;
  242. set_realtime_value(Id,128+base,v,t);
  243. }
  244. base++;
  245. cure6 = e6.indexOf((char)(0xe6),6);
  246. if(cure6>0)
  247. e6 = e6.mid(cure6);
  248. else
  249. break;
  250. }else{
  251. break;
  252. }
  253. }
  254. }
  255. }
  256. }
  257. }
  258. void DNCommThread::comm_read()
  259. {
  260. int t_len,len;
  261. fd_set reads;
  262. struct timeval timeout;
  263. unsigned char tmp[1024];
  264. unsigned char rtn[1024];
  265. FD_ZERO(&reads);
  266. FD_SET(fd,&reads);
  267. timeout.tv_sec = 1;
  268. timeout.tv_usec = 500000;
  269. // printf("comm %d read\n",Id);
  270. if(select(fd+1,&reads,NULL,NULL,&timeout)>=0)
  271. {
  272. if(FD_ISSET(fd,&reads))
  273. {
  274. // printf("comm %d reading\n",Id);
  275. t_len= read(fd,tmp,1024);
  276. len=t_len;
  277. // printf("comm %d length:%d\n",Id,len);
  278. if(t_len>0){
  279. printf("%s ",QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss").toUtf8().data());
  280. for(int i=0;i<t_len;i++)
  281. {
  282. rtn[i] = tmp[i]&0xff;
  283. printf("%02x ",rtn[i]);
  284. }
  285. if((rtn[t_len-2]!=0xcd)||(rtn[t_len-1]!=0xdc)){
  286. t_len=read(fd,tmp,1024);
  287. if(t_len>0){
  288. for(int i=0;i<t_len;i++)
  289. {
  290. rtn[i+len]=tmp[i]&0xff;
  291. printf("%02x ",rtn[i+len]);
  292. }
  293. len += t_len;
  294. }
  295. }
  296. printf("\n");
  297. QByteArray Rtn = QByteArray(len,0);
  298. for(int i=0;i<len;i++)
  299. Rtn[i]=rtn[i]&0xff;
  300. chk_rtn(Rtn);
  301. return;
  302. }
  303. }else{
  304. // printf("comm %d isset out\n",Id);
  305. }
  306. }else{
  307. // printf("comm %d select <0\n",Id);
  308. }
  309. return;
  310. }
  311. void DNCommThread::chk_e2(QByteArray d, uint t)
  312. {
  313. if(d.length()>=4){
  314. for(int i=0;i<d.length();i++)
  315. {
  316. if(ytShm->sPointList.sPoint[Id][64+i].ENABLED==0x01)
  317. {
  318. if((d.at(i)&0xff)==0x00)
  319. set_realtime_value(Id,64+i,0,t);
  320. else
  321. set_realtime_value(Id,64+i,1,t);
  322. }
  323. }
  324. }
  325. }
  326. void DNCommThread::chk_e3(QByteArray d, uint t)
  327. {
  328. for(int i=0;i<d.length();i++)
  329. {
  330. if(ytShm->sPointList.sPoint[Id][1+i].ENABLED==0x01)
  331. {
  332. if((d.at(i)&0xff)==0x00)
  333. set_realtime_value(Id,1+i,0,t);
  334. else
  335. set_realtime_value(Id,1+i,1,t);
  336. }
  337. }
  338. }
  339. void DNCommThread::run()
  340. {
  341. if(Enabled)
  342. {
  343. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  344. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30;
  345. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01;
  346. emit log(QString("[%1] dncomm[%2] thread start\r\n")
  347. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  348. .arg(Id));
  349. }
  350. else
  351. {
  352. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  353. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 0;
  354. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x00;
  355. emit log(QString("[%1] dncomm[%2] thread disabled\r\n")
  356. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  357. .arg(Id));
  358. }
  359. while(1)
  360. {
  361. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  362. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30;
  363. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01;
  364. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED
  365. = QDateTime::currentDateTime().toTime_t();
  366. // printf("dncomm %d last feed %d\n",Id,ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED);
  367. if(!Enabled)
  368. {
  369. if(CommOpened){
  370. close(fd);
  371. emit log(QString("[%1] dncomm[%2] thread stop\r\n")
  372. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  373. .arg(Id));
  374. return;
  375. }
  376. }else{
  377. if(!CommOpened){
  378. if(open_comm()==-1)
  379. {
  380. emit log(QString("[%1] dncomm[%2] open failed\r\n")
  381. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  382. .arg(CommPath));
  383. return;
  384. }else{
  385. emit log(QString("[%1] dncomm[%2] opened\r\n")
  386. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  387. .arg(CommPath));
  388. CommOpened = true;
  389. }
  390. }else if(reopen){
  391. if(CommOpened){
  392. close(fd);
  393. emit log(QString("[%1] dncomm[%2] thread stop\r\n")
  394. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  395. .arg(Id));
  396. CommOpened = false;
  397. }
  398. reopen = false;
  399. }else{//read and write
  400. // comm_read(&len,&rtn);
  401. comm_read();
  402. }
  403. }
  404. usleep(500000);
  405. }
  406. }