리눅스 null modem통신 질문입니다.
안녕하세요 리눅스 초보자입니다. 다름이 아니라 제가 시리얼 케이블을 이용해서 com1,com2간에 통신을 하려고 하는데 생각대로 잘 되지 않네요. 무엇이 잘못되엇는지 모르겟지만. 서로간에 보낸 데이터가 다른 포트로 전달이 되지 않네요. 제가 여기 가입한지 얼마 안대서 소스를 어떻게 올려야 할지 몰라서 그냥 드래그해서 올립니다.(보시기 불편하실듯) 아직 encoding_raw_hdlc,decoding_raw_hdlc부분은 구현을 못해서 그냥 스트링만 카피하는 루틴으로만 작성햇습니다. 제가 보기엔 포트설정부분이나 read,write부분이 잘못된듯 한데 모르겟네요 ㅠ ㅠ
하드웨어는 cat /dev/ttyS0 &후에 cat > /dev/ttyS1으로 해서 메세지 입력시 글이 뜨는건 확인을 했습니다..
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "rawhdlc.h"
WINDOW *OutputWin, *Win, *ChatWin,*InputWin;
u_char filename[40]="/dev/ttyS";
u_char src_buffer[MAX_BUFF_SIZE];
u_char dst_buffer[MAX_BUFF_SIZE];
int main(void)
{
int running = TRUE;
char retval;
pthread_t read_pthread, write_pthread;
struct everything hdlc;
if ( (hdlc.fd = init_scr())< 0 )
{
insert_data(OutputWin,"port initialize error");
delete_win();
return 0;
}
insert_data(OutputWin,"port initalize");
memset(&(hdlc.state),0,sizeof(struct hdlc_state));
hdlc.state.dst_len = MAX_BUFF_SIZE;
if ( pthread_create( &read_pthread, NULL, read_handle, (void *)&hdlc ) != 0 )
{
insert_data(OutputWin,"read_pthread_can't create");
delete_win();
close(hdlc.fd);
exit(1);
}
insert_data(OutputWin,"read_pthread initalize");
if ( pthread_create( &write_pthread, NULL, write_handle, (void *)&hdlc ) != 0 )
{
insert_data(OutputWin,"write_pthread_can't create");
delete_win();
close(hdlc.fd);
exit(1);
}
insert_data(OutputWin,"write_pthread initalize");
pause();
close(hdlc.fd);
return 0;
}
void insert_data(WINDOW *tt, u_char *buf)
{
wmove(tt,17,0);
wprintw(tt,"%s",buf);
scroll(tt);
wrefresh(tt);
wmove(InputWin,0,0);
wrefresh(InputWin);
}
int init_scr()
{
int retval;
initscr();
start_color();
init_pair(1,COLOR_BLUE, COLOR_WHITE);
init_pair(2,COLOR_MAGENTA, COLOR_WHITE);
init_pair(3,COLOR_BLACK, COLOR_WHITE);
Win = newwin(20,40,1,1);
wbkgd(Win,COLOR_PAIR(1));
box(Win,ACS_VLINE,ACS_HLINE);
refresh();
wrefresh(Win);
OutputWin = subwin(Win,18,37,2,2);
scrollok(OutputWin,TRUE);
wbkgd(OutputWin,COLOR_PAIR(1));
refresh();
wrefresh(OutputWin);
ChatWin = newwin(10,40,21,1);
wbkgd(ChatWin,COLOR_PAIR(1));
box(ChatWin,ACS_VLINE,ACS_HLINE);
refresh();
wrefresh(ChatWin);
InputWin = subwin(ChatWin,8,37,22,2);
wbkgd(InputWin,COLOR_PAIR(2));
refresh();
wrefresh(InputWin);
retval = port_scr();
wmove(InputWin,0,0);
return retval;
}
void delete_win()
{
delwin(InputWin);
delwin(OutputWin);
delwin(ChatWin);
delwin(Win);
refresh();
endwin();
}
int port_scr()
{
WINDOW *port;
int retval = TRUE;
u_char port_buff[5];
port = newwin(4,38,2,2);
wmove(port,1,2);
wprintw(port,"select com port[0-3].>");
wbkgd(port,COLOR_PAIR(3));
box(port,ACS_VLINE,ACS_HLINE);
refresh();
wgetnstr(port,port_buff,5);
strcat(filename,port_buff);
if ( ( retval = initport() ) < 0 )
{
if ( retval == -1 )
wprintw(port,"open_port: Unable to open com port ");
else if ( retval == -2 )
wprintw(port,"open_port: Unable to set termios ");
}
refresh();
delwin(port);
refresh();
return retval;
}
void *read_handle( void *pts )
{
fd_set readfds;
int retval;
u_char string;
struct timeval tm;
struct everything *hdlc = (struct everything *)pts;
pthread_detach(pthread_self());
tm.tv_sec = 0;
tm.tv_usec = 0;
while( hdlc->state.end == FALSE )
{
FD_ZERO(&readfds);
FD_SET(hdlc->fd, &readfds);
if ( ( retval = select((hdlc->fd)+1, &readfds, NULL, NULL, &tm) ) > 0 )
{
if ( FD_ISSET(hdlc->fd, &readfds) )
{
if ( ( retval = read( hdlc->fd, &string,1 ) ) < 0 )
{
insert_data(OutputWin,"read() error");
check_error(retval);
}
if ( ( retval = decode_raw_hdlc(&hdlc->state,string) ) < 0 )
{
insert_data(OutputWin,"decoding error");
check_hdlc_error(retval);
}
}
}
if ( hdlc->state.read_possible )
{
hdlc->state.dst_buffer[retval] = '\0';
insert_data(OutputWin,hdlc->state.dst_buffer);
if ( strncmp(hdlc->state.dst_buffer,"quit",4) == 0 )
hdlc->state.end = TRUE;
hdlc->state.read_possible = FALSE;
hdlc->state.o_bitcnt = 0;
}
}
insert_data(OutputWin,"read_pthread end");
delete_win();
}
void *write_handle( void *pts )
{
fd_set readfds;
int retval;
char running = TRUE;
struct everything *hdlc = (struct everything *)pts;
pthread_detach(pthread_self());
while(running)
{
wgetnstr(InputWin,src_buffer,MAX_BUFF_SIZE);
wclear(InputWin);
wrefresh(InputWin);
if ( strncmp(src_buffer,"quit",4) == 0 )
{
insert_data(OutputWin,"quit");
running = FALSE;
hdlc->state.end = TRUE;
}
insert_data(OutputWin,src_buffer);
if ( ( retval = encode_raw_hdlc(src_buffer,strlen(src_buffer),dst_buffer,MAX_BUFF_SIZE) ) < 0 )
{
insert_data(OutputWin,"encoding error");
check_hdlc_error(retval);
}
insert_data(OutputWin,"%d",retval);
if ( ( retval = write( hdlc->fd, dst_buffer,retval ) ) < 0 )
{
insert_data(OutputWin,"write error");
check_error(retval);
}
}
insert_data(OutputWin,"write_pthread end");
}
int initport()
{
int fd;
struct termios options;
struct termios test;
fd = open(filename, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
return fd;
else
fcntl(fd, F_SETFL, 0);
// Get the current options for the port...
tcgetattr(fd, &options);
// Set the baud rates to 9600...
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);
// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_lflag &= ~ECHO;
// Set the new options for the port...
tcflush(fd,TCIOFLUSH);
if ( tcsetattr(fd, TCSANOW, &options) < 0 )
return -2;
tcgetattr(fd, &test);
fcntl(fd, F_SETFL, FNDELAY); // don't block serial read
return fd;
}
댓글 달기