2008년 11월 12일 수요일

터미널 IO Write, Read

write.c



#include
#include
#include
#include
#include

#define BAUDRATE B57600
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,c, res;
struct termios oldtio,newtio;
int bbb[1],aaa[1];
bbb[0]=0;

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0)
{
perror(MODEMDEVICE);
exit(-1);
}

tcgetattr(fd,&oldtio);

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

newtio.c_lflag = 0;


tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);


while (1) {
scanf("%d",&bbb[0]);

aaa[0]=255;
write(fd,aaa,1);
aaa[0]=0;
write(fd,aaa,1);
if(bbb[0]=='\n');
else write(fd,bbb,1);

aaa[0] = (aaa[0]^bbb[0]) & 0x7f;

write(fd,aaa,1);

}

tcsetattr(fd,TCSANOW,&oldtio);
}








------------------------------------------------------------------------

read.c





#include
#include
#include
#include
#include

#define BAUDRATE B115200
#define MODEMDEVICE "/dev/ttyS0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

main()
{
int fd,c, res;
struct termios oldtio,newtio;
//char buf[255];
//char buf[0],ab;
int buf[1];

fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

tcgetattr(fd,&oldtio);

bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;

tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);


while (STOP==FALSE) {
res = read(fd,buf,1);
//buf[res]=0;
printf("%c\n", buf[0]);
if (buf[0]=='z') STOP=TRUE;
}
tcsetattr(fd,TCSANOW,&oldtio);
}

댓글 없음: