리눅스에서 시리얼 통신 할려고 하는데요...

kittenjun의 이미지

수신 코드 입니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <pthread.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h> 
 


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\n", 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 \n", thread);
	printf("============================\n");
	//while(1);
	state=pthread_join(thread, &thread_result);
	if(state != 0){
      printf("쓰레드 Join 오류");
      exit(1);
    }
    printf("============================\n");
	printf("main함수 종료, 쓰레드 리턴 %s", (char*)thread_result);
	printf("============================\n");
    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("============================\n");
	state = select(fd+1,&fdcom2,NULL,NULL,&timeout);
	 memset(buf, 0, sizeof(buf));
	if(state == -1)
	{
		printf("select error\n");
		exit(1);
	}
	  else if(state == 0)
	  {
		printf("====================>timeout\n");
	  }
	    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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <pthread.h>
#include <termios.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>

#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\n", 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\n")){
			exit(0);
		}
	    sprintf(name_message, "%s",message);//name
	    printf("data : %s\n",name_message);
	    write(fd,name_message,sizeof(name_message));
	    //write(fd, message, sizeof(message));
	}
}

왜 안될까요.ㅡㅡ;
시리얼 통신은 소켓과 틀려거서 그냥 com포트를 열어 주고 날려 주면 된다고 들었습니다. 그래서 com포트를 열어서 지속적을 날려 주고 날라오는 데이터가 있는지 감시도 하고 있는데 전혀 받을 생각을 안하네요.. 그리고 송신 쪽에서 타켓이 없는데 그냥 날리면
시리얼 이 사용하는 버퍼에 들어가는지....여하튼 공부 좀 더 해야 겠네요....
이 소스 좀 봐주세요....도무지 뭐게 문제인지 모르겠네요....
그리고 집에 시리얼 통신 되면 테스트도 같이 좀 해주심이...ㅠ.ㅠ
그래서 소스도 같이 올립니다....
하나는 송수신 두프로그램으로 만든 것이고
다른하나는 한개로 합쳐서 만든 것인데...급하게 만든거라..이 소스는 문제가 많을 거라 봅니다......구래도 봐주세요...ㅠ.ㅠ
File attachments: 
첨부파일 크기
Package icon comport(2.zip24.71 KB
Package icon comport.zip41.96 KB

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.