59d0da76b7c7fcc1e739a30b6739e1ca1d65dd30.svn-base 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. extern YT_UNIT_SHM *ytShm;
  12. int speed_arr[] = { B115200, B57600, B38400, B19200, B9600, B4800,
  13. B2400, B1200};
  14. int name_arr[] = {115200, 57600, 38400, 19200, 9600, 4800, 2400, 1200};
  15. DNCommThread::DNCommThread(QObject *parent, quint8 id, bool enabled, QString commpath) :
  16. QThread(parent)
  17. {
  18. Id = id;
  19. liveList.clear();
  20. deathList.clear();
  21. liveCur = 0;
  22. deathCur = 0;
  23. Enabled = enabled;
  24. CommOpened = false;
  25. CommPath = commpath;
  26. reopen = false;
  27. }
  28. void DNCommThread::setparam(QString path, bool enabled)
  29. {
  30. Enabled = enabled;
  31. CommPath = path;
  32. if(CommOpened)
  33. {
  34. reopen = true;
  35. }
  36. }
  37. void DNCommThread::set_speed(int speed)
  38. {
  39. uint i;
  40. int status;
  41. struct termios Opt;
  42. tcgetattr(fd, &Opt);
  43. for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
  44. {
  45. if (speed == name_arr[i])
  46. {
  47. tcflush(fd, TCIOFLUSH);
  48. cfsetispeed(&Opt, speed_arr[i]);
  49. cfsetospeed(&Opt, speed_arr[i]);
  50. status = tcsetattr(fd, TCSANOW, &Opt);
  51. if (status != 0)
  52. perror("tcsetattr fd1");
  53. return;
  54. }
  55. tcflush(fd,TCIOFLUSH);
  56. }
  57. }
  58. int DNCommThread::set_Parity(int databits, int stopbits, int parity)
  59. {
  60. struct termios options;
  61. if ( tcgetattr( fd,&options) != 0)
  62. {
  63. perror("SetupSerial 1");
  64. return -1;
  65. }
  66. options.c_cflag &= ~CSIZE;
  67. switch (databits)
  68. {
  69. case 7:
  70. options.c_cflag |= CS7;
  71. break;
  72. case 8:
  73. options.c_cflag |= CS8;
  74. break;
  75. default:
  76. fprintf(stderr,"Unsupported data size\n");
  77. return -1;
  78. }
  79. switch (parity)
  80. {
  81. case 'n':
  82. case 'N':
  83. options.c_cflag &= ~PARENB;
  84. options.c_iflag &= ~INPCK;
  85. break;
  86. case 'o':
  87. case 'O':
  88. options.c_cflag |= (PARODD | PARENB);
  89. options.c_iflag |= INPCK;
  90. break;
  91. case 'e':
  92. case 'E':
  93. options.c_cflag |= PARENB;
  94. options.c_cflag &= ~PARODD;
  95. options.c_iflag |= INPCK;
  96. break;
  97. case 'S':
  98. case 's':
  99. options.c_cflag &= ~PARENB;
  100. options.c_cflag &= ~CSTOPB;
  101. break;
  102. default:
  103. fprintf(stderr,"Unsupported parity\n");
  104. return -1;
  105. }
  106. switch (stopbits)
  107. {
  108. case 1:
  109. options.c_cflag &= ~CSTOPB;
  110. break;
  111. case 2:
  112. options.c_cflag |= CSTOPB;
  113. break;
  114. default:
  115. fprintf(stderr,"Unsupported stop bits\n");
  116. return -1;
  117. }
  118. options.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  119. options.c_oflag &= ~OPOST;
  120. options.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  121. /* Set input parity option */
  122. if (parity != 'n')
  123. options.c_iflag |= INPCK;
  124. options.c_cc[VTIME] = 150; // 15 seconds
  125. options.c_cc[VMIN] = 0;
  126. tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
  127. if (tcsetattr(fd,TCSANOW,&options) != 0)
  128. {
  129. perror("SetupSerial 3");
  130. return -1;
  131. }
  132. return 0;
  133. }
  134. int DNCommThread::open_comm()
  135. {
  136. fd = open(CommPath.toUtf8().data(),O_RDWR);
  137. if(fd==-1)
  138. return -1;
  139. set_speed(19200);
  140. if(set_Parity(8,1,'N')==-1){
  141. close(fd);
  142. return -1;
  143. }
  144. return 0;
  145. }
  146. void DNCommThread::chk_list()
  147. {
  148. liveList.clear();
  149. deathList.clear();
  150. liveCur = 0;
  151. deathCur = 0;
  152. QString living="living:",death="death:";
  153. for(quint8 i=1;i<13;i++)
  154. {
  155. if(ytShm->sPointList.sPoint[Id][i].ENABLED==0x01)
  156. {
  157. }
  158. }
  159. emit log(living+"\r\n");
  160. emit log(death+"\r\n");
  161. }
  162. QByteArray DNCommThread::comm_work(QByteArray cmd, bool needrtn)
  163. {
  164. QByteArray Rtn;
  165. fd_set reads;
  166. struct timeval timeout;
  167. unsigned char rtn[1024];
  168. if(write(fd,cmd.data(),cmd.length())==cmd.length())
  169. {
  170. if(needrtn)
  171. {
  172. FD_ZERO(&reads);
  173. FD_SET(fd,&reads);
  174. timeout.tv_sec = 1;
  175. timeout.tv_usec = 500000;
  176. if(select(fd+1,&reads,NULL,NULL,&timeout)>=0)
  177. {
  178. if(FD_ISSET(fd,&reads))
  179. {
  180. int len = read(fd,rtn,1024);
  181. if(len>0){
  182. Rtn.resize(len);
  183. for(int i=0;i<len;i++)
  184. {
  185. Rtn[i] = rtn[i]&0xff;
  186. }
  187. return Rtn;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. Rtn.resize(0);
  194. return Rtn;
  195. }
  196. void DNCommThread::run()
  197. {
  198. if(Enabled)
  199. {
  200. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  201. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30;
  202. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01;
  203. emit log(QString("[%1] dncomm[%2] thread start\r\n")
  204. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  205. .arg(Id));
  206. }
  207. else
  208. {
  209. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  210. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 0;
  211. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x00;
  212. emit log(QString("[%1] dncomm[%2] thread disabled\r\n")
  213. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  214. .arg(Id));
  215. }
  216. while(1)
  217. {
  218. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].IDX = DNCOMM_THREAD_0+Id;
  219. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].WAITSEC = 30;
  220. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].ENABLED = 0x01;
  221. ytShm->dogTimeList.dogTime[DNCOMM_THREAD_0+Id].LASTFEED
  222. = QDateTime::currentDateTime().toTime_t();
  223. if(!Enabled)
  224. {
  225. if(CommOpened){
  226. close(fd);
  227. emit log(QString("[%1] dncomm[%2] thread stop\r\n")
  228. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  229. .arg(Id));
  230. return;
  231. }
  232. }else{
  233. if(!CommOpened){
  234. if(open_comm()==-1)
  235. {
  236. emit log(QString("[%1] dncomm[%2] open failed\r\n")
  237. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  238. .arg(CommPath));
  239. return;
  240. }else{
  241. emit log(QString("[%1] dncomm[%2] opened\r\n")
  242. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  243. .arg(CommPath));
  244. CommOpened = true;
  245. chk_list();
  246. }
  247. }else if(reopen){
  248. if(CommOpened){
  249. close(fd);
  250. emit log(QString("[%1] dncomm[%2] thread stop\r\n")
  251. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"))
  252. .arg(Id));
  253. CommOpened = false;
  254. }
  255. reopen = false;
  256. }else{//read and write
  257. }
  258. }
  259. usleep(500000);
  260. }
  261. }