|
波特率是115200,写260个字节左右,占用50毫秒到1000毫秒不等的时间,按计算发送完应该是22.6毫秒左右,不至于那么多。
相关的代码:
/**
open serial
@param COMx: eg:_T("COM1")
@param BaudRate:
return 0 success ,return Negative is haed err
*/
int Serial_open(LPCWSTR COMx,int BaudRate)
{
DCB dcb={0};
COMMTIMEOUTS CommTimeOuts;
hCom =CreateFile(COMx,
GENERIC_READ|GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,//FILE_FLAG_OVERLAPPED, //同步方式 或 重叠方式
0
);
if(hCom ==INVALID_HANDLE_VALUE)
{
DWORD dwError=GetLastError();
return -1;
}
dcb.DCBlength = sizeof(DCB);
if(!GetCommState(hCom,&dcb))
{
DWORD dwError=GetLastError();
return -1;
}
dcb.BaudRate = BaudRate; //波特率
dcb.ByteSize = 8; //位数
dcb.Parity = NOPARITY; //奇偶检验
dcb.StopBits =ONESTOPBIT; //停止位数
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
if(!SetCommState(hCom,&dcb))
{
DWORD dwError=GetLastError();
return -1;
}
if( !PurgeComm( hCom, PURGE_RXCLEAR ) ) return -1;
SetCommMask(hCom,EV_RXCHAR); //串口事件:接收到一个字符
GetCommTimeouts(hCom,&CommTimeOuts);
CommTimeOuts.ReadIntervalTimeout=10; //接收字符间最大时间间隔;原值100
CommTimeOuts.ReadTotalTimeoutMultiplier=10;
CommTimeOuts.ReadTotalTimeoutConstant=10; //读数据总超时常量;原值100
CommTimeOuts.WriteTotalTimeoutMultiplier=0;
CommTimeOuts.WriteTotalTimeoutConstant=0;
if(!SetCommTimeouts(hCom,&CommTimeOuts))
{
printf("无法设置超时参数!\n");
CloseHandle(hCom);
return -1;
}
SetupComm(hCom,4096,4096);
return 0;
}
/**
serial read
@param Buf:data buf
@param size:
@return The len of read
*/
int Serial_read(void *OutBuf,int size)
{
DWORD cnt=0;
ReadFile(hCom,OutBuf,size,&cnt,0);
return cnt;
}
/**
serial write
@param Buf:data buf
@param size:bytes of Buf
@return The len of writen
*/
int Serial_write(const void *Buf,int size)
{
DWORD dw;
WriteFile(hCom,Buf,size,&dw,NULL);
return dw;
}
/**
serial close
*/
void Serial_close(void)
{
CloseHandle(hCom);
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
|