Linux 下串口编程实例

Linux 下串口编程实例

如下代码实现了uart配置,同时封装了初始化函数、串口读、串口写以便移植,直接看代码吧

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>

#define  TRUE    0
#define  FALSE  -1


unsigned char CharToHex(unsigned char bHex)
{
	if((bHex>=0)&&(bHex<=9))
	{
		bHex += 0x30;
	}
	else if((bHex>=10)&&(bHex<=15))//Capital
	{
		bHex += 0x37;
	}
	else 
	{
		bHex = 0xff;
	}
	return bHex;
}
 

unsigned char HexToChar(unsigned char bChar)
{
	if((bChar>=0x30)&&(bChar<=0x39))
	{
		bChar -= 0x30;
	}
	else if((bChar>=0x41)&&(bChar<=0x46)) // Capital
	{
		bChar -= 0x37;
	}
	else if((bChar>=0x61)&&(bChar<=0x66)) //littlecase
	{
		bChar -= 0x57;
	}
	else 
	{
		bChar = 0xff;
	}
	return bChar;
}
 

/**
*@brief  设置串口通信速率
*@param  fd     类型 int  打开串口的文件句柄
*@param  speed  类型 int  串口速度
*@return  void*/

int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300,
	    B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400,  19200,  9600,  4800,  2400,  1200,  300,
	    38400,  19200,  9600, 4800, 2400, 1200,  300, };
void set_speed(int fd, int speed)
{
	int i;
	int status;
	struct termios Opt;

	tcgetattr(fd, &Opt);
	for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) {
		if (speed == name_arr[i])	{
			tcflush(fd, TCIOFLUSH);
			cfsetispeed(&Opt, speed_arr[i]);
			cfsetospeed(&Opt, speed_arr[i]);
			status = tcsetattr(fd, TCSANOW, &Opt);
			if (status != 0)
				perror("tcsetattr fd1");
			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 set_parity(int fd,int databits,int stopbits,int parity)
{
	struct termios options;
	if ( tcgetattr(fd, &options) != 0)
	{
		perror("SetupSerial 1");
		return FALSE;
	}
	options.c_cflag &= ~CSIZE;
	switch (databits) {/*设置数据位数*/
	case 7:
  		options.c_cflag |= CS7;
  		break;
  	case 8:
		options.c_cflag |= CS8;
		break;
	default:
		fprintf(stderr,"Unsupported data size\n");
		return FALSE;
	}

	switch (parity) {
  	case 'n':
	case 'N':
		options.c_cflag &= ~PARENB;   /* Clear parity enable */
		options.c_iflag &= ~INPCK;     /* Enable parity checking */
		break;
	case 'o':
	case 'O':
		options.c_cflag |= (PARODD | PARENB);  /* 设置为奇效验*/ 
		options.c_iflag |= INPCK;             /* Disnable parity checking */
		break;
	case 'e':
	case 'E':
		options.c_cflag |= PARENB;     /* Enable parity */
		options.c_cflag &= ~PARODD;   /* 转换为偶效验*/  
		options.c_iflag |= INPCK;       /* Disnable parity checking */
		break;
	case 'S':
	case 's':  /*as no parity*/
		options.c_cflag &= ~PARENB;
		options.c_cflag &= ~CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported parity\n");
		return FALSE;
	}

	/* 设置停止位*/   
	switch (stopbits) {
  	case 1:
  		options.c_cflag &= ~CSTOPB;
		break;
	case 2:
		options.c_cflag |= CSTOPB;
		break;
	default:
		fprintf(stderr,"Unsupported stop bits\n");
		return FALSE;
	}

	/* Set input parity option */
	if (parity != 'n')
  		options.c_iflag |= INPCK;
	options.c_cc[VTIME] = 150; // 15 seconds
	options.c_cc[VMIN] = 0;
	options.c_cflag &= ~CRTSCTS; //不使用流控制

    options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
                   | INLCR | IGNCR | ICRNL | IXON);
    options.c_oflag &= ~OPOST;
    options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    options.c_cflag &= ~(CSIZE | PARENB);
    options.c_cflag |= CS8;


	tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
	if (tcsetattr(fd,TCSANOW,&options) != 0) {
		perror("SetupSerial 3");
		return FALSE;
	}

	return TRUE;
}


/**
*@breif 打开串口
*/
int open_dev(char *Dev)
{
int	fd = open( Dev, O_RDWR | O_NOCTTY );         //| O_NOCTTY | O_NDELAY
	if (-1 == fd)
		{ /*设置数据位数*/
			perror("Can't Open Serial Port");
			return -1;
		}
	else
	return fd;

}

int  fd;
char *dev = "/dev/ttyS4";
int  speed = 19200;

int ttySX_read(int fd, void *buf, int count)
{
	int ret = read(fd, buf, count);
	if(ret < 0){
		printf("read error!\n");
		return -1;
	}

	return ret;
}


int ttySX_write(int fd, const void *buf, int count)
{
	int ret = write(fd, buf, count);
	if(ret < 0){
		printf("write error!\n");
		return -1;
	}

	return ret;
}


int ttySX_init(int *fd, char *dev, int speed)
{
	if ( (*fd = open_dev(dev)) > 0) {
		set_speed(*fd, speed);
		printf("open %s successfully!\n", dev);
    } else {
		printf("Can't Open Serial Port!\n");
		exit(0);
	}

	if ( set_parity(*fd, 8, 1, 'N') == FALSE) {
		printf("Set Parity Error\n");
		exit(1);
	}
}

int uart_rx(char *buf)
{
	printf("---->uart_rx\n");
}

void uart_tx(char *cmd)
{
	printf("---->uart_tx:0x%02x\n", *cmd);
	ttySX_write(fd, cmd, 1);
}

int main(int argc, char **argv)
{
	int lread, lwrite;
	//int fd;
	char *dev = "/dev/ttyS4";
	int  speed = 19200;
	char buff[8] = {0};
	char recvbuff[16] = {0};
	int count = 0;
	unsigned char cmd[11] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b};
	unsigned char powerval = 0xFA;

	ttySX_init(&fd, dev, speed);
	//ttySX_write(fd, "Uart initiallize done\n", 32);

	int arg = atoi(argv[1]);
	printf("arg:%d\n",arg);
	if(arg < 12) {
		uart_tx(cmd + arg - 1);
	} else if(arg > 150 && arg <= 250){
		powerval = 0x96 + arg - 150;
		uart_tx(&powerval);
	}
#if 1
	while(1) {

   		
	  	while((lread = read(fd,buff,8)) > 0) {
  		    printf("------->while(...)\n");
      		printf("Len %d\n",lread);
      		buff[lread+1]='\0';
      		printf("%s\n",buff);
      		for(int i=0; i< lread;i++)
      			printf("0x%02x ", buff[i]);
      		printf("\n");
   		}
   	 }

#endif	

   	return 0;
}


 

注意事项:

1. 确认接收设备与发送设备波特率、数据位长度、停止位数、有无校验位、有无流量控制设置,主从设备上述参数是否统一;

2. 主控收发?从设备收发?

单独将设备rx,tx接到串口工具上,在windows上通过调试助手与设备调试。特别注意接线,过程可以用示波器或者逻辑分析仪确认是否有数据,以及数据对不对;

3. 确认数据收发格式,是hex还是text?

数据以字节流收发,但是不管什么方式,数据都是16进制。

4. 示波器上有数据发出,但是没有收到?

可能是线接错了,rx对tx,tx对rx。如果接线比较乱,建议把梅根线贴上标签,标明tx、rx

5.确认串口属性设置,数据收发是否是raw原始数据(不经过tty系统处理),实现方法:

    options.c_cflag &= ~CRTSCTS; //不使用流控制
    options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
                   | INLCR | IGNCR | ICRNL | IXON);
    options.c_oflag &= ~OPOST;
    options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
    options.c_cflag &= ~(CSIZE | PARENB);
    options.c_cflag |= CS8;

 

 

参考链接:

https://www.ibm.com/developerworks/cn/linux/l-serials/

https://blog.csdn.net/lhl_blog/article/details/82254056

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值