linux串口控制台调试以及应用程序编程

4 篇文章 0 订阅

linux arm板(orange pi lite) 下的4个串口,ttyS0作为控制台,以下是对其他三个串口的说明

 stty查看串口参数

stty -F /dev/ttyS2 -a

 stty设置串口参数

stty -F /dev/ttyS2 ispeed 115200 ospeed 115200 cs8

 回传接受到的数据

cat /dev/ttyS2

echo "hello orange pi lite" > /dev/ttyS2

 以上内容只能在控制台或者shell脚本中使用,应用程序还是得写代码

#include <stdio.h>

//文件操作函数头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>

#define     SIZE    (1024 * 1)
#define ZIGBEE_DEV   "/dev/ttyS2"
int zigbeefd;


int speed_arr[] = {B115200,B38400,B19200,B9600,B4800,B2400,B1200,B300};
int bps_arr[]  = {115200,38400,19200,9600,4800,2400,1200,300};

void setBps(int fd,int bps)
{
    int i;
    int status;
    struct termios opt;
    tcgetattr(fd,&opt);
    for(i=0;i<sizeof(speed_arr)/sizeof(int);i++)
    {
        if(bps==bps_arr[i])
   	{
            tcflush(fd,TCIOFLUSH);
            cfsetispeed(&opt,speed_arr[i]);
            cfsetospeed(&opt,speed_arr[i]);
            status = tcsetattr(fd,TCSANOW,&opt);
            if(status != 0)
            {
                perror("Set Serial Bps Error");
            }
            return;
        }
        tcflush(fd,TCIOFLUSH);
    }
}

/**
*@brief   设置串口数据位,停止位和效验位
*@param  fd     类型  int  打开的串口文件句柄*
*@param  databits 类型  int 数据位   取值 为 7 或者8*
*@param  stopbits 类型  int 停止位   取值为 1 或者2*
*@param  parity  类型  int  效验类型 取值为N,E,O,,S
*/
int setOption(int fd,int databits,int stopbits,int parity)
{
    struct termios opt;
    if(tcgetattr(fd,&opt) != 0)
    {
        perror("Get Serial Options Error");
        return(-1);
    }
    opt.c_cflag &= ~CSIZE;
    //set data bit length
    switch (databits) /*设置数据位数*/
    {
        case 7:
            opt.c_cflag |= CS7;
            break;
  	case 8:
            opt.c_cflag |= CS8;
            break;
	default:
            fprintf(stderr,"Unsupported data size!\n");
            return (-1);
    }
    //set parity bit mode
    switch (parity)
    {
        case 'n':
        case 'N':
            opt.c_cflag &= ~PARENB;		/* Clear parity enable */
            opt.c_iflag &= ~INPCK;			/* Disable parity checking */
            break;
        case 'o':
        case 'O':
            opt.c_cflag |= (PARODD|PARENB);	/* 设置为奇效验*/ 
            opt.c_iflag |= INPCK;			/* Enable parity checking */
            break;
        case 'e':
        case 'E':
            opt.c_cflag |= PARENB;     		/* Enable parity */
            opt.c_cflag &= ~PARODD;   		/* 转换为偶效验*/  
            opt.c_iflag |= INPCK;      		/* Enable parity checking */ break; case 's': case 'S':  				/*as no parity*/
            opt.c_cflag &= ~PARENB;
            opt.c_cflag &= ~CSTOPB;
            break;
	default:
            fprintf(stderr,"Unsupported parity\n");
            return (-1);
    }
    //set stop bit length
    switch (stopbits)
    {
        case 1:
            opt.c_cflag &= ~CSTOPB;
            break;
	case 2:
            opt.c_cflag |= CSTOPB;
            break;
        default:
            fprintf(stderr,"Unsupported stop bits\n");
        return (-1);
    }
    //set input parity option
    //if (parity != 'n')
    //{
        //opt.c_iflag |= INPCK;
    //}

    opt.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);
    opt.c_oflag &= ~OPOST;

    //opt.c_cc[VTIME] = 150; // 15 seconds
    opt.c_cc[VMIN] = 0;

    tcflush(fd,TCIFLUSH);			//Update the options and do it NOW
    if (tcsetattr(fd,TCSANOW,&opt) != 0)
    {
        perror("Set Serial Options Error");
        return (-1);
    }
    return (0);
}

int main(int argv,char *argc[])
{
	int ret;
	int len;
	char rx_buffer[SIZE]; //接收网络数据缓冲区1K  
	
	zigbeefd = open(ZIGBEE_DEV, O_RDWR);
	if (zigbeefd < 0) {
		printf("open zigbee device error\n");
		return -1;
	}
	setBps(zigbeefd,115200);
	while (1)
	{
		int s;
		fd_set rfds;
		struct timeval tv = {
			.tv_sec = 0,
			.tv_usec = 20,
		};
		FD_ZERO(&rfds);
		FD_SET(zigbeefd, &rfds);

		s = select(zigbeefd + 1, &rfds, NULL, NULL, &tv);			
		if (s < 0)
		{
			printf("Select failed!\r\n");
			break;
		}
		else if (s == 0)
		{
			//printf("Timeout has been reached and nothing has been received\r\n");
		}
		else
		{
			if (FD_ISSET(zigbeefd, &rfds))
			{
				//len=read(zigbeefd, rx_buffer, sizeof(rx_buffer) - 1, 0);
				len=read(zigbeefd, rx_buffer, SIZE);
				if (len)
				{
					printf("%s", rx_buffer);
					rx_buffer[len] = 0; // Null-terminate whatever we received and treat like a string
					//Receive_Process("tcp",(char*)rx_buffer,len);
					//write(zigbeefd, rx_buffer, len);
				}
				else
				{
				   //printf("read error\r\n");
				   break;
				}
			}
			else
			{
				printf(" No uart has been set in select()");
				break;
			}
		}			
		usleep(2000);
	}	
	 
}




 收到什么就转发什么,

 能力有限,水平有限,如有错误,恳请留言批评指正

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值