通过UDP发送数据块

时间:2019-11-03 12:10:32

标签: c matlab udp buffer

我正在尝试通过UDP发送数据块(每个包现在为1024字节),以便我可以使用DatagramReceivedFcn在Matlab中实时(或至少接近实时)接收数据。

因此,我编写了一个C代码,该代码在Linux机器上执行以获取模拟输入值(出于测试目的,我使用整数),并试图使该代码在缓冲区大小达到最大1024字节时发送包。 / p>

我还需要添加一个“!”作为Matlab的终结符,位于缓冲区末尾。

但是似乎缓冲区的大小并不总是精确地为1024(有时为1022,有时会更大一点),这在Matlab中接收数据时给我带来了问题(奇怪的符号而不是值)。

您能否给我一个提示,以更改代码中的内容?我对编程特别是C语言中的套接字编程非常陌生。

谢谢!

#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <string.h> 
#include <sys/types.h> 
#include <sys/socket.h> 
#include <arpa/inet.h> 
#include <netinet/in.h> 
#include <time.h>
#include "/RedPitaya/api/include/redpitaya/rp.h"   //Header for RP API

#define MAXLINE 1024 
#define MAXBUFFSIZE 1024                //Definiert maximale Buffergroesse in Bytes - 8192 in MatLab Maximum

int main() { 
    int sockfd=0;
    char recv_buffer[MAXLINE];
    char send_buffer[MAXBUFFSIZE];          //Array als buffer
    struct sockaddr_in servaddr, cliaddr; 
    //float voltage;

    socklen_t len;

    // Initialization of RP-API
    if (rp_Init() != RP_OK) 
    {
        fprintf(stderr, "Red Pitaya API init failed!\n");
        return EXIT_FAILURE;
    }


    // Creating socket file descriptor 
    if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) 
    { 
        perror("socket creation failed"); 
        exit(EXIT_FAILURE); 
    } 

    memset(&servaddr, 0, sizeof(servaddr)); 
    memset(&cliaddr, 0, sizeof(cliaddr)); 

    // Filling server information 
    servaddr.sin_family    = AF_INET; // IPv4 
    servaddr.sin_addr.s_addr = INADDR_ANY; 
    servaddr.sin_port = htons(8080); 

    // Bind the socket with the server address 
    if ( bind(sockfd, (const struct sockaddr *)&servaddr,  
            sizeof(servaddr)) < 0 ) 
    { 
        perror("bind failed"); 
        exit(EXIT_FAILURE); 
    } 

    len = sizeof(cliaddr);
    int n;

    n = recvfrom(sockfd, (char *)recv_buffer, MAXLINE,  
                MSG_WAITALL, ( struct sockaddr *) &cliaddr, 
                &len); 
    recv_buffer[n] = '\0'; 
    printf("Client : %s", recv_buffer); 


int pck_counter = 0;
int i = 0;
int buffsize = 0;
int bytes_per_value = 0; 
int byteswritten;

while(pck_counter<10)               //counter ist Anzahl der Pakete - spaeter while(1)
{


    if((buffsize + bytes_per_value + strlen("!")) < MAXBUFFSIZE)
    {

        while((buffsize + bytes_per_value + strlen("!")) < MAXBUFFSIZE)     //https://stackoverflow.com/questions/37715229/c-sending-float-over-udp-results-in-random-symbols                   
        {
            //usleep(1);                                        //sleep 1ms

            //Get AI1-Value 
            //rp_AIpinGetValue(1, &voltage);                //Read Voltage of Pin1

            if(i==0)
            {
                bytes_per_value = sprintf(send_buffer,"%i %i;",pck_counter, i); //Konvertiert float zu string und schreibt in buffer - written = bytes die in Buffer geschrieben wurden

            }
            else
            {

                byteswritten = sprintf(send_buffer+strlen(send_buffer),"%i %i;",pck_counter, i); //Erhoeht Buffergroesse und fuegt neuen Wert hinzu //Funktioniert nicht wie es soll
                buffsize = (byteswritten*i)+bytes_per_value;
            }

            //printf("buffer: %s ", send_buffer); //printing value
            //printf("buffsize: %d ", buffsize);

            i++;

        }
    }

    else
    {

        // Add Terminator "!" 
        byteswritten = sprintf(send_buffer+strlen(send_buffer),"!");


        // Packet finished- Buffer full
        printf("Buffer %i full (%i) \n", pck_counter, buffsize);
        printf("buffer: %s ", send_buffer); //printing value

        if(sendto(sockfd, send_buffer, buffsize, 0,                 //Send data packet
                 (const struct sockaddr *) &cliaddr, 
                  len)<0)
        {
            perror("Sending failed");
        }
        else
        {
        //  printf("Data transmission for packet %i complete\n", pck_counter);
        }


        //Buffer leeren fuer naechsten Durchlauf
        fflush(stdin);
        buffsize = 0;
        i = 0;

        pck_counter++;

    }

}
    printf("All data transfered");
    return 0; 
} 


0 个答案:

没有答案