全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3906
推到 Plurk!
推到 Facebook!

請問LINUX的RS-232收碼問題!!

缺席
vk8051
一般會員


發表:22
回覆:33
積分:21
註冊:2007-08-10

發送簡訊給我
#1 引用回覆 回覆 發表時間:2010-06-13 19:51:46 IP:114.36.xxx.xxx 訂閱
請問我在網路上找到這個範例程式,然後我要收RS232的通訊碼如0XFF, 0X01, 0X03.....等等,但是都會變成分2次收碼...像0XFF    第2次才再印出其他剩下的碼0X01, 0X03.....
請問使用READ()涵式讀取 UART埠要如何可以一次收完一組完整的碼,而不是分2次收完...或是這範例那邊還需要修改的....

謝謝


#include
#include
#include
#include
#include
#include
#include
#include
int speed_arr[] = { B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400, B1200, B300, };
int name_arr[] = {38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300, };
void set_speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i )
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}

int set_Parity(int fd,int databits,int stopbits,int parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(FALSE);
}
options.c_cflag &= ~CSIZE;

switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (FALSE);
}

switch (parity)
{
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK; /* Disnable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (FALSE);
}

switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (FALSE);
}
/* Set input parity option */
if (parity != 'n')
options.c_iflag |= INPCK;
options.c_cc[VTIME] = 150; // 15 seconds
options.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (FALSE);
}
return (TRUE);
}

int OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}

int main(int argc, char **argv)
{
int fd;
int nread,i;
char buff[512];
char *dev ="/dev/ttyS1";
fd = OpenDev(dev);
if (fd>0)
set_speed(fd,19200);
else
{
printf("Can't Open Serial Port!\n");
exit(0);
}
if (set_Parity(fd,8,1,'N')== FALSE)
{
printf("Set Parity Error\n");
exit(1);
}
while(1)
{
while((nread = read(fd,buff,512))>0)
{
printf("\nLen %d\n",nread);
for( i=0; i {
printf( "0x%X ",buff[i] );
}
}
}

}
------
vincent
編輯記錄
vk8051 重新編輯於 2010-06-13 19:52:30, 註解 無‧
ub901
一般會員


發表:3
回覆:7
積分:2
註冊:2010-11-24

發送簡訊給我
#2 引用回覆 回覆 發表時間:2011-02-24 14:59:48 IP:114.32.xxx.xxx 訂閱
不知vk8051兄這問題解決了嗎?
小弟之前也卡在這裡卡很久!
vk8051
一般會員


發表:22
回覆:33
積分:21
註冊:2007-08-10

發送簡訊給我
#3 引用回覆 回覆 發表時間:2011-02-24 19:36:00 IP:118.166.xxx.xxx 訂閱
解決了.....
------
vincent
系統時間:2024-03-28 23:46:56
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!