JMPlc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/socket.h>
  7. #include <netinet/in.h>
  8. #include <netdb.h>
  9. #include <pthread.h>
  10. #include <errno.h>
  11. #include "pxTools.h"
  12. #include "pxEpoll.h"
  13. #include "JMcan.h"
  14. #include "canTransmit.h"
  15. #include "JMPlc.h"
  16. extern CONFIG_S config;
  17. extern POSTDATABUF_S postData;
  18. extern int epollfd;
  19. static int serverfd=-1;
  20. static int setMode=1;
  21. static char ModeToSet=0;
  22. static unsigned short packIndex=0;
  23. static double wheelSet=0;
  24. static int wheelFlag=1;
  25. void PlcSetMode(int mode)
  26. {
  27. ModeToSet=mode&0xff;
  28. if(mode==REMOTECONTROL)
  29. // postData.wheelSet=postData.wheelGet;
  30. wheelSet=0;
  31. setMode=1;
  32. }
  33. void wheelData(double *w)
  34. {
  35. if(wheelFlag)
  36. {
  37. wheelSet=0;
  38. wheelFlag=0;
  39. }
  40. wheelSet+=*w;
  41. pxLog(DEBUG,"[PLC] set wheel[%2f] by %2f",wheelSet,*w);
  42. }
  43. int PlcSetWheel()
  44. {
  45. static char buf[15]={0x00,0x00,0x00,0x00,0x00,0x09,0x01,0x10,0x01,0xA5,0x00,0x01,0x02,0x00,0x01};
  46. packIndex++;
  47. buf[0]=(packIndex>>8)&0xff;
  48. buf[1]=packIndex&0xff;
  49. // short wheel=(short)((*(double*)(&postData.wheelSet))*100);
  50. short wheel=(short)wheelSet;
  51. memcpy(&postData.wheelSet,&wheelSet,8);
  52. pxLog(DEBUG,"[PLC] send wheel [%d]",wheel);
  53. wheelFlag=1;
  54. buf[13]=(wheel>>8)&0xff;
  55. buf[14]=wheel&0xff;
  56. send(serverfd,buf,sizeof(buf),0);
  57. char retbuf[512]={0};
  58. int n=recv(serverfd,retbuf,sizeof(retbuf),0);
  59. if(n<=0)
  60. return -1;
  61. return 0;
  62. }
  63. int PlcGetWheel()
  64. {
  65. // Tx:000131-06 79 00 00 00 06 01 03 01 F6 00 01
  66. // Rx:000132-06 79 00 00 00 05 01 03 02 00 00
  67. static char buf[12]={0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x3,0x01,0xF6,0x00,0x01};
  68. packIndex++;
  69. buf[0]=(packIndex>>8)&0xff;
  70. buf[1]=packIndex&0xff;
  71. send(serverfd,buf,sizeof(buf),0);
  72. char retbuf[512]={0};
  73. int n=recv(serverfd,retbuf,sizeof(retbuf),0);
  74. if(n<=0)
  75. return -1;
  76. if(n>=11 && retbuf[7]==0x03)
  77. {
  78. short wheel=0;
  79. *((short*)&wheel)=retbuf[9]<<8 | retbuf[10];
  80. *((double*)&postData.wheelGet)=(double)wheel/100;
  81. }
  82. return 0;
  83. }
  84. int PlcGetLegs()
  85. {
  86. // Tx:000131-06 79 00 00 00 06 01 03 02 04 00 02
  87. // Rx:000132-06 79 00 00 00 05 01 03 04 00 00 00 00
  88. static char buf[12]={0x00,0x00,0x00,0x00,0x00,0x06,0x01,0x3,0x02,0x08,0x00,0x07};
  89. packIndex++;
  90. buf[0]=(packIndex>>8)&0xff;
  91. buf[1]=packIndex&0xff;
  92. send(serverfd,buf,sizeof(buf),0);
  93. char retbuf[512]={0};
  94. int n=recv(serverfd,retbuf,sizeof(retbuf),0);
  95. if(n<=0)
  96. return -1;
  97. if(n>=23 && retbuf[7]==0x03)
  98. {
  99. memcpy(postData.plcdata,retbuf+9,sizeof(PLCRECVDATA_S));
  100. // short left=retbuf[9]<<8 | retbuf[10];
  101. // short right=retbuf[11]<<8 | retbuf[12];
  102. // postData.LeftLeg=(left/config.LeftLeg)*100;
  103. // postData.RightLeg=(right/config.RightLeg)*100;
  104. }
  105. return 0;
  106. }
  107. int sendMode()
  108. {
  109. static char buf[15]={0x00,0x00,0x00,0x00,0x00,0x09,0x01,0x10,0x01,0x90,0x00,0x01,0x02,0x00,0x01};
  110. packIndex++;
  111. buf[0]=(packIndex>>8)&0xff;
  112. buf[1]=packIndex&0xff;
  113. buf[14]=ModeToSet;
  114. send(serverfd,buf,sizeof(buf),0);
  115. char retbuf[512]={0};
  116. int n=recv(serverfd,retbuf,sizeof(retbuf),0);
  117. if(n<=0)
  118. return -1;
  119. if(retbuf[7]==0x10)
  120. setMode=0;
  121. return 0;
  122. }
  123. void PLC_Run()
  124. {
  125. //发送mode
  126. pxLog(INIT,"[PLC] PLC thread run");
  127. sendMode();
  128. while(1)
  129. {
  130. //发送mode
  131. //if(setMode)
  132. {
  133. if(sendMode()!=0)
  134. {
  135. timerAdd(epollfd,0,3000,0,PLC_conn,NULL);
  136. return;
  137. }
  138. }
  139. if(PlcGetWheel()!=0 || PlcGetLegs()!=0)
  140. {
  141. timerAdd(epollfd,0,3000,0,PLC_conn,NULL);
  142. return;
  143. }
  144. if(postData.status.system==REMOTECONTROL)
  145. {
  146. if(PlcSetWheel()!=0)
  147. {
  148. timerAdd(epollfd,0,3000,0,PLC_conn,NULL);
  149. return;
  150. }
  151. }
  152. usleep(40000);
  153. }
  154. }
  155. void PLC_conn(PXTIMER_S *p)
  156. {
  157. pxLog(INIT,"[PLC] communication Init");
  158. postData.status.plc=0;
  159. postData.status.ros=0;
  160. serverfd=-1;
  161. if(config.PlcIP==NULL)
  162. {
  163. pxLog(ERROR,"[PLC] no pl ip");
  164. return;
  165. }
  166. int sock=0;
  167. struct sockaddr_in my_addr;
  168. if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
  169. {
  170. pxLog(ERROR,"[PLC] socket error:%d,retry in 3 sec",sock);
  171. timerAdd(epollfd,0,3000,0,PLC_conn,NULL);
  172. return;
  173. }
  174. int mw_optval = 1;
  175. setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&mw_optval,sizeof(mw_optval));
  176. my_addr.sin_family = AF_INET;
  177. my_addr.sin_port = htons(502);
  178. if (inet_pton(AF_INET, config.PlcIP, &(my_addr.sin_addr)) <= 0)
  179. {
  180. close(sock);
  181. pxLog(ERROR,"[PLC] plc ip parse error");
  182. return;
  183. }
  184. postData.status.ros=1;
  185. bzero(&(my_addr.sin_zero),8);
  186. // 设置连接超时时间
  187. struct timeval timeout={2,0};
  188. if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout)) < 0 || setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)))
  189. {
  190. close(sock);
  191. pxLog(ERROR,"[PLC] socket set timeout error,retry in 3 sec");
  192. timerAdd(epollfd,0,3000,0,PLC_conn,NULL);
  193. return;
  194. }
  195. // 连接到服务器
  196. if (connect(sock, (struct sockaddr*)&my_addr, sizeof(my_addr)) == -1)
  197. {
  198. close(sock);
  199. pxLog(ERROR,"[PLC] connect error");
  200. timerAdd(epollfd,0,3000,0,PLC_conn,NULL);
  201. return;
  202. }
  203. serverfd=sock;
  204. pthread_t thread;
  205. pthread_create(&thread, NULL, PLC_Run, NULL);
  206. }