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

📄 com0t_txt.c

📁 一个基于嵌入式linux的串口驱动程序和测试程序的源码
💻 C
字号:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>


#define BUFSIZE 1000
             
int  main(void)
{
	FILE *fp;
    	int fd,result;
   	int i;
    	char read_buf[BUFSIZE];
	char write_buf[BUFSIZE]; //={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p'};
    	
    /*
    Open modem device for reading and writing and not as controlling tty
    because we don't want to get killed if linenoise sends CTRL-C.
  */
   	fd = open("/dev/uart0", O_RDWR | O_NOCTTY | O_NDELAY);
   	if (fd < 0){
     		printf("Error in open COM0\n");
	     	exit(-1); 
     	}
	printf("open ok\n");
//	while(1){
#if 0	
		result=read(fd, read_buf, sizeof(read_buf));
		if (result==BUFSIZE){
/*			printf("%d character read, read right.\n", result);
			for (i=0;i<BUFSIZE;i++){
				printf("%c", read_buf[i]);
			}
			printf("\n");
*/
		}else if (result<BUFSIZE){
			printf("only read %d character.\n", result);
			return result;
		}else{
			printf("read error.\n");
			return -1;
		}

		fp=fopen("/home/com0.txt", "w+");
		fwrite(&read_buf, sizeof(read_buf), 1, fp);
		fclose(fp);
#endif
#if 1
		fp=fopen("/home/com0.txt", "r");
		fread(&write_buf, sizeof(write_buf), 1, fp);
		fclose(fp);
		result=write(fd, write_buf, sizeof(write_buf));
	
		if (result==BUFSIZE){
/*
			printf("%d character write, write right.\n", result);
*/
		}else if (result<BUFSIZE){
			printf("only write %d character.\n", result);
			return result;
		}else{
			printf("write error.\n");
			return -1;
		}
#endif
//	}
	close(fd);
    	return 0;
}

⌨️ 快捷键说明

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