我写的程序是从linux发数据到win!在win下写的串口程序好像要接受到两个字符才能返回!
我的代码如下: while((new_time - old) < delay_time)
{
read_port( buffer, 1, &res);
new_time = time((time_t *)NULL); if( res <= 0) continue;
if( buffer[0] == 0x15) break; }其中read_port的定义如下!
read_port(unsigned char *read_buffer, unsigned int read_buffer_length, unsigned long *length)
{
if( ReadFile(serial_handle, // handle of file to read
read_buffer, //
read_buffer_length, //size of buffer to read
length,
NULL) == 0)
{
AfxMessageBox(_T("wrong in reading of serial communication"));
return FALSE;
} return TRUE;
}我当单独在linux下发送0x15(这是1BYTE的数据),则无法接受到!随便加一个BYTE例如发两BYTE,第一个BYTE是0x15则可以接收到~到底是为什么呢?
我的代码如下: while((new_time - old) < delay_time)
{
read_port( buffer, 1, &res);
new_time = time((time_t *)NULL); if( res <= 0) continue;
if( buffer[0] == 0x15) break; }其中read_port的定义如下!
read_port(unsigned char *read_buffer, unsigned int read_buffer_length, unsigned long *length)
{
if( ReadFile(serial_handle, // handle of file to read
read_buffer, //
read_buffer_length, //size of buffer to read
length,
NULL) == 0)
{
AfxMessageBox(_T("wrong in reading of serial communication"));
return FALSE;
} return TRUE;
}我当单独在linux下发送0x15(这是1BYTE的数据),则无法接受到!随便加一个BYTE例如发两BYTE,第一个BYTE是0x15则可以接收到~到底是为什么呢?
解决方案 »
- 数据库连接问题(救急)!!
- 关于VC界面开发的问题。具体也不好描述。请高手帮忙解答下【十万火急】
- BCG 在vc6上怎么用?或者大家推荐一下好的界面优化方法,,
- 用WinInet作HTTP访问遇到的问题
- 在MFC程序中使用Windows Midea Player 9的控件,怎么控制它播放一个AVI文件?奇了怪了.
- 请问各位VC大侠,怎么用VC编图像采集卡或者摄像头的驱动程序?
- 文件打开的时候碰到的一个问题
- vc可以算出(0.01+0.02)*1000=29来,不相信你试试运行下面的代码,怎么回事?怎么解决?
- how to set enviroment variable
- 谁能给想学VC的朋友一点建议
- 远程控制 如何发送按钮按下的消息
- 关于VC操作access数据库的问题
{
serial_handle = CreateFile( serial_port,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL); if( serial_handle == (HANDLE)0xFFFFFFFF)
{
AfxMessageBox("Open the serial port wrong");
return FALSE;
} if( GetCommState(serial_handle, &config) == 0)
{
AfxMessageBox(_T("Wrong in get configuration port."));
return FALSE;
} if( SetCommState(serial_handle, &config) == 0)
{
AfxMessageBox(_T("Wrong in set config of serial port"));
return FALSE;
} //set the buffer of sending and reciving
SetupComm(serial_handle, 1024, 1024);
PurgeComm(serial_handle, PURGE_TXCLEAR);
//set the time out parity
COMMTIMEOUTS com_time_out;
memset(&com_time_out, 0, sizeof(com_time_out));
GetCommTimeouts(serial_handle, &com_time_out);//get the time out set now //set the read time out
//return at once while get one charater
com_time_out.ReadIntervalTimeout = MAXDWORD;
com_time_out.ReadTotalTimeoutConstant = 0;
com_time_out.ReadTotalTimeoutMultiplier = 0; //set the write time out
com_time_out.WriteTotalTimeoutConstant = 2;
com_time_out.WriteTotalTimeoutMultiplier = 3; //set the time out parity SetCommTimeouts(serial_handle, &com_time_out); return TRUE;
}我串口设置参数是按照以上设置的!麻烦您看看,不知道哪错了!我应该没有加握手吧~
可能WriteTotalTimeoutConstant设置的太小而导致写入超时.把它设置的大点.比如3000;