open ,write, read close 관련 질문있습니다.

pwy1575의 이미지

안녕하세요

open ,write, read close 4가지 함수를 이용해서 디바이스 장치와 통신하는 간단한 프로그램 작성중 이해가 안되는 부분이 있어 질문드립니다.

int		nComDev;
long 	BAUD;
long 	DATABITS;
long 	STOPBITS;
long 	PARITYON;
long 	PARITY;
struct termios oldtio, newtio;
 
	nComDev = open ("/dev/ttyUSB4", O_RDWR | O_NONBLOCK | O_NOCTTY);
 
	if (nComDev < 0)
	{
		// FILE Open Fail
		 printf( "Device OPEN FAIL %s\n", "/dev/ttyUSB4" );
		 return -1;
	}
 
		tcgetattr(nComDev,&oldtio); // save current port settings
 
		// set new port settings for canonical input processing
		BAUD=B115200;
		DATABITS = CS8;
		STOPBITS= 0;
		PARITY = 0;
		PARITYON=0;
 
		newtio.c_cflag = BAUD | CRTSCTS | DATABITS | STOPBITS | PARITYON | PARITY | CLOCAL | CREAD;
		newtio.c_iflag = IGNPAR;
		newtio.c_oflag = 0;
		newtio.c_lflag = 0;       //ICANON;
		newtio.c_cc[VMIN]=1;
		newtio.c_cc[VTIME]=0;
		tcflush(nComDev, TCIFLUSH);
		tcsetattr(nComDev,TCSANOW,&newtio);
 
		res = write((LPSTR)"AT",strlen("AT"));
 
		while( (read_n = read(nComDev, InData, sizeof(InData))) > 0)
		{
 
			printf("\nREADING....\n");
 
			for(i=0;i<read_n;i++)
			{
				printf("%c",InData[i]);
			}
 
			memset(InData,0x00,sizeof(InData));
		}
 
	close(nComDev);	

위와 같은 프로그램에서 최초 1회 디바이스와 통신을 성공한 후 다시 프로그램을 실행 시키면 read함수 부분에서 -1 를 반환합니다.

그래서 'cat /dev/ttyUSB4' 명령어를 이용해서 디바이스를 화면에 찍어 보았습니다. (위 프로그램을 무한 반복 시킨후 background로 실행 이후 'cat /dev/ttyUSB4' 또한 background 실행)

cat을 이용해 ttyUSB4를 읽을 경우에는 read함수와 달리 위 프로그램에서 입력하는 AT에 대한 디바이스의 응답('OK'란 문자열) 이 반환 되는 것을 확인 할 수 있었습니다.

이 경우로 볼때 open에 의해 장치의 파일 디스크립터 연결 및 write 함수의 기능을 정상적으로 동작한거 같습니다.(추측)
하지만 read함수 호출시에는 -1을 반환합니다.

이 문제에 대해 짐작 가는점이 있으시면 답변 부탁드리겠습니다.

감사합니다.

bushi의 이미지

write() 함수 사용법 틀렸습니다.

O_NONBLOCK 으로 open() 하셨으니 non-bloking I/O 상태이고,
read() 시스템콜이 호출될 때 읽어낼 게 아무것도 없으면 -1 이 리턴되고 errno 는 EAGAIN 혹은 EWOULDBLOCK 값을 가지게 됩니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.