0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

c: uart 通信のテストプログラム

Last updated at Posted at 2020-09-30

uart 通信のテストをする c のプログラムです。
Raspberry Pi で、TxD と RxD を短絡させてテストしました。

uart_test.c
// -----------------------------------------------------------------------
/*
	uart_test.c

					Sep/20/2020
*/
// -----------------------------------------------------------------------
# include <stdio.h>
# include <stdlib.h>
# include <fcntl.h>
# include <termio.h>
# include <unistd.h>
# include <sys/ioctl.h>
# include <string.h>
# include <sys/select.h>

# define SERIAL_PORT "/dev/ttyS0"

static char w[2] = "A";

// -----------------------------------------------------------------------
int main(int argc,char *argv[])
{
	int fd;
	int it;
	unsigned char buf[255];
	struct termios tio;

	fprintf(stderr, "*** start ***\n");
	fprintf(stderr, "*** Sep/30/2020 ***\n");

	for (it = 0; it < sizeof(buf); it++)
		{
		buf[it] = 0;
		}
 
	int rv; 
	struct timeval timeout;

	timeout.tv_sec = 0;
	timeout.tv_usec = 10000;

	fd_set set;

	if ((fd = open(SERIAL_PORT, O_RDWR)) < 0)
		{
		fprintf(stderr, "open error\n");
		exit(1);
		}

	FD_ZERO(&set);
	FD_SET(fd, &set);

	fprintf(stderr, "*** check *** aaa ***\n");

	bzero(&tio, sizeof(tio));

	/* 115200bps, フロー制御有り, 8ビット,DTR/DSR無効,受信可能 */
	tio.c_cflag = B115200 | CRTSCTS | CS8 | CLOCAL | CREAD;
	tio.c_cc[VMIN] = 1;	/* 入力データをバッファしない */
	tcsetattr(fd, TCSANOW, &tio); /* アトリビュートのセット */

	fprintf(stderr, "*** check *** bbb ***\n");

	for(it = 0; it < 10; it++)
		{
		write(fd, w, 1);

		rv = select(fd + 1, &set, NULL, NULL, &timeout);

		if(rv == -1)
			fprintf(stderr,"select\n"); /* an error accured */
		else if(rv == 0)
			fprintf(stderr,"timeout\n"); /* a timeout occured */
		else
			{
			int len = read(fd, buf, sizeof(buf));
			if (0 < len)
				{
				printf("read = %s\n", buf);
				}
			else
				{
				printf("len = %d\n", len);
				}
			}

		w[0]++;
	}

	close(fd);

	fprintf(stderr, "*** end ***\n");

	exit(0);
}



// -----------------------------------------------------------------------
Makefile
all: uart_test.o
	cc -o uart_test uart_test.c
clean:
	rm -f uart_test *.o

TxD と RxD を短絡させた時の結果

# ./uart_test 
*** start ***
*** Sep/30/2020 ***
*** check *** aaa ***
*** check *** bbb ***
read = A
read = B
read = C
read = D
read = E
read = F
read = G
read = H
read = I
read = J
*** end ***

短絡させない時の結果

# ./uart_test 
*** start ***
*** Sep/30/2020 ***
*** check *** aaa ***
*** check *** bbb ***
timeout
timeout
timeout
timeout
timeout
timeout
timeout
timeout
timeout
timeout
*** end ***

次のページを参考にしました。
Raspberry PiでUARTの有効化+シリアル通信

関連ページ
Raspberry Pi で uart 通信のテスト

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?