ttyTest.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <errno.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. //#include <sys/file.h>
  10. #include <signal.h>
  11. #include <termios.h>
  12. #include <sys/ioctl.h>
  13. static volatile sig_atomic_t doneflag = 0;
  14. static void setdoneflag(int signo)
  15. {
  16. doneflag = 1;
  17. }
  18. static int initSig(void)
  19. {
  20. struct sigaction act;
  21. act.sa_handler = setdoneflag;
  22. act.sa_flags = 0;
  23. if((sigemptyset(&act.sa_mask) == -1)||
  24. (sigaction(SIGINT,&act,NULL) == -1))
  25. {
  26. perror("Failed to set SIGINT handler");
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. int setSerial(int fd,int nSpeed,int nBits,char nEvent,int nStop)
  32. {
  33. struct termios newtio,oldtio;
  34. if(tcgetattr(fd,&oldtio) != 0)
  35. {
  36. perror("SetupSerial 1");
  37. return 1;
  38. }
  39. bzero(&newtio,sizeof(newtio));
  40. //1:Setting char size
  41. newtio.c_cflag |= CLOCAL | CREAD;
  42. newtio.c_cflag &= ~CSIZE;
  43. //2:Setting bits
  44. switch(nBits)
  45. {
  46. case 7:
  47. newtio.c_cflag |= CS7;
  48. break;
  49. case 8:
  50. newtio.c_cflag |= CS8;
  51. break;
  52. }
  53. //3:Setting parity
  54. switch(nEvent)
  55. {
  56. case 'O':
  57. newtio.c_cflag |= PARENB;
  58. newtio.c_cflag |= PARODD;
  59. newtio.c_cflag |= (INPCK | ISTRIP);
  60. break;
  61. case 'E':
  62. newtio.c_cflag |= (INPCK | ISTRIP);
  63. newtio.c_cflag |= PARENB;
  64. newtio.c_cflag &= ~PARODD;
  65. break;
  66. case 'N':
  67. newtio.c_cflag &= ~PARENB;
  68. break;
  69. }
  70. //4:Setting speed
  71. switch(nSpeed)
  72. {
  73. case 2400:
  74. cfsetispeed(&newtio,B2400);
  75. cfsetospeed(&newtio,B2400);
  76. break;
  77. case 4800:
  78. cfsetispeed(&newtio,B4800);
  79. cfsetospeed(&newtio,B4800);
  80. break;
  81. case 9600:
  82. cfsetispeed(&newtio,B9600);
  83. cfsetospeed(&newtio,B9600);
  84. break;
  85. case 115200:
  86. cfsetispeed(&newtio,B115200);
  87. cfsetospeed(&newtio,B115200);
  88. break;
  89. case 460800:
  90. cfsetispeed(&newtio,B460800);
  91. cfsetospeed(&newtio,B460800);
  92. break;
  93. default:
  94. cfsetispeed(&newtio,B115200);
  95. cfsetospeed(&newtio,B115200);
  96. break;
  97. }
  98. //5:Setting stop bits
  99. switch(nStop)
  100. {
  101. case 1:
  102. newtio.c_cflag &= ~CSTOPB;
  103. break;
  104. case 2:
  105. newtio.c_cflag |= CSTOPB;
  106. break;
  107. }
  108. //6:Setting recvice timeout and mini bytes size of recvice
  109. newtio.c_cc[VTIME] = 0;
  110. newtio.c_cc[VMIN] = 0;
  111. //7:flush input and output buffer
  112. tcflush(fd,TCIOFLUSH);
  113. if(tcsetattr(fd,TCSANOW,&newtio) != 0)
  114. {
  115. perror("serial set error");
  116. return 2;
  117. }
  118. printf("serial setting done\n");
  119. return 0;
  120. }
  121. int openSerial(int fd,unsigned char *SerialName)
  122. {
  123. fd = open(SerialName,O_RDWR|O_NOCTTY|O_NDELAY);
  124. if(fd <0)
  125. {
  126. perror("open");
  127. return fd;
  128. }
  129. #if 1
  130. if(fcntl(fd,F_SETFL,0) < 0)
  131. printf("fcntl failed!\n");
  132. else
  133. printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
  134. #endif
  135. return fd;
  136. }
  137. int rwTest(int fd)
  138. {
  139. int rLen=0,wLen=0,ret=0;
  140. unsigned char buf[3000];
  141. unsigned int i=0,j=0;
  142. unsigned int r_count=0;
  143. #if 1
  144. fd_set rd;
  145. FD_ZERO(&rd);
  146. FD_SET(fd,&rd);
  147. #endif
  148. while(doneflag ==0)
  149. {
  150. #if 1
  151. while(!FD_ISSET(fd,&rd));
  152. if(select(fd+1,&rd,NULL,NULL,NULL) <0)
  153. continue;
  154. #endif
  155. rLen= read(fd,buf,sizeof(buf));
  156. if(rLen < 0)
  157. {
  158. perror("read");
  159. return 1;
  160. }
  161. if(rLen == 0)
  162. {
  163. //sleep(1);
  164. // printf("sleep\n");
  165. continue;
  166. }
  167. //r_count++;
  168. //if(r_count%100 == 0)
  169. // printf("rLen:%d\n",rLen);
  170. wLen = 0;
  171. for(j=0;j<5;j++)
  172. {
  173. ret = write(fd, buf + wLen, rLen - wLen);
  174. if(ret < 0)
  175. {
  176. perror("write");
  177. return 2;
  178. }
  179. wLen += ret;
  180. if(wLen == rLen)
  181. break;
  182. }
  183. if(wLen == rLen)
  184. {
  185. //if(r_count%100 ==0)
  186. // printf("wLen:%d\n",wLen);
  187. }
  188. else
  189. {
  190. printf("write err 1\n");
  191. return 3;
  192. }
  193. }
  194. return 0;
  195. }
  196. int main(int argc, char *argv[])
  197. {
  198. int fd;
  199. int flags;
  200. int ret;
  201. unsigned char SerialName[30]={0};
  202. printf("Version:%s\n","2010.12.30_00");
  203. if(argc == 1)
  204. {
  205. strcpy(SerialName,"/dev/ttyPos0");
  206. }
  207. else if(argc == 2)
  208. {
  209. strcpy(SerialName,argv[1]);
  210. }
  211. else
  212. {
  213. printf("ARG ERROR\n");
  214. return 1;
  215. }
  216. printf("SerialName:%s\n",SerialName);
  217. ret = initSig();
  218. if(ret)
  219. return ret;
  220. fd = openSerial(fd,SerialName);//open(SerialName,O_RDWR);
  221. if(fd <0)
  222. {
  223. exit(1);
  224. }
  225. ret = setSerial(fd,115200,8,'N',1);
  226. if(ret != 0)
  227. {
  228. printf("setSerial:%d\n");
  229. goto byebye;
  230. }
  231. if(ioctl(fd,TIOCMGET,&flags)<0)
  232. {
  233. perror("ioctl failed");
  234. goto byebye;
  235. }
  236. printf("flags:%X\n",flags);
  237. ret = rwTest(fd);
  238. printf("rwTest:%d\n",ret);
  239. byebye:
  240. printf("doneflag:%d\n",doneflag);
  241. ret = close(fd);
  242. if(ret <0)
  243. {
  244. perror("close");
  245. }
  246. else
  247. {
  248. printf("close success!\n");
  249. }
  250. exit(0);
  251. }