Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.78 KB | None | 0 0
  1. component stmbl "udp stuff";
  2.  
  3. pin in float pos_cmd0;
  4. pin in float pos_cmd1;
  5. pin in float pos_cmd2;
  6.  
  7. pin out float pos_fb0;
  8. pin out float pos_fb1;
  9. pin out float pos_fb2;
  10.  
  11. variable int sockfd;
  12. function calc;
  13. option extra_setup yes;
  14. license "GPL";
  15. ;;
  16.  
  17. #include <arpa/inet.h>
  18. #include <ifaddrs.h>
  19.  
  20. EXTRA_SETUP(){
  21.     rtapi_print("hallo\n");
  22.  
  23.     int ret;
  24.  
  25.     sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
  26.     if (sockfd < 0) {
  27.         rtapi_print("ERROR: can't open socket: %s\n", strerror(errno));
  28.         return -errno;
  29.     }
  30.  
  31.     struct sockaddr_in server_addr;
  32.     struct sockaddr_in local_addr;
  33.  
  34.     server_addr.sin_family = AF_INET;
  35.     server_addr.sin_port = htons(27181);
  36.     server_addr.sin_addr.s_addr = inet_addr("192.168.1.150");
  37.  
  38.     local_addr.sin_family      = AF_INET;
  39.     local_addr.sin_addr.s_addr = INADDR_ANY;
  40.  
  41.     ret = connect(sockfd, (struct sockaddr *) &server_addr, sizeof(struct sockaddr_in));
  42.     if (ret < 0) {
  43.         rtapi_print("ERROR: can't connect: %s\n", strerror(errno));
  44.         return -errno;
  45.     }
  46.  
  47.    
  48.  
  49.     struct timeval timeout;
  50.     timeout.tv_sec = 0;
  51.     timeout.tv_usec = 10;
  52.  
  53.     ret = setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
  54.     if (ret < 0) {
  55.         rtapi_print("ERROR: can't set socket option: %s\n", strerror(errno));
  56.         return -errno;
  57.     }
  58.  
  59.     timeout.tv_usec = 10;
  60.     setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, sizeof(timeout));
  61.     if (ret < 0) {
  62.         rtapi_print("ERROR: can't set socket option: %s\n", strerror(errno));
  63.         return -errno;
  64.     }
  65.  
  66.  
  67.     return 0;
  68. }
  69.  
  70. FUNCTION(calc){
  71.     pos_fb0 = pos_cmd0;
  72.     pos_fb1 = pos_cmd1;
  73.     pos_fb2 = pos_cmd2;
  74.     uint8_t buf[16];
  75.  
  76.     buf[0] = 0x01;
  77.     buf[1] = 0x42;
  78.     buf[2] = 0x00;
  79.     buf[3] = 0x01;
  80.  
  81.     int ret = send(sockfd,buf,4,0);
  82.     if (ret < 0) {
  83.         rtapi_print("send fail %s\n", strerror(errno));
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement