DSP

DSP C6455与上位机实现UDP数据传输

2019-07-13 15:07发布

本文主要实现DSP TMSC6455与上位机(平台:Qt)UDP传输数据。刚开始,尝试将NDK提供的Helloworld例程跑通。需要配置的文件均配置好,可以编译通过,但是将out文件load之后,运行不对,始终接收不到数据。后来我参考链接(http://e2e.ti.com/support/processors/f/791/t/365786)将UDP配置部分加入到自己原来的工程中,可以实现双向通讯了。也见网上有网友讲,Helloworld例程无法正常运行,是缺少TaskSleep(1)。没有实际验证。在此,记录下必要的配置步骤。 (一)CCS中相关配置 1、找到主函数main所在文件,例如helloWorld.c,将下面这句代码注释掉。 hHello = DaemonNew( SOCK_DGRAM, 0, 7, dtask_udp_hello, OS_TASKPRINORM, OS_TASKSTKNORM, 0, 1 ); 2、在工程中新建网络传输数据任务int sendUDPTsk() 如果工程中已经创建网络通讯任务,可以直接将下面代码更改到自己的函数中。 #include SOCKET socksend = INVALID_SOCKET; unsigned char RecevOK=0; static int SendNum = 0; static int RecevNum = 0; unsigned char send_buff[100] = {0};//设置缓存区大小 unsigned char *recev_buff; void sendUDPTsk() { struct sockaddr_in sinData, cliAddr, tmpAddr; int cliAddr_size = 0, err = 0; int actual_send_size = 0, send_buff_size = 0, i = 0, chain = 0; int actual_recv_size = 0,recv_buff_size = 0; int tmp; //变量初始化 RecevOK=0; //将接收的数据存储到指定地址上 recev_buff = (unsigned char *)0xe0000000; // Allocate the file environment for this task fdOpenSession( TaskSelf() ); //Initialize the socket //注意:UDP传输时,此处一定配置为SOCK_DGRAM socksend = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); if (socksend == INVALID_SOCKET) { printf(" socket(socksend) failed "); goto leave; } err = setsockopt( socksend, SOL_SOCKET, SO_REUSEADDR, send_buff, sizeof(send_buff) ); err = setsockopt( socksend, SOL_SOCKET, SO_REUSEPORT, send_buff, sizeof(send_buff) ); //Initialize socket address in structure,服务器端配置 bzero( &sinData, sizeof(struct sockaddr_in) ); sinData.sin_family = AF_INET; sinData.sin_len = sizeof( sinData ); sinData.sin_addr.s_addr = INADDR_ANY; sinData.sin_port = htons(5000); //Bind the socket err = bind (socksend, (struct sockaddr *)&sinData, sizeof(sinData)); if(err == 0) printf("Send-Bind operation successfully done "); else { printf("Send-Bind operation failed errno= %d ", fdError()); goto leave; } //客户端配置,为服务器端指定客户端的IP地址和端口号 bzero( &cliAddr, sizeof(struct sockaddr_in) ); cliAddr.sin_family = AF_INET; cliAddr.sin_len = sizeof( cliAddr ); cliAddr.sin_addr.s_addr = inet_addr("192.168.0.10"); //Target IP(PC IP address) cliAddr.sin_port = htons(5000); cliAddr_size = sizeof(struct sockaddr_in); tmp = sizeof( struct sockaddr_in ); //设置发送缓存区大小 send_buff_size = 100; //设置接收缓存区大小 recv_buff_size = 3000; //一直循环,等待接收或者发送数据信号 while(1) { memset(send_buff, '1', 100); /////////发送数据 if(RecevOK) { //发送数据 actual_send_size = sendto(socksend, send_buff, send_buff_size, 0, (struct sockaddr *)&cliAddr, cliAddr_size); if(actual_send_size < 0) printf(" Error Sending sendto() %d ", fdError()); else { printf(" %d.Send_Size = %d ", SendNum,actual_send_size); SendNum++; } RecevOK=0; } /////////接受数据 else { //接收数据 actual_recv_size = recvfrom(socksend, recev_buff, recv_buff_size, 0, (struct sockaddr *)&tmpAddr, &tmp); if (actual_recv_size<0) printf(" Error Receiving recvfrom() %d ", fdError()); else { RecevNum++; printf(" %d.actual_recv_size = %d ", RecevNum,actual_recv_size); RecevOK = 1; } } TaskSleep(1); } leave: printf("tskNetwork Fatal Error "); // This task is killed by the system - here, we block TaskBlock(TaskSelf()); } 3、Qt中配置 见上篇博客:《QT UDP数据传输》 链接:https://blog.csdn.net/weixin_38621214/article/details/86539570 参考链接:http://e2e.ti.com/support/processors/f/791/t/365786