[embeded] A sample of searial port communication

2019-7-10 写技术

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

void main()
{
	struct termios opt;
	int tty;
	int len;
	int i;
	unsigned char data[1024] = {'a','b','c','d','e','f','g','h','i','j'};
	printf("log.anycle.com");
	printf("\n");

	tty = open("/dev/ttyUSB0", O_RDWR );

	if (tcgetattr(tty, &opt)< 0) {
		printf(" get error\n");
	}
	cfsetispeed(&opt, B115200);
	cfsetospeed(&opt, B115200);
	cfmakeraw(&opt);
	if (tcsetattr(tty,   TCSANOW,   &opt)<0) {
		printf(" set error\n");
	}

	write(tty, data, 10);

	while( (len = read(tty, data, 1024)) <= 0){
		usleep(50);
	}
	printf(" %d\n", len);
	for(i=0;i<len;i++){
		printf(" %02x", data[i]);
	}
	printf(" \n");

	close(tty);
}

标签: embedded C linux

发表评论:

Powered by anycle 湘ICP备15001973号-1