// QZSerialServer.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include "QZSerialServer.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern HANDLE m_hWriteSerialEvent;
extern bool writethreadmutex;
HANDLE m_hMMObj = 0; // Memory-mapped object
pCommandVar m_pBuff = 0; // Pointer to mm object
HANDLE m_hmWriteOkay = 0; // Write mutex
HANDLE m_hComeDataEvent = 0; // 要求写串口时的事件
HANDLE m_hComeDataReadEndEvent = 0; // 完成写串口时的事件
UINT ReadComeDataThread(LPVOID);
//
// Note!
//
// If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
//
// It is very important that this macro appear in each
// function, prior to any calls into MFC. This means that
// it must appear as the first statement within the
// function, even before any object variable declarations
// as their constructors may generate calls into the MFC
// DLL.
//
// Please see MFC Technical Notes 33 and 58 for additional
// details.
//
/////////////////////////////////////////////////////////////////////////////
// CQZSerialServerApp
BEGIN_MESSAGE_MAP(CQZSerialServerApp, CWinApp)
//{{AFX_MSG_MAP(CQZSerialServerApp)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CQZSerialServerApp construction
CQZSerialServerApp::CQZSerialServerApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CQZSerialServerApp object
CQZSerialServerApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CQZSerialServerApp initialization
BOOL CQZSerialServerApp::InitInstance()
{
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
//service interface
CMobileModule mobile;
extern CListComData_List ;
HWND hWnd;
int m_cmd=0,m_int1=0,m_int2=0,m_int3=0,m_int4=0,m_int5=0;
char m_str1[1024],m_str2[1024],m_str3[1024] ;
void GetVariable(CommandVar *CV)
{
m_cmd = CV->ATCommand;
m_int1 = CV->CV.var_int1;
m_int2 = CV->CV.var_int2;
m_int3 = CV->CV.var_int3;
m_int4 = CV->CV.var_int4;
m_int5 = CV->CV.var_int5;
strcpy(m_str1,CV->CV.var_str1);
strcpy(m_str2,CV->CV.var_str2);
strcpy(m_str3,CV->CV.var_str3);
}
extern "C" DWORD PASCAL EXPORT QZS_Init (DWORD dwData)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// Create a file-mapping object.
m_hMMObj = CreateFileMapping ((HANDLE)-1, NULL, PAGE_READWRITE, 0,
4*1024, TEXT ("DataComeBuffer"));
if (m_hMMObj == 0)
{
return 0;
}
// Map into memory the file-mapping object.
m_pBuff = (pCommandVar)MapViewOfFile (m_hMMObj, FILE_MAP_WRITE,0, 0, 0);
if (!m_pBuff)
CloseHandle (m_hMMObj);
// Initialize structure if first application started.
memset (m_pBuff, 0, sizeof (CommandVar));
// Create event
m_hComeDataEvent = CreateEvent (NULL, FALSE, FALSE, TEXT ("COMEDATAEVENT"));
m_hComeDataReadEndEvent = CreateEvent (NULL, FALSE, FALSE, TEXT ("COMEDATAREADENDEVENT"));
if (!m_hComeDataEvent || !m_hComeDataReadEndEvent)
{
return 0;
}
mobile.OpenComm(L"COM1:",38400);
printf("server begin\n");
AfxBeginThread(ReadComeDataThread,0);
return 1;
}
extern "C" BOOL PASCAL EXPORT QZS_Deinit(DWORD dwData)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return TRUE;
}
extern "C" DWORD PASCAL EXPORT QZS_Open (DWORD dwData, DWORD dwAccess, DWORD dwShareMode)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return 1;
}
extern "C" BOOL PASCAL EXPORT QZS_Close (DWORD dwData)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return TRUE;
}
extern "C" DWORD PASCAL EXPORT QZS_Write (DWORD dwData, LPCVOID pInBuf, DWORD dwInLen)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return 1;
}
extern "C" DWORD PASCAL EXPORT QZS_Read (DWORD dwData, LPVOID pBuf, DWORD dwLen)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return 1;
}
extern "C" BOOL PASCAL EXPORT
QZS_IOControl(DWORD dwData, DWORD dwCode, PBYTE pBufIn,
DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut,
PDWORD pdwActualOut) {
AFX_MANAGE_STATE(AfxGetStaticModuleState());
CString str_send;
TCHAR *tcIn=(TCHAR *)pBufIn;
switch (dwCode) {
//
// IOCTLs to handle well-defined services.exe codes
//
// Control code sent to start a service (not the same as IOCTL_SERVICE_STARTED).
case IOCTL_SERVICE_START:
break;
case IOCTL_SERVICE_REFRESH:
case IOCTL_SERVICE_STOP:
break;
case IOCTL_SERVICE_STATUS:
break;
case IOCTL_SERVICE_CONTROL:
break;
case IOCTL_SERVICE_STARTED:
break;
case IOCTL_SERVICE_REGISTER_SOCKADDR: {
return TRUE;
}
break;
case IOCTL_SERVICE_DEREGISTER_SOCKADDR:
// When our sockets are being disconnected this message is sent to us.
return TRUE;
break;
case IOCTL_SERVICE_CONNECTION:
break;
}
SetLastError(ERROR_INVALID_PARAMETER);
return FALSE;
}
extern "C" DWORD PASCAL EXPORT QZS_Seek (DWORD dwData, long pos, DWORD type)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
// We don't support seek -- return an error
return (DWORD)-1;
}
extern "C" void PASCAL EXPORT QZS_PowerUp(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return;
}
extern "C" void PASCAL EXPORT QZS_PowerDown(void)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return;
}
//end service interface
/////////////////////////////////////////////////////////////////////
UINT ReadComeDataThread( LPVOID pParam )
{
VariableList m_vList;
int nGoCode, rc;
hWnd = (HWND)pParam;
while (1)
{
printf("wait come date\n");
nGoCode = WaitForSingleObject(m_hComeDataEvent, INFINITE);
if (nGoCode == WAIT_OBJECT_0)
{
GetVariable((CommandVar*)m_pBuff);
memset(&m_vList,0,sizeof(m_vList));
m_vList.m_CommandId = m_cmd ;
m_vList.var.var_int1 = m_int1 ;
m_vList.var.var_int2 = m_int2 ;
m_vList.var.var_int3 = m_int3 ;
m_vList.var.var_int4 = m_int4 ;
m_vList.var.var_int5 = m_int5 ;
strcpy(m_vList.var.var_str1,m_str1) ;
strcpy(m_vList.var.var_str2,m_str2) ;
strcpy(m_vList.var.var_str3,m_str3) ;
ComData_List.AddHead(m_vList) ;
SetEvent(m_hWriteSerialEvent) ;
SetEvent (m_hComeDataReadEndEvent);
}
else
{
rc = GetLastError();
}
}
UnmapViewOfFile(m_pBuff);
return 1;
}