⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_lucas_com.c

📁 Linux下面的串口驱动程序
💻 C
字号:
#include<stdio.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<string.h>void show_usage(char* argv);const char* DEV_NAME="/dev/lucas_com0";int main(int argc, char** argv){	int times=2;		char ch;		int i;	int fd;	int rd_bytes,wr_bytes;	char buf[3]={'?','?','?'};	char one_byte=0;	if(2!=argc)	{		show_usage(argv[0]);		}		fd=open(DEV_NAME,O_RDWR,0777);	printf("fd=%d\n",fd);	if(0==strcmp(argv[1],"-w"))	{		buf[0]='a';buf[1]='b';buf[2]='c';		wr_bytes=write(fd,buf,sizeof(buf));		printf("%d bytes written\n",wr_bytes);	}	else if(0==strcmp(argv[1],"-r"))	{		printf("address of buf:%llu\n",buf);		do{							rd_bytes=read(fd,buf,sizeof(buf));			//printf("%d bytes read\n",rd_bytes);						printf("buf[0]=%c\n",buf[0]);			for(i=0;i<rd_bytes;i++)			{				printf("buf[%d]=%d\n",i,buf[i]);				}			printf("press q to quit\n");		}while('q'!=getchar());	}	else if(0==strcmp(argv[1],"-i"))	{		printf("address of buf:%llu\n",buf);		printf("press q to quit read, press other keys to read once\n");		while(1)		{			scanf("%c",&ch);			if('q'==ch)	break;								rd_bytes=read(fd,buf,sizeof(buf));			printf("%d bytes read\n",rd_bytes);						for(i=0;i<rd_bytes;i++)			{				printf("buf[%d]=%c\n",i,buf[i]);				}		}	}	else	{		close(fd);		show_usage(argv[0]);			}		close(fd);	return 0;}void show_usage(char* argv){		printf("usage: %s [-r|-w|-i]\n",argv);	exit(0);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -