簡體   English   中英

當TCP服務器正在等待數據時,如何使程序每10毫秒讀取一次傳感器

[英]How can I make the program read sensors every 10 milliseconds while the tcp server is waiting for data

我的程序需要每10毫秒讀取一次傳感器數據,然后通過TCP套接字將其發送到客戶端。 該程序是用C語言編寫的。 tcp服務器和傳感器讀取代碼位於同一c文件中。 服務器不會定期發送數據。 而是,它等待來自客戶端的數據請求(每50毫秒)。 服務器收到請求后,便將數據發送到客戶端。

我的問題是我需要每10毫秒讀取一次傳感器並將其保存在緩沖區中。 服務器收到請求后,將緩沖區中的傳感器數據發送給客戶端。 但是,當tcp服務器正在等待請求時,整個程序都停留在read函數上:

n = read(newsockfd,buffer,31)

然后,我看不到傳感器。

我試圖將線程用於傳感器讀取和tcp服務器。 它適用於讀取傳感器。 但是,如果我在tcp服務器線程中添加任何usleep()(例如5毫秒),則在將數據發送到客戶端之前需要5毫秒。

我的程序需要tcp服務器在收到請求后立即將數據發送到客戶端。 同時,每10毫秒讀取一次傳感器。 有人有更好的解決方案嗎?

tcp服務器和傳感器讀取的代碼如下:

#include <stdio.h>
#include <sys/types.h> 
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>

#define MAX_SIZE 256
#define SAMPLE_RATE 



void read_sensor(void) {
    printf("read sensors\n");
}


int main() {

     int sockfd, newsockfd, portno = 10000, clilen;
     char buffer[MAX_SIZE];
     struct sockaddr_in serv_addr, cli_addr;
     int n;
     int data;

     printf( "using port #%d\n", portno );

     sockfd = socket(AF_INET, SOCK_STREAM, 0);
     if (sockfd < 0) 
         printf("ERROR opening socket\n");
     bzero((char *) &serv_addr, sizeof(serv_addr));

     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons( portno );
     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) 
       error( const_cast<char *>( "ERROR on binding" ) );
     listen(sockfd,5);
     clilen = sizeof(cli_addr);

     while (1) {
        printf( "waiting for new client...\n" );
        if ( ( newsockfd = accept( sockfd, (struct sockaddr *) &cli_addr, (socklen_t*) &clilen) ) < 0 )
            printf("ERROR on accept");
        printf( "opened new communication with client\n" );

        clock_gettime(CLOCK_REALTIME, &gettime_now);
        start_time = gettime_now.tv_nsec;       //Get nS value
        while (1) {
             int n = 0;
             if ( (n = read(newsockfd,buffer,31) ) < 0 ) //read data
                printf("ERROR reading from socket");
             buffer[n] = '\0';

             if (n > 0)
                printf("%s\n", buffer);
             else {
                break;             
             }

             if ( (n = write( newsockfd, buffer, strlen(buffer) ) ) < 0 ) //send data
                printf( "ERROR writing to socket\n");
             buffer[n] = '\0';

        }
        close( newsockfd );
     }


     return 0; 
}

您可以使用selectpoll來等待數據到達,並且超時。

例如,如果您需要從現在起5毫秒內讀取傳感器,則可以調用poll或以5毫秒的超時時間進行select 如果套接字接收到數據,它將立即返回並告訴您。 否則,它將在5毫秒后返回,您將知道沒有數據。

poll示例:

struct pollfd pollfd = {.fd = socket_fd, .events = POLLIN};
int err = poll(&pollfd, 1, milliseconds_timeout);
// error check here; if err is negative then the error code is in errno
if (pollfd.revents & POLLIN) {
    // Socket received data
}
// Otherwise it didn't.
// Note that if data was received, it might not be time to read the sensors yet.
// You should keep poll()ing until it's the right time to read the sensors, using clock_gettime to check the time.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM