linux 串口测试程序

头文件:

#ifndef _UART_H_


#define _UART_H_





#include <fcntl.h>

#include <termios.h>

#include <stdio.h>



/*

baud rate

=============

B600

B1200

B9600

B19200

B38400

B57600

B115200

B230400

B460800

B921600

B1000000

B2000000

B3000000

B4000000

*/



/**

*@brief set uart port baud rate

*@param fd[in] file descriptor of uart port

*@param baud[in] baud rate like B9600

*@return none

*/

void set_baud(int fd, int baud)

{



	int status;

	struct termios opt;



	tcgetattr(fd, &opt);

	tcflush(fd, TCIOFLUSH);

	cfsetispeed(&opt, baud);

	cfsetospeed(&opt, baud);



	status = tcsetattr(fd, TCSANOW, &opt);

	if  (status != 0) {

        	perror("tcsetattr fd1");

        	return;

	}

	tcflush(fd,TCIOFLUSH);



}



/**

*@brief set word length, parity and stop bits

*@param fd[in] file descriptor of uart port

*@param databits[in] bits of word, valid value 5/6/7/8

*@param stopbits[in] stop bits, valid value 1 or 2

*@param parity[in]  parity should be one of None,Even,Odd,Mark,Space

*/



int set_params(int fd, int databits, int stopbits, int parity)

{



	struct termios options;



	if  ( tcgetattr( fd,&options)  !=  0) {

		perror("tcgetattr fail");

		return -1;

	}

	options.c_iflag = 0;

	options.c_oflag = 0;



	//setup data bits

	options.c_cflag &= ~CSIZE;

	switch (databits) {

	case 5:

		options.c_cflag |= CS5;

		break;

	case 6:

		options.c_cflag |= CS6;

		break;

	case 7:

		options.c_cflag |= CS7;

		break;

	case 8:

		options.c_cflag |= CS8;

		break;

	default:

		return -1;

	}



	//parity

	switch (parity) {

	case 'n':

	case 'N':

		options.c_cflag &= ~PARENB;	// Clear parity enable/

		options.c_iflag &= ~INPCK;	// disable input parity checking/

		break;

	case 'o':

	case 'O':

		options.c_cflag |= (PARODD | PARENB); // odd parity checking

		options.c_iflag |= INPCK;             // enable parity checking

		break;

	case 'e':

	case 'E':

		options.c_cflag |= PARENB;	// Enable parity /

		options.c_cflag &= ~PARODD;	// even parity/

		options.c_iflag |= INPCK;       // enable parity checking /

		break;

	case 'M':

	case 'm':  //mark parity/

		options.c_cflag |= PARENB | CMSPAR | PARODD;

		options.c_iflag |= INPCK;       // enable parity checking /

		break;

	case 'S':

	case 's':  //space parity/

		options.c_cflag |= PARENB | CMSPAR;

		options.c_cflag &= ~PARODD;

		options.c_iflag |= INPCK;       //enable parity checking /

		break;

	default:

		return -1;

	}



	// stop bits/

	switch (stopbits) {

	case 1:

		options.c_cflag &= ~CSTOPB;

		break;

	case 2:

		options.c_cflag |= CSTOPB;

		break;

	default:

		return -1;

	}



	// cts rts /

	//if ( uartID == 1 || uartID == 2 )

	//	options.c_cflag |= CRTSCTS;

	//else

	options.c_cflag &= ~CRTSCTS;



    options.c_lflag = 0;

    options.c_cc[VTIME]    = 10;

    options.c_cc[VMIN]     = 1;



	// Set input parity option /

	tcflush(fd,TCIFLUSH);

	if (tcsetattr(fd,TCSANOW,&options) != 0) {

		return -1;

	}

	return 0;

}





speed_t getBaudrate(int baudrate)

{

	switch(baudrate)

	{

		case 0: return B0;

		case 50: return B50;

		case 75: return B75;

		case 110: return B110;

		case 134: return B134;

		case 150: return B150;

		case 200: return B200;

		case 300: return B300;

		case 600: return B600;

		case 1200: return B1200;

		case 1800: return B1800;

		case 2400: return B2400;

		case 4800: return B4800;

		case 9600: return B9600;

		case 19200: return B19200;

		case 38400: return B38400;

		case 57600: return B57600;

		case 115200: return B115200;

		case 230400: return B230400;

		case 460800: return B460800;

		case 500000: return B500000;

		case 921600: return B921600;

		case 1000000: return B1000000;

		case 1152000: return B1152000;

		case 1500000: return B1500000;

		case 2000000: return B2000000;

		case 2500000: return B2500000;

		case 3000000: return B3000000;

		case 3500000: return B3500000;

		case 4000000: return B4000000;

		default: return 1152000;

	}

	return 1152000;

}



int uartDevInit(int fd, int uartBaud)

{

	//printf("baud=%d\n", baud);

	set_baud(fd, getBaudrate(uartBaud));



	// uart param /

	if(set_params(fd, 8, 1, 'n'))

	{

		printf("Set uart parameters fail.\n");

		return -1;

	}



	return 0;

}



#endif

#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "uart.h"

int main()
{
        int fd ;
        int buf[20];
        fd = open("/dev/ttyS4",O_RDWR);
        uartDevInit(fd,115200);

        while(1)
        {
                read(fd,buf,20);
                printf("%d\n",buf[0]);
        }

        return 0;

}
配置串口头文件就好了


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Linux串口测试程序的源码示例: ```c #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> #include <termios.h> int main() { int fd; struct termios options; fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY); // 打开串口设备 if (fd == -1) { perror("无法打开串口"); exit(EXIT_FAILURE); } fcntl(fd, F_SETFL, 0); // 获取当前串口的配置 tcgetattr(fd, &options); // 设置串口的波特率 cfsetispeed(&options, B9600); cfsetospeed(&options, B9600); // 设置数据位、停止位和奇偶校验位 options.c_cflag &= ~CSIZE; options.c_cflag |= CS8; options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; // 应用配置到串口 tcsetattr(fd, TCSANOW, &options); // 向串口发送数据 char buffer[] = "Hello, Serial Port!"; write(fd, buffer, sizeof(buffer)); // 从串口接收数据 char read_buffer[256]; int num_bytes = read(fd, read_buffer, sizeof(read_buffer)); read_buffer[num_bytes] = '\0'; printf("接收到的数据:%s\n", read_buffer); close(fd); return 0; } ``` 在以上示例中,程序首先打开了一个串口设备(在这里使用的是/dev/ttyS0,你可以根据实际情况修改)。然后,它获取了当前串口的配置,对其进行了相应的设置(如波特率、数据位、停止位和奇偶校验位等),并将配置应用到串口。程序接着向串口发送了一串数据,并从串口接收了数据。最后,程序关闭了串口设备。如果程序无法打开串口或者在读写串口时出现错误,它会打印相应的错误信息。 这只是一个简单的串口测试程序示例,你可以根据自己的需求对其进行修改和扩展。另外,请注意在编译时链接所需的头文件和库文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值