2016年5月25日 星期三

UDP connection programming

Although I plan to use BBB as my embedded host, I want to transfer data from BBB to my host PC as well for one major reason; data verification and display. Simply because coding on desktop computer is easier to setup and rich ready to use library can be used to display my data. 
There are many ways to communicate with BBB such as TCP/ UDP, USB, UART, I chose UDP to be my main communication method for its simplicity and speed. UDP libraries are already in GCC, allI have to do is to implement the libraries.  

You just have to put the data pointer, number of bytes to be sent and destination IP address, the data will be sent from BBB to PC. 

size_t udpTXClient(const void *bData, int nSize, char *IP){
struct sockaddr_in serv_addr;
    int sockfd, i, servLen;
size_t nTxLen; 
size_t nWriteLen; 
    if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0))==-1)
        err("socket");
servLen = sizeof(serv_addr);
    bzero(&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(IP);
    serv_addr.sin_port = htons(1111);
//printf("UDP sender enabled\r\n") ;
if((nTxLen = sendto(sockfd, (void *)bData, nSize, 0, (struct sockaddr *) &serv_addr, servLen))==-1)
printf("UDP TX client fail\r\n");
    close(sockfd);
return nTxLen; 
}


沒有留言:

張貼留言