tcp/ip to serial , serial to tcp/ip

황기천의 이미지

제가 하려는 작업은 이더넷과 시리얼 로 서로 데이터를 주고 받고 하는 겁니다.

그런데 tcp/ip에서 시리얼로는 데이터가 잘가는데요.

시리얼에서 tcp/ip로 보낼때는 tcp/ip에서 한바이트 정보를 보내야 시리얼에서 보낸데이터를 받을수 있습니다.

시리얼 통신부분은 kldp에 있는 how to serial 문서를 참고 했구요.

serial_init() 함수에 있는 아래 인자의 값을 수정해서 블럭되지 않도록 했거든요.

newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* blocking read until 1 character arrives */

serial에서 이더넷으로만 정보를 보낼때는 블럭되지 않고 잘가거든요.(아래 루프문 참고)

while(1)
{
str_len=read(fd, message,BUFSIZE);
write(clnt_sock, message, str_len);
}

근데 양방향으로 할려니 잘 안되네요. 고수님들 도움 바랍니다....

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

#include
#include
#include
#include
#include
#include
#include

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#include
#include
#include

#include "setup.h"

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 /* POSIX 호환 소스 */

#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

int fd;

int baud;

#define BUFSIZE 255
void error_handling(char *message);

struct termios oldtio,newtio;
void serial_init(int idx)
{
int c, res;
char buf[255];

load();
baud = bps_bit[App[idx].SI.bps];
s_port = App[idx].s_port;
c_ip = App[idx].c_ip;
c_port = App[idx].c_port;
s_mode = App[idx].s_mode;
se_dev= App[idx].SI.device;

printf(" device : %s, baud :%s, sport %s, cip %s, cport %s , mode %d \n",se_dev, bps_str[App[idx].SI.bps], s_port, c_ip, c_port, s_mode);

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

tcgetattr(fd,&oldtio); /* save current serial port settings */
bzero(&newtio, sizeof(newtio)); /* clear struct for new port settings */

//newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_cflag = baud | CRTSCTS | CS8 | CLOCAL | CREAD;

newtio.c_iflag = IGNPAR | ICRNL;

newtio.c_oflag = 0;

newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 0; /* blocking read until 1 character arrives */

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

int main(int argc, char **argv)
{
int serv_sock;
int clnt_sock;
char message[BUFSIZE];
int str_len;
int c, res;
int index;
char tmp;
char buf[255];

struct sockaddr_in serv_addr;
struct sockaddr_in clnt_addr;
int clnt_addr_size;
/* serial device index*/
index=0;
/*
if(argc!=2){
printf("Usage :%s \n", argv[0]);
exit(1);
}
*/
serial_init(index);
serv_sock= socket(AF_INET, SOCK_STREAM, 0);
if (serv_sock == -1)
error_handling("socket() error");

memset(&serv_addr, 0, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
// serv_addr.sin_port = htons(atoi(argv[1]));
serv_addr.sin_port = htons(atoi(s_port));

if(bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))== -1)
error_handling("bind() error");

if(listen(serv_sock, 5) == -1)
error_handling("listen() error");

clnt_addr_size = sizeof(clnt_addr);
clnt_sock = accept(serv_sock, (struct sockaddr*)& clnt_addr,(socklen_t*) &clnt_addr_size);
if(clnt_sock == -1)
error_handling("accept() error");

while(1){
str_len=read(clnt_sock, message, BUFSIZE);
write(fd, message, str_len);
str_len=read(fd, message,BUFSIZE);
write(clnt_sock, message, str_len);
}
tcsetattr(fd,TCSANOW,&oldtio);// restore
close(clnt_sock);

return 0;
}

댓글 달기

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