From owner-FreeBSD-users-jp@jp.FreeBSD.org Sat Nov 17 14:59:27 2012
Received: (from daemon@localhost)
	by castle.jp.FreeBSD.org (8.11.6p2+3.4W/8.11.3) id qAH5xRZ25387;
	Sat, 17 Nov 2012 14:59:27 +0900 (JST)
	(envelope-from owner-FreeBSD-users-jp@jp.FreeBSD.org)
Received: from sh.kkdlabs.jp (s236214.ppp.asahi-net.or.jp [220.157.236.214])
	by castle.jp.FreeBSD.org (8.11.6p2+3.4W/8.11.3) with ESMTP/inet id qAH5xRL25382
	for <FreeBSD-users-jp@jp.FreeBSD.org>; Sat, 17 Nov 2012 14:59:27 +0900 (JST)
	(envelope-from kohi@kkdlabs.jp)
Received: from localhost (sh.kkdlabs.jp [10.2.255.241])
	by sh.kkdlabs.jp (8.14.5/8.14.4) with ESMTP id qAH5xPn2099373;
	Sat, 17 Nov 2012 14:59:27 +0900 (JST)
	(envelope-from kohi@kkdlabs.jp)
Message-Id: <20121117.145925.2034792197741372928.kohi@kkdlabs.jp>
To: FreeBSD-users-jp@jp.FreeBSD.org
From: Koh-ichi Ito <kohi@kkdlabs.jp>
In-Reply-To: <20121115223746.15ecb0ef.kh@mogami.com>
References: <20121115063148.3ee4a1eb.kh@mogami.com>
	<20121115.200701.137056035.kohi@kkdlabs.jp>
	<20121115223746.15ecb0ef.kh@mogami.com>
X-Mailer: Mew version 6.5 on Emacs 24.2 / Mule 6.0 (HANACHIRUSATO)
Mime-Version: 1.0
Content-Type: Text/Plain; charset=iso-2022-jp
Content-Transfer-Encoding: 7bit
Reply-To: FreeBSD-users-jp@jp.FreeBSD.org
Precedence: list
Date: Sat, 17 Nov 2012 14:59:25 +0900
X-Sequence: FreeBSD-users-jp 94056
Subject: [FreeBSD-users-jp 94056] Re: umodem
 =?ISO-2022-JP?B?GyRCJEcbKEI=?= PICC18F2550
 =?ISO-2022-JP?B?GyRCJEslIiUvJTslOSQ5JGskSElUMEJEahsoQg==?=
Sender: owner-FreeBSD-users-jp@jp.FreeBSD.org
X-Originator: kohi@kkdlabs.jp
X-Distribute: distribute version 2.1 (Alpha) patchlevel 24e+060209

$B$$$H$&$G$9!#AjJQ$o$i$:;W$o$7$/$"$j$^$;$s$,!";n$7$?$3$H$N%l%]!<%H$G$9!#(B

From: Kouichi Hirabayashi <kh@mogami.com>
Subject: [FreeBSD-users-jp 94055] Re: umodem $B$G(B PICC18F2550 $B$K%"%/%;%9$9$k$HIT0BDj(B
Date: Thu, 15 Nov 2012 22:37:46 +0900

> $BAj<j$N%G%P%$%9$,%3%^%s%I(B(BARH\r\n)$B$rFI$a$F$$$J$$$+!"(B
> $B%3%^%s%I(B/$B%l%9%]%s%9$,$A$0$O$0$K$J$C$F$$$k$h$&$J5$$,(B
> $B$7$^$9!#Aj<jB&$N%G!<%?$rFI$_=P$9J}K!$,$"$k$H$h$$$N(B
> $B$G$9$,!"$=$l$,Hs8=<BE*$G$J$$$H$7$?$i!"(Bselect() $B$r;H(B
> $B$o$9$K!"(B
> 
>   open();	// LF $B=*C<$N(B canonincal $BF~NO(B
>   sleep(); 	// 1, 2 $BIC$H$+D9$a$K$7$F(B
>   write();	// $B%3%^%s%I(B
>   read();

$B$d$C$F$_$^$7$?$,!">u67$OJQ$o$j$^$;$s$G$7$?!#(B

> $B$r;n$7$F!"$=$l$G$b$?$a$J$i!"(Bopen() $B$G%G%j%_%?(B(LF)$B$G(B
> $B$J$/(B 1 $B%P%$%HC10L$NF~NO$r;XDj$7$F!"%P%$%HC10L$GFI$s(B
> $B$G8+$k$H2?$+$o$+$k$+$b$7$l$^$;$s!#(B

$B$$$?$@$$$?%"%I%P%$%9$rH?1G$7$?$D$b$j$G!"KvHx$N$h$&$J%3!<%I$r;n$7$F$_$^(B
$B$7$?!#(B

- raw $B%b!<%I$K$7$F!"(B1Byte $B$:$D(B sleep()$B$7$J$,$i(B write()$B$7!"(Bread()$B$b(B
  1Byte $B$:$DFI$`$h$&$K$7$?!#(B
- read()$B$NA0$N(B select()$B$rI|3h$5$;$F!"5$057WB&$,BT$A$KF~$C$F$$$k$h$&$J$i(B
  $B:FAw$9$k$h$&$K$7$?!D$D$b$j$@$,!"$&$^$/$$$C$F$$$J$$LOMM!#(B

$B$H$$$&$N$,5$$r$D$1$?$H$3$m$G$9!#(B

$B7k2L$O(B
-----
# ./test11
i = 0
  j = 0
    BARH
      :
    ($BN,(B)
      :
i = 9
  j = 0
    BARH
    calling select()  back from select(), n = 1
calling read()   c = '1'(0x31)  calling read()   c = '0'(0x30)  calling read()   c = '0'(0x30)  calling read()   c = '4'(0x34)  calling read()   c = '.'(0x2e)  calling read()   c = '5'(0x35)  calling read()   c = 'h'(0x68)  calling read()   c = 'P'(0x50)  calling read()   c = 'a'(0x61)  calling read()   c = '.'(0x0d)  calling read()   c = '.'(0x0a)  calling read()   c = '>'(0x3e)
rmesg: 1004.5hPa
>
i = 10
  j = 0
    BARH
    calling select()  back from select(), n = 0
  j = 1
    BARH
    calling select()  back from select(), n = 0
  j = 2
    BARH
    calling select()  back from select(), n = 0
  j = 3
    BARH
    calling select()  back from select(), n = 0
  retry limit of write() exceeded.
-----
$B$H!"(Bread()$B$N<jA0$N(B select()$B$,%?%$%`%"%&%H$7$F!"$5$i$K(B close()$B$,5"$C$FMh(B
$B$^$;$s!#(B

$ ps alxwwp 98979
  UID   PID  PPID CPU PRI NI   VSZ   RSS MWCHAN STAT  TT       TIME COMMAND
    0 98979 91357   0  44  0  5832  1052 ttyout I+     1    0:00.00 ./test11

$B$H!":#EY$O(B ttyout $B$G%V%m%C%/$5$l$F$$$^$9!#(B

$B$J$*!"(B0x0a $B$N<!$N(B'>'$B$O!"5$057W$N%3%^%s%I%i%$%s$N%W%m%s%W%H$G$9!#(B
-----
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/uio.h>

#define PAUSE	2
#define MAX_RETRY	3

main()
{
	int	i,
		j,
		k,
		fd;
	size_t	tlen;
	ssize_t	n;
	char	*tmesg,
		c,
		*p,
		rmesg[16];
	fd_set	fds;
	struct termios	tios;
	struct timeval	timeout;

	for (i = 0; ; i++) {
		printf("i = %d\n", i);
		if ( (fd = open("/dev/cuaU0", O_RDWR) ) < 0) {
			err(1, "open()");
		}
		sleep(PAUSE);
		if (ioctl(fd, TIOCEXCL) < 0) {
			err(1, "ioctl(TIOCEXCL)");
		}
		if (tcflush(fd, TCOFLUSH) < 0) {
			err(1, "tcflush(TCOFLUSH)");
		}
		if (tcflush(fd, TCIFLUSH) < 0) {
			err(1, "tcflush(TCIFLUSH)");
		}
		if (tcgetattr(fd, &tios) < 0) {
			err(1, "tcgetattr()");
		}
		tios.c_cc[VMIN] = 0;
		tios.c_cc[VTIME] = 100;
		tios.c_cflag |= (CREAD | CLOCAL);
		tios.c_iflag &= ~(IXON | IXOFF | IMAXBEL);
		tios.c_lflag &= ~(ECHO | ECHONL);
		cfmakeraw(&tios);
		if (tcsetattr(fd, TCSADRAIN, &tios) < 0) {
			err(1, "tcsetattr()");
		}
		timeout.tv_sec = 1;
		timeout.tv_usec = 0;
		tmesg = "BARH\r\n";
		tlen = strlen(tmesg);
		p = rmesg;
		j = 0;
		do {
			printf("  j = %d\n    ", j);
			for (k = 0; k < tlen; k++) {
				sleep(PAUSE);
				if (write(fd, &tmesg[k], 1) < 0) {
					err(1, "write()");
				}
				putchar(tmesg[k] );
				fflush(stdout);
			}
			FD_ZERO(&fds);
			FD_SET(fd, &fds);
			printf("    calling select()");
			fflush(stdout);
			if ( (n = select(fd + 1, &fds, NULL, NULL,
					&timeout) ) < 0) {
				err(1, "select() for read()");
			}
			printf("  back from select(), n = %d\n", n);
		} while ( (n == 0) && j++ < MAX_RETRY);
		if (!n) {
			printf("  retry limit of write() exceeded.\n");
			close(fd);
			printf("  back from close()\n");
			sleep(PAUSE);
			exit(1);
		}
		j = 0;
		while ( (j < MAX_RETRY) && (p - rmesg < sizeof(rmesg) ) ) {
			printf("calling read()  ");
			fflush(stdout);
			if ( (n = read(fd, &c, 1) ) < 0) {
				err(1, "read()");
				close(fd);
				sleep(PAUSE);
				exit(1);
			}
			if (n) {
				printf(" c = '%c'(0x%02x)  ",
					isprint(c)? c:'.', c);
			} else {
				printf(" n = 0  ");
			}
			fflush(stdout);
			if (c) {
				*p++ = c;
			} else {
				j++;
			}
			if (c == '>') {
				goto DONE;
			}
		}
		printf("retry limit of read() exceeded\n");
		close(fd);
		sleep(PAUSE);
		exit(1);
DONE:
		*p = '\0';
		printf("\nrmesg: %s\n", rmesg);
		close(fd);
		sleep(PAUSE);
	}
}
-----
kkdlabs.jp, featuring Koh-ichi Ito as just another DNS freak in town.
