怎样在dll中使用API函数实现串口和UB口通信(200分)

  • 主题发起人 主题发起人 zh06j08w
  • 开始时间 开始时间
Z

zh06j08w

Unregistered / Unconfirmed
GUEST, unregistred user!
最好有现成的代码,使用无窗体的其他控件也行
 
串口的 PCOMM 啊,多专业多经典的dll啊[:D]
 
www.delphibox.com上有串口和USB的通讯控件
 
可我在dll中不能使用窗体控件啊
 
#include "stdafx.h"
#include <stdio.h>
HANDLE hcom;
DWORD nobr;
DWORD nobw;
COMMTIMEOUTS comTimeOuts;

DCB dcb;
DWORD dwEvtMask;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}

void init(void)
{

//set COMMTIMEOUTS
comTimeOuts.ReadIntervalTimeout = MAXDWORD;
comTimeOuts.ReadTotalTimeoutConstant = 0;
comTimeOuts.ReadTotalTimeoutMultiplier = 0;
comTimeOuts.WriteTotalTimeoutConstant = 5000;
comTimeOuts.WriteTotalTimeoutMultiplier = 50;
SetCommTimeouts(hcom , &comTimeOuts);

GetCommState(hcom, &dcb);

dcb.BaudRate = CBR_38400;
dcb.fBinary = true;
dcb.fParity = false;
dcb.fOutxCtsFlow = false;
dcb.fOutxDsrFlow = false;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fDsrSensitivity = false;
dcb.fTXContinueOnXoff = false;
dcb.fOutX = false;
dcb.fInX = false;
dcb.fErrorChar = false;
dcb.fNull = false;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fAbortOnError = false;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;

SetCommState(hcom, &dcb);

}

CL800DEV_API bool __stdcall XCreateFile(char* fileName)
{
hcom = CreateFile(fileName,
GENERIC_READ |GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);

if( hcom == INVALID_HANDLE_VALUE)
return false;
else
init();
return true;

}

CL800DEV_API int __stdcall XCloseHandle(void)
{
if(hcom)
{
CloseHandle(hcom);
return true;
}return false;
}


CL800DEV_API int __stdcall XWriteFile(LPCVOID lpBuffer,
DWORD nNumberOfBytesToWrite,
LPDWORD lpNumberOfBytesWritten)
{

return WriteFile(hcom, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten,NULL);

}
CL800DEV_API int __stdcall XReadFile(LPVOID lpBuffer,
DWORD nNumberOfBytesToRead,
LPDWORD lpNumberOfBytesRead)
{
return ReadFile(hcom, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead , NULL);
}
 
谢谢各位大哥
有没有使用delphi的代码
 
多人接受答案了。
 
来如风大哥给分配差了,请多担待
 
后退
顶部