|
新手写的wince下的串口驱动,请大家看看驱动的框架是否是这样写,在XXX_Init()中的代码是否正确,是不是在在这里使用createfile()
有关程序逻辑的错误可先不讨论
#include
#include
#include
#include
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
DEBUGMSG(1, (TEXT( "DLL_PROCESS_ATTACH\r\n" )) );
break;
case DLL_PROCESS_DETACH:
DEBUGMSG(1, (TEXT( "DLL_PROCESS_DETACH\r\n" )) );
break;
case DLL_THREAD_ATTACH:
DEBUGMSG(1, (TEXT( "DLL_THREAD_ATTACH\r\n" )) );
break;
case DLL_THREAD_DETACH:
DEBUGMSG(1, (TEXT( "DLL_THREAD_DETACH\r\n" )) );
break;
}
DEBUGMSG(1, (TEXT( "Go DllMain\r\n" )) );
return TRUE;
}
extern "C" __declspec(dllexport)
DWORD SCA_Init(DWORD dwContext,LPCVOID lpvBusContext)
{
// MessageBox(NULL,TEXT("初始化SCA_Init"),TEXT("初始化SCA_Init"),0);
//1.打开指定串口
HANDLE hComm;
hComm = CreateFile(_T("COM1:"), // 串口名称(COMx)
GENERIC_READ | GENERIC_WRITE, // 串口属性为可读/写
0, // 串口设备必须被独占性的访问
NULL, // 无安全属性
OPEN_EXISTING, // 串口设备必须使用OPEN_EXISTING参数
FILE_ATTRIBUTE_NORMAL, // 同步式 I/O
0); // 对于串口设备而言此参数必须为0
if (hComm == INVALID_HANDLE_VALUE)
{
return 0;
}
//2.设置串口参数:波特率、数据位、校验位、停止位等信息
DCB dcb;
GetCommState(hComm, &dcb); //获取该端口的默认参数
//修改波特率
dcb.BaudRate = 115200;
dcb.fBinary=0;
//重新设置参数
SetCommState(hComm, &dcb);
SetupComm(hComm,//通信设备句柄DWORD
1024,//输入缓冲区大小DWORD
1024//输出缓冲区大小
);
// _beginthread( CheckKey, 0, NULL );
BYTE Byte;
int iCounter = 0;
DWORD dwBytes;
char ReceiveBuf[1000]; ///缓冲区的大小
SetCommMask(hComm, EV_RXCHAR); ///只接收字符
while(hComm != INVALID_HANDLE_VALUE)
{
DWORD dwCommStatus;
WaitCommEvent(hComm, &dwCommStatus, 0);
SetCommMask(hComm, EV_RXCHAR); ///重新设置要等待的信号
// 接收数据
do
{
ReadFile(hComm,&Byte,1,&dwBytes,0);
if(dwBytes==1)
{
keybd_event(int(Byte), 0, 0, 0); //VK_F1
keybd_event(int(Byte), 0, KEYEVENTF_KEYUP, 0 );
ReceiveBuf[iCounter++]=Byte;
if(iCounter==1000)
{
break;
}
}
}while(dwBytes==1);
}
return 1;
}
extern "C" __declspec(dllexport)
BOOL SCA_Deinit(DWORD hDeviceContext)
{
BOOL bReturn = TRUE;
return bReturn;
}
extern "C" __declspec(dllexport)
DWORD SCA_Open(DWORD hDeviceContext,
DWORD AccessCode,
DWORD ShareMode)
{
DWORD dwReturn=0;
return dwReturn;
}
extern "C" __declspec(dllexport)
BOOL SCA_CLOSE(DWORD hOpenContext)
{
BOOL bReturn=TRUE;
if(hComm != NULL)
{
CloseHandle(hComm);
hComm = NULL;
}
return bReturn;
}
extern "C" __declspec(dllexport)
BOOL SCA_IOControl(DWORD hOpenContext,
DWORD dwCode,
PBYTE pBufIn,
DWORD dwLenIn,
PBYTE pBufOut,
DWORD dwLenOut,
PDWORD pdwActualOut
)
{
BOOL bReturn=TRUE;
return bReturn;
}
extern "C" __declspec(dllexport)
void SCA_PowerDown(DWORD hDeviceContext)
{
}
extern "C" __declspec(dllexport)
void SCA_PowerUp(DWORD hDeviceContext)
{
}
extern "C" __declspec(dllexport)
DWORD SCA_Read(DWORD hOpenContext,
LPVOID pBuffer,
DWORD Count
)
{
return 1;
}
extern "C" __declspec(dllexport)
DWORD SCA_Seek(DWORD hOpenContext,
LONG Amount,
DWORD Type
)
{
return 0;
}
extern "C" __declspec(dllexport)
DWORD SCA_Write(DWORD hOpenContext,
LPVOID pSourceBytes,
DWORD NumberOfBytes
)
{
return 0;
}
|
|