100分求教:如何通过API函数向串口发送16进制数字
我想往串口发送3个16进制数字:0xeb,0x87,0x90,请问如果用WriteComm这个函数的话该什么去写,VC里的16进制是什么类型的?BYTE吗??
谢谢了
问题点数:0、回复次数:2Top
1 楼zfive5(醉马不肖)回复于 2004-05-03 20:39:44 得分 0
BYTE
WriteFile(...)Top
2 楼zhangnanonnet(鱼欢)回复于 2004-05-03 20:52:24 得分 0
byte a[3];
a[0]=0x12;
a[1]=0x13;
ZComm::Write(BYTE *buf,int buflen)
{
BOOL fWriteStat;
DWORD dwErrorFlags;
DWORD dwError;
COMSTAT ComStat;
char szError[ 10 ] ;
DWORD ret;
if (fCOMMOpened==0) return -2 ;//串口未打开
ret=0;
if (olap)
{
fWriteStat = WriteFile(idComDev,buf,buflen,&ret, &WRITE_OS ) ;
if (!fWriteStat)
{
if ((dwError=GetLastError()) == ERROR_IO_PENDING)
{
while (!GetOverlappedResult(idComDev,&WRITE_OS,&ret, TRUE ))
{
dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
{
OutputDebugString("write io pending");
continue;
}
else
{
//an error occurred, try to recover
wsprintf( szError, "\n\r <CE-%u>", dwError ) ;
OutputDebugString(szError);
ClearCommError(idComDev, &dwErrorFlags, &ComStat ) ;
if (dwErrorFlags > 0)
{
wsprintf( szError, "\n\r <CE-%u>",dwErrorFlags ) ;
OutputDebugString(szError);
}
break;
}
}
}
else
{
// some other error occurred
wsprintf( szError, "\n\r <CE-%u>", dwError ) ;
OutputDebugString(szError);
ClearCommError(idComDev,&dwErrorFlags,&ComStat ) ;
if (dwErrorFlags>0)
{
wsprintf( szError, "\n\r <CE-%u>", dwErrorFlags) ;
OutputDebugString(szError);
}
return ( FALSE );
}
}
}
else
{
fWriteStat = WriteFile(idComDev, buf,(DWORD)buflen,&ret, NULL);
if (!fWriteStat)
{
if(GetLastError() == ERROR_IO_PENDING)
{
dwError = GetLastError();
// an error occurred, try to recover
wsprintf( szError, "\n\r <CE-%u>", dwError ) ;
OutputDebugString(szError);
ClearCommError(idComDev, &dwErrorFlags, &ComStat ) ;
if (dwErrorFlags >0)
{
wsprintf( szError, "\n\r <CE-%u>", dwErrorFlags ) ;
OutputDebugString(szError);
}
}
else
{
// some other error occurred
ClearCommError(idComDev, &dwErrorFlags, &ComStat ) ;
if (dwErrorFlags > 0)
{
wsprintf( szError, "\n\r <CE-%u>", dwErrorFlags ) ;
OutputDebugString(szError);
}
return ( FALSE );
}
}
}
return (ret);
}Top




