리눅스 분류
리눅스에서 com1송신하고 com2로 수신하기
작성자 정보
- kittenjun 작성
- 작성일
컨텐츠 정보
- 2,020 조회
- 0 추천
- 목록
본문
수신 코드 입니다. #include #include #include #include #include #include #include #include #include #include #include #include void* Com1OnRead(void* arg); int main(int argc, char *argv[]) { int fd; int state; struct termio tio; pthread_t thread; void* thread_result; if(fd >= 0) { close(fd); fd = -1; } fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NONBLOCK); printf ("fd====> %d ", fd); if(fd < 0) { fd =-1; return -1; } memset(&tio, 0, sizeof(tio)); tio.c_iflag = IGNBRK | INPCK; tio.c_oflag = 0; tio.c_cflag = B4800 | CS8 | CLOCAL | CREAD ; tcflush (fd, TCIFLUSH); tcsetattr (fd, TCSANOW, &tio); state = pthread_create(&thread,NULL,Com1OnRead,(void *)fd); if(state != 0) { printf("쓰레드 Join 오류"); exit(1); } printf("생성된 쓰레드의 ID : %d ", thread); printf("============================ "); //while(1); state=pthread_join(thread, &thread_result); if(state != 0){ printf("쓰레드 Join 오류"); exit(1); } printf("============================ "); printf("main함수 종료, 쓰레드 리턴 %s", (char*)thread_result); printf("============================ "); free(thread_result); return 0; } void* Com1OnRead(void* arg) { int fd = (int)arg; int state; char buf[256]; int len =0 ; fd_set fdcom1,fdcom2; struct timeval timeout; FD_ZERO(&fdcom1); FD_SET(fd, &fdcom1); while(1) { fdcom1 = fdcom2 ; timeout.tv_sec = 10; timeout.tv_usec = 0; printf("============================ "); state = select(fd+1,&fdcom2,NULL,NULL,&timeout); memset(buf, 0, sizeof(buf)); if(state == -1) { printf("select error "); exit(1); } else if(state == 0) { printf("====================>timeout "); } else { /*while(1) { ioctl(fd, FIONREAD, &len); if(len > 1) break; }*/ if(FD_ISSET(fd, &fdcom2)) { //ioctl(fd,FIONREAD,&len); len = read(fd, buf, sizeof(buf)); //if(len < 0) return 1; buf[len] = 0; printf("%s", buf); } }//else }//while() }//void |
코드: |
송신코드입니다. #include #include #include #include #include #include #include #include #include #include #include #define BUFSIZE 100 //#define NAMESIZE 20 void* Com1OnWrite(void* arg); //char name[NAMESIZE] = "[Default]"; char message[BUFSIZE]; int main(int argc, char *argv[]) { int fd; pthread_t thread; void* thread_result; struct termio tio; //sprintf(name, "[%s]", argv[0]); if(fd >= 0) { close(fd); fd = -1; } fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NONBLOCK); printf ("fd====> %d ", fd); if(fd < 0) { fd =-1; return -1; } memset(&tio, 0, sizeof(tio)); tio.c_iflag = IGNBRK | INPCK; tio.c_oflag = 0; tio.c_cflag = B4800 | CS8 | CLOCAL | CREAD ; tcflush (fd, TCIFLUSH); tcsetattr (fd, TCSANOW, &tio); pthread_create(&thread,NULL,Com1OnWrite,(void*)fd); pthread_join(thread, &thread_result); return 0; } void* Com1OnWrite(void* arg) { int fd = (int)arg; char name_message[BUFSIZE];//NAMESIZE while(1){ fgets(message, BUFSIZE, stdin); if(!strcmp(message,"q ")){ exit(0); } sprintf(name_message, "%s",message);//name printf("data : %s ",name_message); write(fd,name_message,sizeof(name_message)); //write(fd, message, sizeof(message)); } } |
왜 안될까요.ㅡㅡ;
시리얼 통신은 소켓과 틀려거서 그냥 com포트를 열어 주고 날려 주면 된다고 들었습니다. 그래서 com포트를 열어서 지속적을 날려 주고 날라오는 데이터가 있는지 감시도 하고 있는데 전혀 받을 생각을 안하네요.. 그리고 송신 쪽에서 타켓이 없는데 그냥 날리면
시리얼 이 사용하는 버퍼에 들어가는지....여하튼 공부 좀 더 해야 겠네요....
이 소스 좀 봐주세요....도무지 뭐게 문제인지 모르겠네요....
그리고 집에 시리얼 통신 되면 테스트도 같이 좀 해주심이...ㅠ.ㅠ
관련자료
-
이전
-
다음
댓글 0
등록된 댓글이 없습니다.