6b2f1226859a46ed2daf17c52407136f05091820.svn-base 14 KB

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