Comm_Test.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /*
  2. * Comm_Test.c
  3. *
  4. * Created on: 2020¦~4¤ë20¤é
  5. * Author: Wendell
  6. */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include "Comm_Test.h"
  10. #include "IO_Test.h"
  11. #include <sys/ioctl.h>
  12. #include <sys/socket.h>
  13. #include <linux/can.h>
  14. #include <linux/can/raw.h>
  15. #include <linux/wireless.h>
  16. #include <unistd.h> //write, close, usleep, read
  17. #include <string.h>
  18. #include <fcntl.h> //uart
  19. #include <termios.h> //uart
  20. int Can0Fd, Can1Fd, LcmPort, RfidPort, InternalCommPort;
  21. char *LcmPortName = "/dev/ttyS3";
  22. char *RfidPortName = "/dev/ttyS2";
  23. char *InternalCommPortName = "/dev/ttyS5";
  24. //================================================
  25. // initial can-bus 0
  26. //================================================
  27. int InitCan0Bus()
  28. {
  29. int s0,nbytes;
  30. struct timeval tv;
  31. struct ifreq ifr0;
  32. struct sockaddr_can addr0;
  33. system("/sbin/ip link set can0 down");
  34. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  35. system("/sbin/ip link set can0 up");
  36. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  37. tv.tv_sec = 0;
  38. tv.tv_usec = 10000;
  39. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  40. {
  41. #ifdef SystemLogMessage
  42. DEBUG_ERROR("Set SO_RCVTIMEO NG");
  43. #endif
  44. }
  45. nbytes = 40960;
  46. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  47. {
  48. #ifdef SystemLogMessage
  49. DEBUG_ERROR("Set SO_RCVBUF NG");
  50. #endif
  51. }
  52. nbytes = 40960;
  53. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  54. {
  55. #ifdef SystemLogMessage
  56. DEBUG_ERROR("Set SO_SNDBUF NG");
  57. #endif
  58. }
  59. strcpy(ifr0.ifr_name, "can0" );
  60. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  61. addr0.can_family = AF_CAN;
  62. addr0.can_ifindex = ifr0.ifr_ifindex;
  63. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  64. return s0;
  65. }
  66. //================================================
  67. // initial can-bus 1
  68. //================================================
  69. int InitCan1Bus()
  70. {
  71. int s0,nbytes;
  72. struct timeval tv;
  73. struct ifreq ifr0;
  74. struct sockaddr_can addr0;
  75. system("/sbin/ip link set can1 down");
  76. system("/sbin/ip link set can1 type can bitrate 500000 restart-ms 100");
  77. system("/sbin/ip link set can1 up");
  78. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  79. tv.tv_sec = 0;
  80. tv.tv_usec = 10000;
  81. if (setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  82. {
  83. #ifdef SystemLogMessage
  84. PRINTF_LIB_FUNC("Set SO_RCVTIMEO NG");
  85. #endif
  86. }
  87. nbytes = 40960;
  88. if (setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  89. {
  90. #ifdef SystemLogMessage
  91. PRINTF_LIB_FUNC("Set SO_RCVBUF NG");
  92. #endif
  93. }
  94. nbytes = 40960;
  95. if (setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  96. {
  97. #ifdef SystemLogMessage
  98. PRINTF_LIB_FUNC("Set SO_SNDBUF NG");
  99. #endif
  100. }
  101. strcpy(ifr0.ifr_name, "can1" );
  102. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  103. addr0.can_family = AF_CAN;
  104. addr0.can_ifindex = ifr0.ifr_ifindex;
  105. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  106. return s0;
  107. }
  108. void SendCan0Frame(int cmd, unsigned char *data, unsigned char dataLen)
  109. {
  110. struct can_frame frame;
  111. frame.can_id = cmd;
  112. frame.can_dlc = dataLen;
  113. memcpy(frame.data, data, sizeof(frame.data));
  114. write(Can0Fd, &frame, sizeof(struct can_frame));
  115. }
  116. void SendCan1Frame(int cmd, unsigned char *data, unsigned char dataLen)
  117. {
  118. struct can_frame frame;
  119. frame.can_id = cmd;
  120. frame.can_dlc = dataLen;
  121. memcpy(frame.data, data, sizeof(frame.data));
  122. write(Can1Fd, &frame, sizeof(struct can_frame));
  123. }
  124. void Can0TxMessageTest(void)
  125. {
  126. int id = 0x80123456;
  127. unsigned char data[8];
  128. data[0] = 0x55;
  129. data[1] = 0xAA;
  130. SendCan0Frame(id, data, 2);
  131. }
  132. void Can1TxMessageTest(void)
  133. {
  134. int id = 0x80654321;
  135. unsigned char data[8];
  136. data[0] = 0xAA;
  137. data[1] = 0x55;
  138. SendCan1Frame(id, data, 2);
  139. }
  140. //==========================================
  141. // Open LCM Port
  142. //==========================================
  143. int OpenLcmPort()
  144. {
  145. int fd;
  146. struct termios tios;
  147. fd = open(LcmPortName, O_RDWR);
  148. if (fd <= 0)
  149. {
  150. #ifdef SystemLogMessage
  151. DEBUG_ERROR("open /dev/ttyS3 NG \n");
  152. #endif
  153. return -1;
  154. }
  155. ioctl(fd, TCGETS, &tios);
  156. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  157. tios.c_lflag = 0;
  158. tios.c_iflag = 0;
  159. tios.c_oflag = 0;
  160. tios.c_cc[VMIN] = 0;
  161. tios.c_cc[VTIME] = (unsigned char) 5;
  162. tios.c_lflag = 0;
  163. tcflush(fd, TCIFLUSH);
  164. ioctl(fd, TCSETS, &tios);
  165. return fd;
  166. }
  167. void CloseLcmPort()
  168. {
  169. close(LcmPort);
  170. }
  171. //==========================================
  172. // Open RFID Port
  173. //==========================================
  174. int OpenRfidPort()
  175. {
  176. int fd;
  177. struct termios tios;
  178. fd = open(RfidPortName, O_RDWR);
  179. if (fd <= 0)
  180. {
  181. #ifdef SystemLogMessage
  182. DEBUG_ERROR("open /dev/ttyS2 NG \n");
  183. #endif
  184. return -1;
  185. }
  186. ioctl(fd, TCGETS, &tios);
  187. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  188. tios.c_lflag = 0;
  189. tios.c_iflag = 0;
  190. tios.c_oflag = 0;
  191. tios.c_cc[VMIN] = 0;
  192. tios.c_cc[VTIME] = (unsigned char) 5;
  193. tios.c_lflag = 0;
  194. tcflush(fd, TCIFLUSH);
  195. ioctl(fd, TCSETS, &tios);
  196. return fd;
  197. }
  198. void CloseRfidPort()
  199. {
  200. close(RfidPort);
  201. }
  202. void InitRS485DirectionIO(void)
  203. {
  204. //LCD_AC_BIAS_EN => GPIO2_25*//*RS-485 for module DE control
  205. gpio_set_direction(PIN_AM_DE_1, GPIO_DIR_OUTPUT);
  206. gpio_write(PIN_AM_DE_1, 1);
  207. //system("echo 89 > /sys/class/gpio/export");
  208. //system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  209. //system("echo 1 > /sys/class/gpio/gpio89/value");
  210. //LCD_HSYNC => GPIO2_23*//*RS-485 for module RE control
  211. gpio_set_direction(PIN_AM_RE_1, GPIO_DIR_OUTPUT);
  212. gpio_write(PIN_AM_RE_1, 0);
  213. //system("echo 87 > /sys/class/gpio/export");
  214. //system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  215. //system("echo 0 > /sys/class/gpio/gpio87/value");
  216. }
  217. //==========================================
  218. // Open Internal Comm Port
  219. //==========================================
  220. int OpenInternalCommPort()
  221. {
  222. int fd;
  223. struct termios tios;
  224. fd = open(InternalCommPortName, O_RDWR);
  225. if (fd <= 0)
  226. {
  227. #ifdef SystemLogMessage
  228. DEBUG_ERROR("open /dev/ttyS5 NG \n");
  229. #endif
  230. return -1;
  231. }
  232. ioctl(fd, TCGETS, &tios);
  233. tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
  234. tios.c_lflag = 0;
  235. tios.c_iflag = 0;
  236. tios.c_oflag = 0;
  237. tios.c_cc[VMIN] = 0;
  238. tios.c_cc[VTIME] = (unsigned char) 5;
  239. tios.c_lflag = 0;
  240. tcflush(fd, TCIFLUSH);
  241. ioctl(fd, TCSETS, &tios);
  242. return fd;
  243. }
  244. void CloseInternalCommPort()
  245. {
  246. close(InternalCommPort);
  247. }
  248. void DoDCMCommTest(void)
  249. {
  250. struct can_frame frame;
  251. int rxByte = 0;
  252. unsigned char uartTx[2], uartRx[2];
  253. Can0Fd = InitCan0Bus();
  254. Can1Fd = InitCan1Bus();
  255. LcmPort = OpenLcmPort();
  256. RfidPort = OpenRfidPort();
  257. InitRS485DirectionIO();
  258. InternalCommPort = OpenInternalCommPort();
  259. if(Can0Fd < 0)
  260. {
  261. printf("\r\nInit Can Bus 0 fail");
  262. return;
  263. }
  264. if(Can1Fd < 0)
  265. {
  266. printf("\r\nInit Can Bus 1 fail");
  267. return;
  268. }
  269. if(LcmPort < 0)
  270. {
  271. printf("\r\nInit LCM Port fail");
  272. return;
  273. }
  274. if(RfidPort < 0)
  275. {
  276. printf("\r\nInit RFID Port fail");
  277. return;
  278. }
  279. if(InternalCommPort < 0)
  280. {
  281. printf("\r\nInit Internal Comm Port fail");
  282. return;
  283. }
  284. //*************************CAN Bus Test*************************
  285. Can0TxMessageTest();
  286. Can1TxMessageTest();
  287. //usleep(10000);
  288. rxByte = read(Can1Fd, &frame, sizeof(struct can_frame));
  289. if(rxByte > 0 && frame.can_id == 0x80123456 && frame.data[0] == 0x55 && frame.data[1] == 0xAA)
  290. {
  291. printf("\r\nCan Bus 0 Tx OK");
  292. }
  293. else
  294. {
  295. printf("\r\nCan Bus 0 Tx Fail");
  296. return;
  297. }
  298. rxByte = read(Can0Fd, &frame, sizeof(struct can_frame));
  299. if(rxByte > 0 && frame.can_id == 0x80654321 && frame.data[0] == 0xAA && frame.data[1] == 0x55)
  300. {
  301. printf("\r\nCan Bus 1 Tx OK");
  302. }
  303. else
  304. {
  305. printf("\r\nCan Bus 1 Tx Fail");
  306. return;
  307. }
  308. //*************************LCM UART Test*************************
  309. uartTx[0] = 0x55;
  310. uartTx[1] = 0xAA;
  311. write(LcmPort, uartTx, 2);
  312. rxByte = read(LcmPort, uartRx, 2);
  313. CloseLcmPort();
  314. if(rxByte == 2 && uartRx[0] == 0x55 && uartRx[1] == 0xAA)
  315. {
  316. printf("\r\nLCM Port Test OK");
  317. }
  318. else
  319. {
  320. printf("\r\nLCM Port Test Fail");
  321. return;
  322. }
  323. //*************************RFID UART Test*************************
  324. uartTx[0] = 0xAA;
  325. uartTx[1] = 0x55;
  326. write(RfidPort, uartTx, 2);
  327. rxByte = read(RfidPort, uartRx, 2);
  328. CloseRfidPort();
  329. if(rxByte == 2 && uartRx[0] == 0xAA && uartRx[1] == 0x55)
  330. {
  331. printf("\r\nRFID Port Test OK");
  332. }
  333. else
  334. {
  335. printf("\r\nRFID Port Test Fail");
  336. return;
  337. }
  338. //*************************Internal Comm RS485 Test*************************
  339. uartTx[0] = 0x5A;
  340. uartTx[1] = 0x5A;
  341. write(InternalCommPort, uartTx, 2);
  342. rxByte = read(InternalCommPort, uartRx, 2);
  343. CloseInternalCommPort();
  344. if(rxByte == 2 && uartRx[0] == 0x5A && uartRx[1] == 0x5A)
  345. {
  346. printf("\r\nInternal Comm Port Test OK");
  347. }
  348. else
  349. {
  350. printf("\r\nInternal Comm Port Test Fail");
  351. return;
  352. }
  353. printf("\r\nComm Test Done!");
  354. printf("\r\nSuccess!\r\n");
  355. }