当前位置: 技术问答>linux和unix
串口读取的问题(问题解决高分相送 100分)
来源: 互联网 发布时间:2015-08-24
本文导语: 我写了一个读取串口的程序,但是每次读取的类容都是不完整的. 如果我让程序sleep(1)读取的内容又会多. 怎样控制读取才能取得自己想要的完整信息. | 用select监听,一次读取一个直到read返回0即...
我写了一个读取串口的程序,但是每次读取的类容都是不完整的.
如果我让程序sleep(1)读取的内容又会多.
怎样控制读取才能取得自己想要的完整信息.
如果我让程序sleep(1)读取的内容又会多.
怎样控制读取才能取得自己想要的完整信息.
|
用select监听,一次读取一个直到read返回0即可.
|
# include
# include
# include
# include
# include
# include
# include
# include
# include "tools.h"
extern int comm_flag;
jmp_buf env_buf;
void timeout_handler ( int );
FILE * fppp;
int rcv ( fd_r, fd_w, buffer, max_count )
unsigned char * buffer;
unsigned max_count;
int fd_r, fd_w;
{
unsigned char * current, lrc;
unsigned int i, lenth , time_out_comm;
char filename [ 80 ];
char date [ 9 ] , time [ 9 ] , tmp [40];
signal ( SIGALRM, timeout_handler );
alarm ( 0 );
setjmp ( env_buf );
current = buffer;
* current = 0x00;
if ( comm_flag == 1 ) { /* modem pool need handshake */
while ( * current != 0x05 ) /** ENQ **/
read ( fd_r, current, 1 );
* current = 0x06; /** ACK **/
write ( fd_w, current, 1 );
for ( i = 0; i = 6 )
return ( -1 );
}
else { /* nac or rs232 */
while ( * current != 0x02 ) /**** STX ****/
read ( fd_r, current, 1 );
}
/*
* when recv time large 3 sec
* error and exit !
*/
time_out_comm = GetTimeOut ( "COMPOS" ) ;
alarm ( time_out_comm ) ;
current ++;
for ( i = 0; i > 4 ) * 10 + ( * current & 0x0f );
current --;
lenth += ( * current >> 4 ) * 1000 + ( * current & 0x0f ) * 100;
current += 2;
ErrorLog ( TRACE, "lenth = %d", lenth );
if ( lenth >= 512 ) {
* current = 0x0;
alarm ( 0 ) ;
return ( -1 ) ;
}
for ( i = 0; i
# include
# include
# include
# include
# include
# include
# include
# include "tools.h"
extern int comm_flag;
jmp_buf env_buf;
void timeout_handler ( int );
FILE * fppp;
int rcv ( fd_r, fd_w, buffer, max_count )
unsigned char * buffer;
unsigned max_count;
int fd_r, fd_w;
{
unsigned char * current, lrc;
unsigned int i, lenth , time_out_comm;
char filename [ 80 ];
char date [ 9 ] , time [ 9 ] , tmp [40];
signal ( SIGALRM, timeout_handler );
alarm ( 0 );
setjmp ( env_buf );
current = buffer;
* current = 0x00;
if ( comm_flag == 1 ) { /* modem pool need handshake */
while ( * current != 0x05 ) /** ENQ **/
read ( fd_r, current, 1 );
* current = 0x06; /** ACK **/
write ( fd_w, current, 1 );
for ( i = 0; i = 6 )
return ( -1 );
}
else { /* nac or rs232 */
while ( * current != 0x02 ) /**** STX ****/
read ( fd_r, current, 1 );
}
/*
* when recv time large 3 sec
* error and exit !
*/
time_out_comm = GetTimeOut ( "COMPOS" ) ;
alarm ( time_out_comm ) ;
current ++;
for ( i = 0; i > 4 ) * 10 + ( * current & 0x0f );
current --;
lenth += ( * current >> 4 ) * 1000 + ( * current & 0x0f ) * 100;
current += 2;
ErrorLog ( TRACE, "lenth = %d", lenth );
if ( lenth >= 512 ) {
* current = 0x0;
alarm ( 0 ) ;
return ( -1 ) ;
}
for ( i = 0; i