span class=postbody이것은 간단히 Serial 통신을 하는 예제입니다.
br /
serial로부터 받은 데이터를 고스란히 다시 반향하여 보내는 것입니다.
br /
br /
현재 코드는
br /
/spantable align=center border=0 cellpadding=3 cellspacing=1 width=90%tbodytr tdspan class=genmedb인용:/b/span/td /tr tr td class=quoteport=COM1 (/dev/ttyS0)
br /
baud=9600
br /
parity=true
br /
stop=1
br /
data=8bit
br /
/td /tr/tbody/tablespan class=postbody로 만들어져 있습니다.
br /
br /
/spantable align=center border=0 cellpadding=3 cellspacing=1 width=90%tbodytr tdspan class=genmedb코드:/b/span/td /tr tr td class=code
br /
/*
br /
nbsp; Copyright (C) Information Equipment co.,LTD
br /
nbsp; All rights reserved.
br /
nbsp; Code by JaeHyuk Cho lt;mailto:minzkn@infoeq.comgt;
br /
nbsp; CVSTAG=$Header$
br /
*/
br /
br /
#include lt;sys/types.hgt;
br /
#include lt;sys/time.hgt;
br /
#include lt;stdio.hgt;
br /
#include lt;unistd.hgt;
br /
#include lt;fcntl.hgt;
br /
#include lt;malloc.hgt;
br /
#include lt;string.hgt;
br /
#include lt;memory.hgt;
br /
#include lt;signal.hgt;
br /
#include lt;errno.hgt;
br /
#include lt;termios.hgt;
br /
br /
#define mz_dump(m_d,m_s) do{int s_o,s_w,s_i;unsigned char s_b[17];if(((void *)(m_d))!=NULL){s_b[16]='\0';s_o=(int)0;\
br /
while(s_olt;(m_s)){s_w=((m_s)-s_o)lt;16?((m_s)-s_o):16;printf(%08X,s_o);for(s_i=0;s_ilt;s_w;s_i++){if(s_i==8)printf(
| );else printf( );\
br /
s_b[s_i]=*(((unsigned char *)(m_d))+s_o+s_i);printf(%02X,s_b[s_i]);if((s_b[s_i]amp;0x80)||(s_b[s_i]lt;' '))s_b[s_i]='.';}\
br /
while(s_ilt;16){if(s_i==8)printf(nbsp; nbsp; nbsp;);else printf(nbsp; nbsp;);s_b[s_i]=' ';s_i++;}\
br /
printf( [%s]\n,(char *)s_b);s_o+=16;}}else printf(error: dump null\n);}while(0)
br /
br /
int g_mz_break = 0;
br /
br /
void mz_my_signal(int s_signal)
br /
{
br /
switch(s_signal)
br /
{
br /
nbsp; case SIGINT:
br /
nbsp; nbsp; nbsp; nbsp;g_mz_break = 1;
br /
nbsp; nbsp; nbsp; nbsp;(void)signal(s_signal, mz_my_signal);
br /
nbsp; nbsp; nbsp; nbsp;break;
br /
nbsp; default:
br /
nbsp; nbsp; nbsp; nbsp;(void)fprintf(stdout, unknown signal ! (%d)\n, s_signal);
br /
nbsp; nbsp; nbsp; nbsp;break;
br /
}
br /
}
br /
br /
int main(void)
br /
{
br /
/* configuration */
br /
char *s_device_name = /dev/ttyS0;
br /
speed_t s_baud = B9600;
br /
tcflag_t s_data_bits = CS8;
br /
int s_parity_bit = 1;
br /
br /
int s_handle, s_index, s_check;
br /
struct termios s_prev_termios, s_new_termios;
br /
ssize_t s_read_bytes, s_write_bytes;
br /
struct timeval s_timeval;
br /
fd_set s_fd_in;
br /
unsigned char s_buffer[ 4 lt;lt; 10 ];
br /
br /
(void)signal(SIGINT, mz_my_signal);
br /
br /
s_handle = open(s_device_name, O_RDWR | O_NOCTTY);
br /
if(s_handle != (-1))
br /
{
br /
nbsp; if(tcgetattr(s_handle, (struct termios *)(amp;s_prev_termios)) == 0)
br /
nbsp; {
br /
nbsp; nbsp;(void)memcpy((void *)(amp;s_new_termios), (void *)(amp;s_prev_termios), (size_t)sizeof(struct termios));
br /
br /
nbsp; nbsp;s_new_termios.c_iflag = IGNBRK | ((s_parity_bit == 0) ? ((tcflag_t)0) : IGNPAR);
br /
nbsp; nbsp;s_new_termios.c_oflag = (tcflag_t)0;
br /
nbsp; nbsp;s_new_termios.c_cflag = s_data_bits | CLOCAL | CREAD;
br /
nbsp; nbsp;s_new_termios.c_lflag = (tcflag_t)0;
br /
nbsp; nbsp;for(s_index = 0;s_index lt; NCCS;s_index++)s_new_termios.c_cc[s_index] = (cc_t)0;
br /
nbsp; nbsp;s_new_termios.c_cc[VMIN] = (cc_t)1;
br /
nbsp; nbsp;s_new_termios.c_cc[VTIME] = (cc_t)0;
br /
br /
nbsp; nbsp;if(cfsetispeed((struct termios *)(amp;s_new_termios), s_baud) == 0 amp;amp;
br /
nbsp; nbsp; nbsp; cfsetospeed((struct termios *)(amp;s_new_termios), s_baud) == 0)
br /
nbsp; nbsp;{
br /
nbsp; nbsp; if(tcsetattr(s_handle, TCSANOW, (struct termios *)(amp;s_new_termios)) == 0)
br /
nbsp; nbsp; {
br /
nbsp; nbsp; nbsp;if(tcflush(s_handle, TCIOFLUSH) == 0)
br /
nbsp; nbsp; nbsp;{
br /
nbsp; nbsp; nbsp; (void)fprintf(stdout, Ready serial\n);
br /
nbsp; nbsp; nbsp; do
br /
nbsp; nbsp; nbsp; {
br /
nbsp; nbsp; nbsp; nbsp;FD_ZERO(amp;s_fd_in);
br /
nbsp; nbsp; nbsp; nbsp;FD_SET(s_handle, amp;s_fd_in);
br /
nbsp; nbsp; nbsp; nbsp;s_timeval.tv_sec = (long)1, s_timeval.tv_usec = (long)0;
br /
nbsp; nbsp; nbsp; nbsp;s_check = select(s_handle + 1, (fd_set *)(amp;s_fd_in), (fd_set *)0, (fd_set *)0, (struct timeval *)(amp;s_timeval));
br /
nbsp; nbsp; nbsp; nbsp;if(s_check gt; 0)
br /
nbsp; nbsp; nbsp; nbsp;{
br /
nbsp; nbsp; nbsp; nbsp; s_read_bytes = read(s_handle, (void *)(amp;s_buffer[0]), (size_t)sizeof(s_buffer));
br /
nbsp; nbsp; nbsp; nbsp; if(s_read_bytes gt; ((ssize_t)0))
br /
nbsp; nbsp; nbsp; nbsp; {
br /
nbsp; nbsp; nbsp; nbsp; nbsp;(void)fprintf(stdout, recv data %d byte(s) {\n, (int)s_read_bytes);
br /
nbsp; nbsp; nbsp; nbsp; nbsp;mz_dump(amp;s_buffer[0], s_read_bytes);
br /
nbsp; nbsp; nbsp; nbsp; nbsp;(void)fprintf(stdout, }\n);
br /
#if 1 /* echo part */
br /
nbsp; nbsp; nbsp; nbsp; nbsp;s_write_bytes = write(s_handle, (void *)(amp;s_buffer[0]), (size_t)s_read_bytes);
br /
nbsp; nbsp; nbsp; nbsp; nbsp;(void)fprintf(stdout, echo %d/%d byte(s)\n, (int)s_write_bytes, (int)s_read_bytes);
br /
#endif
br /
nbsp; nbsp; nbsp; nbsp; }
br /
nbsp; nbsp; nbsp; nbsp; else if(s_read_bytes == ((ssize_t)0))(void)fprintf(stdout, no data\n);
br /
nbsp; nbsp; nbsp; nbsp; else (void)perror(read);
br /
nbsp; nbsp; nbsp; nbsp;}
br /
nbsp; nbsp; nbsp; nbsp;else if(s_check == 0)(void)fprintf(stdout, wait\n);
br /
nbsp; nbsp; nbsp; nbsp;else (void)perror(select);
br /
nbsp; nbsp; nbsp; }while(g_mz_break == 0);
br /
nbsp; nbsp; nbsp; (void)fprintf(stdout, End of serial\n);
br /
nbsp; nbsp; nbsp;}
br /
nbsp; nbsp; nbsp;else (void)perror(tcflush);
br /
nbsp; nbsp; }
br /
nbsp; nbsp; else (void)perror(tcsetattr);
br /
nbsp; nbsp;}
br /
nbsp; nbsp;else (void)perror(cfset{i/o}speed);
br /
nbsp; nbsp;if(tcsetattr(s_handle, TCSANOW, (struct termios *)(amp;s_prev_termios)) != 0)(void)perror(tcsetattr(restore));
br /
nbsp; }
br /
nbsp; else (void)perror(tcgetattr);
br /
nbsp; (void)close(s_handle);
br /
}
br /
else (void)perror(open);
br /
return(1);
br /
}
br /
br /
/* vim: set expandtab: */
br /
/* End of source *//td/tr/tbody/table
받은 트랙백이 없고,
댓글이 없습니다.

글
댓글을 달아 주세요
댓글 RSS 주소 : http://blog.minzkn.com/rss/comment/113댓글 ATOM 주소 : http://blog.minzkn.com/atom/comment/113