new0001.c

来自「基于at91rm9200的串口驱动」· C语言 代码 · 共 40 行

C
40
字号
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
int main()
{
 char szsendbuf[4];
 int fd,c,res,nsize = 4;
 FILE *pfile;
 int filel;
 struct termios oldtio,newtio;
 struct stat st;
 fd = open("/dev/ttyS0",O_RDWR | O_NOCTTY);
 tcgetattr(fd, &oldtio);
 bzero(&newtio, sizeof(newtio));
 newtio.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
 newtio.c_iflag = IGNPAR | ICRNL;
 tcflush(fd, TCIFLUSH);
 tcsetattr(fd, TCSANOW, &newtio);
 stat("a.txt", &st);
 pfile = fopen("a.txt", "r");
 filel = st.st_size;
 while(4 == nsize)
 {
  bzero(szsendbuf, 4);
  nsize = fread(szsendbuf, 1, 4, pfile);
  nsize = write(fd, szsendbuf, nsize);
 }
 fclose(pfile);
 tcsetattr(fd, TCSANOW, &oldtio);
 close(fd);
}


⌨️ 快捷键说明

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