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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_38621214/article/details/86565726

本文主要实现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 <netmain.h>
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 \n");
	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\n");
    else
    {
	printf("Send-Bind operation failed errno= %d\n", 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 \n", fdError());
	else
	{
	    printf(" %d.Send_Size = %d \n ", 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 \n", fdError());
	else 
	{
	    RecevNum++;
	    printf(" %d.actual_recv_size = %d \n ", RecevNum,actual_recv_size);
	    RecevOK = 1;
	}
    }
	TaskSleep(1);
}

leave:
    printf("tskNetwork Fatal Error\n");

    // 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

展开阅读全文

udp实现数据传输的问题

09-09

想用Java实现简单的UDP数据传输 但是服务器端老报错 Address already in use: Cannot bindrn下面是代码 帮忙看下 谢谢rn/******************客户端**********************************/rnpublic static void main(String[] args) throws java.io.IOExceptionrn rn rn String localhost ="192.168.108.200";rn rn BufferedInputStream inFromUser = rn new BufferedInputStream(new FileInputStream("c:\\test.txt"));rn rn DatagramSocket clientSocket = new DatagramSocket();rn InetAddress IPAddress = InetAddress.getByName(localhost);rn rn byte[] sendData = new byte[1024];rn byte[] receiveData = new byte[1024];rn //read data from inFromUser to sendDatarn while(inFromUser.read(sendData)!=-1)rn rn DatagramPacket sendPacket = rn new DatagramPacket(sendData, sendData.length, IPAddress, 8000);rn clientSocket.send(sendPacket);rnrn rn inFromUser.close();rn clientSocket.close();rn rnrnrnrn/***********************服务器端********************/rnrn public static void main(String[] args) throws Exception rn rn BufferedOutputStream bufferedOutputStream = rn new BufferedOutputStream(new FileOutputStream("d:\\test.txt"));rn DatagramSocket serverSocket = new DatagramSocket(8000);rn byte[] receiveData = new byte[1024];rn byte[] sendData =new byte[1024];rn rn DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);rn serverSocket.receive(receivePacket);rn rn while(true)//循环的条件不知道该怎么定就用true了rn rn bufferedOutputStream.write(receivePacket.getData());rn rn rn 论坛

UDP数据传输时的问题

07-31

我使用UDP在客户端与服务器端进行交互:rn/******************************client side*****************************/rn byte[] buf =new byte[256];rn int port =0;rn String fileName =args[2];//从main中带入的第三个参数,用于打开服务器端文件。rn InetAddress address =null;rn DatagramSocket clientSocket=null;rn DatagramPacket clientPacket=null;rn //convert host from name to ip addressrn address=InetAddress.getByName(args[0]);rn //Set up a socketrn clientSocket=new DatagramSocket();rn port=Integer.parseInt(args[1]);rn //put filename into bufrn buf=fileName.getBytes();rn //send requestrn clientPacket=new DatagramPacket(buf,buf.length,address,port);rn clientSocket.send(clientPacket);rn/*******************server side**********************************/rn int port =0;rn byte[] buf =new byte[256];rn String fileName =null;rn InetAddress address =null;rn DatagramSocket serverSocket=null;rn DatagramPacket serverPacket=null;rn //set up socketrn port=Integer.parseInt(args[0]);rn serverSocket=new DatagramSocket(port);rn //listeningrn serverPacket=new DatagramPacket(buf, buf.length);rn serverSocket.receive(serverPacket);rn //get file namern int c=-1,i=0;rn while ((c=buf[i])!=0)rn fileName=fileName+(char)c;rn i++;rn rn/****************************end ***********************/rn//删除了对exception的处理。rn rn我现在的问题是,这样得到的文件名总在前面有个null,rn如客户输入student.txt, 服务器端得到的字符串是nullstudent.txt.rn我找不出null是哪来的 论坛

用ACE实现UDP数据传输的问题

03-28

通过跟踪发现发送端已经发送成功:udp.send(databuf,strlen(databuf),remote1) != -1通过rn可是服务器端一直接收不到数据 这是我学习ACE的第一个例子 请大家帮帮忙rn贴出程序:rnrn发送端:rnrn#include "stdafx.h"rnrnrn#define sizeData 19rnrn#include "ace/SOCK_Acceptor.h"rn#include "ace/Log_Msg.h"rn#include "ace/SOCK_Stream.h"rn#include "ace/OS.h"rn#include "ace/SOCK_Dgram.h"rn#include rnusing namespace std;rnint _tmain (int argc, char *argv[])rnrn char * databuf = "hello world!";rn ACE_INET_Addr remote1 ;rn ACE_SOCK_Dgram udp ;rnrnrn //if(udp.open(remote1)==-1)rn //rn // ACE_OS::printf("Error opening.\n") ;rn // return 1 ;rn //rnrn if(remote1.set(4200, "127.0.0.1") == -1)rn rn ACE_OS::printf("Error setting remote address.\n") ;rn return 1;rn rnrn ACE_OS::printf("Remote address successfully set.\n") ;rnrn ACE_OS::printf("Sending data...\n") ;rn //int a;rnrn if(0 != udp.open(remote1))rn ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), "4200"));rnrnrn while(udp.send(databuf,strlen(databuf),remote1) != -1)rn rn //a = udp.send(databuf,sizeData,remote)rn Sleep(3000);rn rnrn //udp.close() ;rn return 0;rnrnrn接收端 :rn// testSocket.cpp : 定义控制台应用程序的入口点。rn//rnrn#include "stdafx.h"rnrnrn#define sizeData 19rnrn#include "ace/SOCK_Acceptor.h"rn#include "ace/SOCK_Stream.h"rn#include "ace/OS.h"rn#include "ace/SOCK_Dgram.h"rn#include rnusing namespace std;rnint _tmain (int argc, char *argv[])rnrn char * databuf=NULL;rn databuf =(char*)ACE_OS::malloc(20*sizeof(char));rn int byteCount;rn ACE_INET_Addr remote;rn ACE_SOCK_Dgram udp ;rnrn if(remote.set(4200)==-1)rn rn ACE_OS::printf("Error setting remote address.\n") ;rn return 1 ;rn rnrn if(udp.open(remote)==-1)rn rn ACE_OS::printf("Error opening ...\n") ;rn return 1 ;rn rnrn ACE_OS::printf("listening...") ;rn while((byteCount = udp.recv(databuf,strlen(databuf)+1,remote)) != -1)rn rn //udp.recv(databuf,sizeData,remote);rn //databuf[byteCount] = 0; rn ACE_OS::printf(databuf);rn Sleep(1000);rn rnrn udp.close();rn return 0;rn 论坛

没有更多推荐了,返回首页