使用Linux RS232串行驱动程序进行缓冲读取

时间:2015-08-19 18:09:37

标签: linux serial-port driver

我试图在1200波特率下以10个字节读取串行端口一次。但是我观察到驱动程序在读取返回10个字节之前最初缓冲了一些数据(在我的情况下是96个字节)。由于这种缓冲,我无法实时处理数据......可能是什么原因以及需要在我的代码或内核串行驱动程序中更改的内容,每个都需要10个字节?传入数据流以1200波特连续。

我的代码:

#include <sys/wait.h>
#include<sys/signal.h>
#include<stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <termios.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <linux/serial.h>
#include <sys/ioctl.h>
#include <time.h>
#define BAUDRATE B1200
#define PACKET_SIZE 10
#define RXDEVICE_NAME "/dev/ttyM0"
#define CHAR_TIMEOUT 5 /*0.5sec timeout period*/
static void write2filerd(unsigned char *recvdata, time_t recvtime, int size,
        FILE *fp);
static void TimeToAsciird(time_t timeToConvert, char* buffer);

int main(void) {
    struct termios AP_term;
    int Inp_port = 0;
    time_t recvtime = 0;
    FILE *fptr;
    char textLine[256];
    unsigned char buf[100];
    unsigned int bytes = 0;
    int rd = 0;

    if ((fptr = fopen("/tmp/read.log", "w+")) < 0) {
        perror(" file open failed read ");
        exit(1);
    }

    if ((Inp_port = open(RXDEVICE_NAME, O_RDONLY | O_NOCTTY)) < 0) {
        perror("FAILED TO Open Port \n");
        exit(EXIT_FAILURE); /*exit*/
    }
    bzero(&AP_term, sizeof(AP_term));
    tcsetattr(Inp_port, TCSANOW, &AP_term);

    AP_term.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;
    AP_term.c_iflag = IGNPAR;
    AP_term.c_lflag = 0;
    AP_term.c_oflag = 0;
    AP_term.c_cc[VMIN] = 10;/*10 BYTES LENGTH*/
    AP_term.c_cc[VTIME] = CHAR_TIMEOUT;

    tcflush(Inp_port, TCIOFLUSH);
    tcsetattr(Inp_port, TCSANOW, &AP_term);
    bzero(&buf, sizeof(buf));

    tcflush(Inp_port, TCIOFLUSH);
    time(&recvtime);
    fprintf(fptr, "Start at ");
    fflush(fptr);
    TimeToAsciird(recvtime, textLine);
    strcat(textLine, "\n");
    fwrite(textLine, sizeof(char), strlen(textLine), fptr);
    fflush(fptr);

    ioctl(Inp_port, TIOCINQ, &bytes);
    fprintf(fptr, "Before bytes avail %d \n", bytes);
    fflush(fptr);
    while (1) {
        rd = read(Inp_port, buf, PACKET_SIZE);
        if (rd <= 0) {
            fprintf(stderr, "Read failure with %d: %s ", rd, strerror(errno));
            break;
        }
        ioctl(Inp_port, TIOCINQ, &bytes);
        time(&recvtime);
        write2filerd(buf, recvtime, rd, fptr);
        fprintf(fptr, "::bytes remaining %d \n", bytes);
        fflush(fptr);

    }
    time(&recvtime);
    fprintf(fptr, "End at ");
    fflush(fptr);
    TimeToAsciird(recvtime, textLine);
    strcat(textLine, "\n");
    fwrite(textLine, sizeof(char), strlen(textLine), fptr);
    fflush(fptr);
    fprintf(fptr, "END PROGRAM\n");
    fclose(fptr);
    close(Inp_port);
    exit(EXIT_SUCCESS);
    return 0;
}

static void write2filerd(unsigned char *recvdata, time_t recvtime, int size,
        FILE *fp) {

    unsigned char *buf = recvdata;

    char textLine[256];
    char *msg = malloc(80);
    char s[5];
    int i;

    *msg = '\0';

    for (i = 0; i < size; i++) {
        sprintf(s, "%02X ", buf[i]);
        strcat(msg, s);
    }
    TimeToAsciird(recvtime, textLine);
    strcat(textLine, " ");
    strcat(textLine, msg);
    strcat(textLine, " ");
    fwrite(textLine, sizeof(char), strlen(textLine), fp);
    fflush(fp);
    free(msg);

}
static void TimeToAsciird(time_t timeToConvert, char* buffer) {
    struct tm* timeStruct;

    timeStruct = localtime(&timeToConvert);

    sprintf(buffer, "%.2d:%.2d:%.2d %.2d/%.2d/%.2d", timeStruct->tm_hour,
            timeStruct->tm_min, timeStruct->tm_sec, timeStruct->tm_mday,
            timeStruct->tm_mon + 1, timeStruct->tm_year % 100);
}

上述程序的输出:

Start at 21:40:59 14/08/15 
Before bytes avail 0 
21:41:03 14/08/15 06 07 00 00 12 FD A5 0B 67 9C ::bytes remaining 86 
21:41:03 14/08/15 06 07 00 01 12 FD A5 0B 67 9C ::bytes remaining 76 
21:41:03 14/08/15 06 07 00 02 12 FD A5 0B 67 9C ::bytes remaining 66 
21:41:03 14/08/15 06 07 00 03 12 FD A5 0B 67 9C ::bytes remaining 56 
21:41:03 14/08/15 06 07 00 04 12 FD A5 0B 67 9C ::bytes remaining 46 
21:41:03 14/08/15 06 07 00 05 12 FD A5 0B 67 9C ::bytes remaining 36

0 个答案:

没有答案