因为是作业,不能用MSCOMM,自己没什么头绪,请问有没有这方面的资料或程序可以参考,谢谢
解决方案 »
- 遍历文件夹进行训练
- 与CRect rect; 如何让rect的位置与CRect(1,1,166,52) 一样?
- 关于字符指针与16进制 问题 在线等
- 遍历U盘的时候出现了问题 急急急
- 关于COM的使用:不知道interface可以使用COM吗?
- 接上问,继续请教 vcmute(横秋)有关IMM的问题.
- Dll的问题,load成功但得不到函数
- 要监视进程的产生和销毁,谁给我个例子?谢谢
- 没有人能回答这个问题吗?? ATL中使用ActiveX 问题. 大家真的要救救我啊.......
- 有没有ScrollText的源码(C++)?
- ActiveX控件的事件在asp客户端脚本中为何不被触发?
- 那位大虾知道如何用DAO读取ACCEEE表中的数字型字段值,100分送-------急!!!
{
public:
int n_DecToBCD( char ch_Dec[2] );
CString str_ReadAddress( HANDLE h_Comm = NULL );
CString str_ReadCom( HANDLE h_Comm = NULL );
int n_DecToBCD( char ch_Dec =0 );
BOOL bln_SetPwd( HANDLE h_Comm = NULL , CString str_Pwd = "star" );
BOOL bln_IsOpen( HANDLE h_Comm = NULL );
BOOL bln_Flag;
CString str_ReadCom( CString str_Command = "" , CString str_Address = "" , HANDLE hComm=NULL );
BOOL bln_SendData( CString str_Data="" , CString str_Address="" , HANDLE h_Comm=NULL);
BOOL bln_SendData( CString str_AllData="" , HANDLE h_Comm=NULL);
void CloseCom( HANDLE &h_Comm );
HANDLE OpenCom( CString str_ComName="COM1" , int n_BaudRate =2400 , int n_Parity = 2 );
OVERLAPPED ol_WriteRead;
HANDLE hComm;
CCom();
virtual ~CCom();};
{
}CCom::~CCom()
{}HANDLE CCom::OpenCom(CString str_ComName, int n_BaudRate, int n_Parity)
{
HANDLE hComm_Temp;
CRegSet m_RegSet;
DCB dcb; if( str_ComName.IsEmpty() ){
str_ComName = m_RegSet.str_ReadKey( "szCom" );
}
hComm = CreateFile( str_ComName , GENERIC_READ | GENERIC_WRITE , 0 , NULL ,
OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED , NULL );
if( hComm == NULL ) return NULL;
hComm_Temp = hComm; bln_Flag = TRUE; memset( &ol_WriteRead , 0 , sizeof( OVERLAPPED ) ); COMMTIMEOUTS CommTimeOuts;
CommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
CommTimeOuts.ReadTotalTimeoutMultiplier = 0;
CommTimeOuts.ReadTotalTimeoutConstant = 0;
CommTimeOuts.WriteTotalTimeoutMultiplier = 0;
CommTimeOuts.WriteTotalTimeoutConstant = 5000;
SetCommTimeouts( hComm, &CommTimeOuts ); ol_WriteRead.hEvent = CreateEvent( NULL, TRUE, FALSE, NULL ); dcb.DCBlength = sizeof( DCB );
GetCommState( hComm, &dcb );
dcb.BaudRate = n_BaudRate; if( m_RegSet.str_ReadKey( "szBPS" ) == "1200" )
dcb.BaudRate = 1200;
if( m_RegSet.str_ReadKey( "szBPS" ) == "2400" )
dcb.BaudRate = 2400;
if( m_RegSet.str_ReadKey( "szBPS" ) == "3600" )
dcb.BaudRate = 3600;
if( m_RegSet.str_ReadKey( "szBPS" ) == "4800" )
dcb.BaudRate = 4800;
if( m_RegSet.str_ReadKey( "szBPS" ) == "7200" )
dcb.BaudRate = 7200;
if( m_RegSet.str_ReadKey( "szBPS" ) == "9600" )
dcb.BaudRate = 9600; dcb.ByteSize = 8;
dcb.Parity = n_Parity; //EVENPARITY; //MARKPARITY 偶校验
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = TRUE ;
dcb.fParity = FALSE; //FALSE; //TRUE; if( !SetCommState( hComm, &dcb ) || !SetupComm( hComm, 10000, 10000 ) ||
ol_WriteRead.hEvent == NULL || ol_WriteRead.hEvent == NULL ){ DWORD dwError = GetLastError();
if( ol_WriteRead.hEvent != NULL ) CloseHandle( ol_WriteRead.hEvent );
CloseHandle( hComm );
} return hComm_Temp;
}void CCom::CloseCom(HANDLE &h_Comm)
{
if( h_Comm == NULL )
CloseHandle( hComm );
else
CloseHandle( h_Comm );
bln_Flag = FALSE;
}
BOOL CCom::bln_SendData(CString str_Data,CString str_Address,HANDLE h_Comm)
{
BOOL bln_Result = FALSE;
DWORD dw_BytesSead;
unsigned char ch_Data[128] = {0};
unsigned char ch_TempData[3] = {0};
int n_Loop = 0;
int n_DataLength = 0; if( str_Address.IsEmpty() ){
ch_Data[0] = 0x68;
ch_Data[1] = 0x99;
ch_Data[2] = 0x99;
ch_Data[3] = 0x99;
ch_Data[4] = 0x99;
ch_Data[5] = 0x99;
ch_Data[6] = 0x99;
ch_Data[7] = 0x68;
}else{
ch_Data[0] = 0x68; //此处要求至少长度是6位的表地址,否则程序会报错
ch_Data[1] = str_Address.GetAt(0); //是否需要在这里减去48,以后人考虑吧。
ch_Data[2] = str_Address.GetAt(1);
ch_Data[3] = str_Address.GetAt(2);
ch_Data[4] = str_Address.GetAt(3);
ch_Data[5] = str_Address.GetAt(4);
ch_Data[6] = str_Address.GetAt(5);
ch_Data[7] = 0x68;
} for( n_Loop = 0; n_Loop < str_Data.GetLength(); n_Loop ++ ){
ch_Data[ 8 + n_Loop ] = str_Data.GetAt( n_Loop );
} n_DataLength = 8 + str_Data.GetLength();
ch_Data[n_DataLength] = 0;
for( n_Loop = 0; n_Loop < n_DataLength; n_Loop ++ )
ch_Data[n_DataLength] = ch_Data[n_DataLength] + ch_Data[n_Loop];
ch_Data[n_DataLength] = ch_Data[n_DataLength]%256;
ch_Data[ n_DataLength + 1 ] = 0x16;
//CString str_OOO="";
//str_OOO = ch_Data;
//MessageBox( 0 , str_OOO , "0" , 0 );//输出查看一下是否正确
ch_TempData[0] = 0xFE;
ch_TempData[1] = 0xFE; bln_Result = WriteFile( hComm , ch_TempData , 1 , &dw_BytesSead , &ol_WriteRead );
::Sleep( 6 );
bln_Result = WriteFile( hComm , ch_TempData , 1 , &dw_BytesSead , &ol_WriteRead ); n_DataLength = n_DataLength + 2;
bln_Result = WriteFile( hComm , ch_Data , n_DataLength , &dw_BytesSead , &ol_WriteRead ); //多字节发送 if( !bln_Result ){
if ( GetLastError() == ERROR_IO_PENDING ) {
WaitForSingleObject( ol_WriteRead.hEvent, 1000 );
bln_Result = TRUE;
}else{
bln_Result = FALSE;
}
}else{
bln_Result = TRUE;
} return bln_Result;
}
{
BOOL bln_Result = FALSE;
DWORD dw_BytesSead;
unsigned char ch_Data[128] = {0};
unsigned char ch_TempData[3] = {0};
int n_Loop = 0 , n_Start = 0;
int n_DataLength = 0; if( str_AllData.GetLength() < 8 ){
ch_Data[0] = 0x68;
ch_Data[1] = 0x99;
ch_Data[2] = 0x99;
ch_Data[3] = 0x99;
ch_Data[4] = 0x99;
ch_Data[5] = 0x99;
ch_Data[6] = 0x99;
ch_Data[7] = 0x68;
n_Start = 8;
}else{
if(( str_AllData.GetAt(0) != 0x68 ) && ( str_AllData.GetAt(7) != 0x68 )) {
ch_Data[0] = 0x68;
ch_Data[1] = 0x99;
ch_Data[2] = 0x99;
ch_Data[3] = 0x99;
ch_Data[4] = 0x99;
ch_Data[5] = 0x99;
ch_Data[6] = 0x99;
ch_Data[7] = 0x68;
n_Start = 8;
}
}
for( n_Loop = 0; n_Loop < str_AllData.GetLength(); n_Loop ++ ){
ch_Data[ n_Start + n_Loop ] = str_AllData.GetAt( n_Loop );
} ch_TempData[0] = 0xFE;
ch_TempData[1] = 0xFE; bln_Result = WriteFile( hComm , ch_TempData , 1 , &dw_BytesSead , &ol_WriteRead );
::Sleep( 10 );
bln_Result = WriteFile( hComm , ch_TempData , 1 , &dw_BytesSead , &ol_WriteRead ); ::Sleep( 2 );
for( int i=0; i<str_AllData.GetLength(); i++){
ch_Data[0] = str_AllData.GetAt( i );
bln_Result = WriteFile( hComm , ch_Data , 1 , &dw_BytesSead , &ol_WriteRead ); //多字节发送
::Sleep( 2 );
}
//n_DataLength = n_Start + str_AllData.GetLength();
//bln_Result = WriteFile( hComm , ch_Data , n_DataLength , &dw_BytesSead , &ol_WriteRead ); //多字节发送 if( !bln_Result ){
if ( GetLastError() == ERROR_IO_PENDING ) {
WaitForSingleObject( ol_WriteRead.hEvent, 1000 );
bln_Result = TRUE;
}else{
bln_Result = FALSE;
}
}else{
bln_Result = TRUE;
}
return bln_Result;
}
CString CCom::str_ReadCom(CString str_Command, CString str_Address,HANDLE h_Comm)
{
BOOL bln_Result = FALSE;
DWORD dw_BytesRead;
DWORD dw_Res;
unsigned char ch_Data[128] = {0}; bln_Result = ReadFile( hComm , ch_Data , 1 , &dw_BytesRead , &ol_WriteRead );
bln_Result = ReadFile( hComm , ch_Data , 1 , &dw_BytesRead , &ol_WriteRead );
Sleep( 50 ); memset( ch_Data , 0 , 128 );
bln_Result = ReadFile( hComm , ch_Data , 10 , &dw_BytesRead , &ol_WriteRead );
if( !bln_Result ){
if( GetLastError() == ERROR_IO_PENDING ){
dw_Res = WaitForSingleObject( ol_WriteRead.hEvent, 2000 );
return "";
}
return "";
}
return ch_Data;
}BOOL CCom::bln_IsOpen(HANDLE h_Comm)
{
return bln_Flag;
}BOOL CCom::bln_SetPwd(HANDLE h_Comm,CString str_Pwd)
{ return TRUE;
}int CCom::n_DecToBCD(char ch_Dec)
{
int n_Return = 0;
int x = 0, y = 0; x = ch_Dec/10;
y = ch_Dec%10; n_Return = x*16 + y; return n_Return;
}int CCom::n_DecToBCD(char ch_Dec[])
{
int n_Temp[2] = { 0 };
int n_Return = 0;
if(( ch_Dec[0] == 'A' ) || ( ch_Dec[0] == 'a' ))
n_Temp[0] = 10;
if(( ch_Dec[0] == 'B' ) || ( ch_Dec[0] == 'b' ))
n_Temp[0] = 11;
if(( ch_Dec[0] == 'C' ) || ( ch_Dec[0] == 'c' ))
n_Temp[0] = 12;
if(( ch_Dec[0] == 'D' ) || ( ch_Dec[0] == 'd' ))
n_Temp[0] = 13;
if(( ch_Dec[0] == 'E' ) || ( ch_Dec[0] == 'e' ))
n_Temp[0] = 14;
if(( ch_Dec[0] == 'F' ) || ( ch_Dec[0] == 'f' ))
n_Temp[0] = 15;
if(( ch_Dec[0] >= 48 ) && ( ch_Dec[0] <= 57 ))
n_Temp[0] = ch_Dec[0] - 48; if(( ch_Dec[1] == 'A' ) || ( ch_Dec[1] == 'a' ))
n_Temp[1] = 10;
if(( ch_Dec[1] == 'B' ) || ( ch_Dec[1] == 'b' ))
n_Temp[1] = 11;
if(( ch_Dec[1] == 'C' ) || ( ch_Dec[1] == 'c' ))
n_Temp[1] = 12;
if(( ch_Dec[1] == 'D' ) || ( ch_Dec[1] == 'd' ))
n_Temp[1] = 13;
if(( ch_Dec[1] == 'E' ) || ( ch_Dec[1] == 'e' ))
n_Temp[1] = 14;
if(( ch_Dec[1] == 'F' ) || ( ch_Dec[1] == 'f' ))
n_Temp[1] = 15;
if(( ch_Dec[1] >= 48 ) && ( ch_Dec[1] <= 57 ))
n_Temp[1] = ch_Dec[1] - 48; n_Return = ( n_Temp[0] * 16 ) + n_Temp[1] + 51;
n_Return = n_Return%256;
return n_Return;
}CString CCom::str_ReadCom(HANDLE h_Comm)
{
BOOL bln_Result = FALSE;
DWORD dw_BytesRead;
unsigned char ch_Data[128] = {0}; if( h_Comm == NULL ){
h_Comm = hComm;
}
memset( ch_Data , 0 , 128 );
::Sleep( 300 ); bln_Result = ReadFile( h_Comm , ch_Data , 50 , &dw_BytesRead , &ol_WriteRead );
if( !bln_Result ){
if( GetLastError() == ERROR_IO_PENDING ){
DWORD dwRes = WaitForSingleObject( ol_WriteRead.hEvent, 2000 );
switch( dwRes ){
case WAIT_OBJECT_0:
break;
case WAIT_TIMEOUT:
break;
}
return "";
}
return "";
} if( ch_Data[8] != 0x81 ){
//MessageBox( 0 , "可能Error03" , "0" , 0 );
//return "";
}
if( ch_Data[8] == 0xC1 ){
//MessageBox( 0 , "可能Error发现" , "0" , 0 );
//return "";
} /*
for( int i=0; i<128; i++ ){
ch_Data[i] = ch_Data[i+1];
if( ch_Data[i+1] == 0x16 ){
ch_Data[i+1] = 0;
break;
}
}
for( i=10; i<( 10 + ch_Data[9] ); i++ ){
ch_Data[i] = ch_Data[i] - 0x33;
if( ch_Data[i+2] == 0x16 )
break;
}
*/ return ch_Data;
}CString CCom::str_ReadAddress(HANDLE h_Comm)
{
BOOL bln_Result = FALSE;
DWORD n_BytesRead;
DWORD dwRes;
unsigned char ch_Data[17] = {0};
unsigned char ch_TempData[8] = {0};
unsigned char ch_AllData[150] = {0}; if( h_Comm == NULL ){
h_Comm = hComm;
} ch_Data[0] = 0x68;
ch_Data[1] = 0x99;
ch_Data[2] = 0x99;
ch_Data[3] = 0x99;
ch_Data[4] = 0x99;
ch_Data[5] = 0x99;
ch_Data[6] = 0x99;
ch_Data[7] = 0x68; ch_Data[8] = 0x01;
ch_Data[9] = 0x02;
ch_Data[10] = 0x65;
ch_Data[11] = 0xF3;
ch_Data[12] = 0xC1;
ch_Data[13] = 0x16; ch_TempData[0] = 0xFE;
ch_TempData[1] = 0xFE;
bln_Result = WriteFile( h_Comm , ch_TempData , 1 , &n_BytesRead , &ol_WriteRead );
::Sleep( 10 );
bln_Result = WriteFile( h_Comm , ch_TempData , 1 , &n_BytesRead , &ol_WriteRead ); ::Sleep( 2 );
ch_TempData[0] = 0x00;
ch_TempData[1] = 0x00;
for( int i=0; i<14; i++){
ch_TempData[0] = ch_Data[i];
bln_Result = WriteFile( h_Comm , ch_TempData , 1 , &n_BytesRead , &ol_WriteRead );
::Sleep( 2 );
}
//bln_Result = WriteFile( h_Comm , ch_Data , 16 , &n_BytesRead , &ol_WriteRead );
if( !bln_Result ){
if ( GetLastError() == ERROR_IO_PENDING ) {
WaitForSingleObject( ol_WriteRead.hEvent, 1000 );
}else{
return "";
}
} ::Sleep( 300 );
i = 0;
// for( i=0; i<4; i++ ){
bln_Result = ReadFile( h_Comm , ch_Data , 1 , &n_BytesRead , &ol_WriteRead );
::Sleep( 1 ); memset( ch_Data , 0 , 17 );
ch_TempData[0] = 0x00;
ch_TempData[1] = 0x00;
for( int j=0; j<12; j++ ){
bln_Result = ReadFile( h_Comm , ch_TempData , 1 , &n_BytesRead , &ol_WriteRead );
ch_Data[j] = ch_TempData[0];
::Sleep( 1 );
}
//bln_Result = ReadFile( h_Comm , ch_Data , 12 , &n_BytesRead , &ol_WriteRead );
if( !bln_Result ){
if( GetLastError() == ERROR_IO_PENDING ){
dwRes = WaitForSingleObject( ol_WriteRead.hEvent, 2000 );
switch( dwRes ){
case WAIT_OBJECT_0:
break;
case WAIT_TIMEOUT:
break;
}
return "";
}
return "";
} ch_AllData[( 7 * i ) + 0 ] = ch_Data[1];
ch_AllData[( 7 * i ) + 1 ] = ch_Data[2];
ch_AllData[( 7 * i ) + 2 ] = ch_Data[3];
ch_AllData[( 7 * i ) + 3 ] = ch_Data[4];
ch_AllData[( 7 * i ) + 4 ] = ch_Data[5];
ch_AllData[( 7 * i ) + 5 ] = ch_Data[6];
ch_AllData[( 7 * i ) + 6 ] = ','; if( ch_Data[8] == 0x68 ){
//异常应答
}
if( ch_Data[8] != 0x81 ){
int n_length = strlen( (char *)ch_AllData );
ch_AllData[ n_length - 1 ] = 0;
ch_AllData[ n_length - 2 ] = 0;
ch_AllData[ n_length - 3 ] = 0;
ch_AllData[ n_length - 4 ] = 0;
ch_AllData[ n_length - 5 ] = 0;
ch_AllData[ n_length - 6 ] = 0;
ch_AllData[ n_length - 7 ] = 0;
ch_AllData[ n_length - 8 ] = 0;
return ch_AllData;
}else{
int n_length = strlen( (char *)ch_AllData );
ch_AllData[ n_length - 1 ] = 0;
}
// } return ch_AllData;
}
这个网页上有很多实用的东西,我学习串口时就是看的这个网页