Linux监控串口数据并写入文件-C语言demo

程序是基于Linux操作系统,监控串口是否有数据,如果有数据就写入到文件系统中。Demo涉及串口初始化,poll函数监控,以及文件读写操作。

#include <termios.h>
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>

#define MAX_MSG_SIZE 1024*1024+1
#define NEW_FILE "/mnt/userdata/uart_data_%s"

char buf[MAX_MSG_SIZE + 1]={0};
FILE *fp = NULL;

void exit_handle(int signo) 
{
	int fd = -1;
    printf("kill app!!!\n");
	fflush(fp);
    fd = fileno(fp);
    fsync(fd);
    fclose(fp);
    exit(0);
}


void main(char argc, char *argv[])
{
	int uart_fd                   = -1;
	int num_read                  = 0;
	int ret                       = 0;
	struct termios option;
	struct pollfd pollinfo        = {0};
    char a[100];

	signal(SIGINT, exit_handle);
	snprintf(a, 90, NEW_FILE, argv[1]);

    fp = fopen(a, "w+");
	if(fp == NULL)
	{
	  printf("new file error\n");
	  return;
	}
	
	
	pollinfo.events = POLLIN | POLLPRI | POLLERR | POLLHUP;
	
	uart_fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
	
	if (uart_fd < 0)
	{
	  printf("open uart_fd file error\n");
	  return;
	}
	
	pollinfo.fd = uart_fd;

	if (tcgetattr(uart_fd, &option) < 0)
	{
	  printf("Attempt to get attr error\n");
	}
	else
	{
	  printf("get attr ok\n");

	  option.c_cc[VTIME] = 0;    //set timeout
	  option.c_cc[VMIN] = 1;     // define the minimum bytes data to be readed

	  option.c_cflag &= ~CSIZE;
	  option.c_cflag |= (CS8 | CLOCAL | CREAD);
	  option.c_cflag &= ~PARENB;
	  option.c_cflag &= ~CSTOPB;
	  option.c_iflag = IGNPAR;
	  option.c_cflag &= ~PARODD;
	  option.c_iflag &= ~INPCK; /* Enable parity checking */
	  option.c_oflag = 0;
	  option.c_lflag = 0;

	  option.c_cflag &= ~(IXOFF | IXON | IXANY);
	  option.c_cflag &= ~CRTSCTS;

	  cfsetospeed(&option, B460800);
	  cfsetispeed(&option, B460800);
	  
	  tcflush(uart_fd, TCIOFLUSH);
	  if (tcsetattr(uart_fd, TCSANOW, &option) < 0)
	  {
		  printf("Attempt to set attr error\n");
	  }
	}
	
	while(1)
	{
		pollinfo.revents = 0;
		ret = poll(&pollinfo, 1, -1);
		
		if (pollinfo.revents & POLLIN)
		{
		  num_read = read(uart_fd, buf, MAX_MSG_SIZE);
		  printf("read uart_fd num %d\n", num_read);

		  if (num_read > 0)
		  {
			fwrite(buf, num_read, 1, fp);
		  }
		}
	}
}
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
串口1中断收发是指在单片机系统中,通过配置和使用串口1的中断功能,实现串口数据的接收和发送。 首先,我们需要在程序中配置串口1相关的寄存器,以设置数据位数、波特率、停止位等参数。然后,启用串口1的中断功能,使得当接收或发送数据时,可以触发中断并执行相应的中断服务程序。 在数据的接收方面,当有数据通过串口1发送过来时,中断会被触发,程序会跳转到中断服务程序中。在中断服务程序中,我们可以读取串口1接收缓冲区中的数据,并进行后续的处理。例如,可以将数据保存到指定的变量中,或是进行数据的解析和处理等操作。 在数据的发送方面,当我们需要发送数据时,可以调用相应的发送函数。发送函数会将数据写入串口1的发送缓冲区,并启动发送操作。当数据发送完毕后,中断也会被触发,程序会跳转到中断服务程序中。在中断服务程序中,我们可以检查发送状态寄存器,判断发送是否完成,以便进行后续的操作。 通过使用串口1中断收发,我们可以实现较高效的数据通信。相比于轮询方式,中断方式能够使处理器在等待数据到来或发送完毕时进行其他任务,提高处理器的利用率和系统的响应速度。 总结起来,串口1中断收发是通过配置和使用串口1中断功能,实现串口数据的接收和发送。在中断服务程序中,我们可以读取接收缓冲区的数据,并进行后续的处理;同时,在发送数据时,可以检查发送状态寄存器以了解发送状态。通过使用中断方式,可以实现较高效的串口数据通信。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值