📄 opam300cdlg.cpp
字号:
// OpAm300cDlg.cpp : implementation file
//
#include "stdafx.h"
#include "OpAm300c.h"
#include "OpAm300cDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define ARRAY_LENGTH(x) (sizeof(x)/sizeof((x)[0]))
struct DIBINFO : public BITMAPINFO
{
RGBQUAD arColors[255]; // Color table info - adds an extra 255 entries to palette
operator LPBITMAPINFO() { return (LPBITMAPINFO) this; }
operator LPBITMAPINFOHEADER() { return &bmiHeader; }
RGBQUAD* ColorTable() { return bmiColors; }
};
#define IS_WIN30_DIB(lpbi) ((*(LPDWORD)(lpbi)) == sizeof(BITMAPINFOHEADER))
#define DIB_HEADER_MARKER ((WORD) ('M' << 8) | 'B')
#ifndef WIDTHBYTES
#define WIDTHBYTES(bits) ((((bits) + 31)>>5)<<2)
#endif
BOOL m_bConnected;
BOOL m_bSending;
int m_nHexCount;
CWinThread* m_pThread; ////
OVERLAPPED read_os,write_os;
HANDLE hCommDev,postRecvEvent,postSendEvent;
HWND postRecvHwnd,postSendHwnd;
UINT myThreadProc(LPVOID pParam);
DWORD iSendLen;
BOOL m_IsFile;
CFile file;
char *strSendData=new char[512];
/////////////////////////////////////////////////////////////////////////////
// COpAm300cDlg dialog
COpAm300cDlg::COpAm300cDlg(CWnd* pParent /*=NULL*/)
: CDialog(COpAm300cDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(COpAm300cDlg)
m_Order = _T("");
m_Message = _T("");
m_szSendData = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
bfile = false;
memset(resA,0,2);
memset(resD,0,9);
memset(resW,0,2);
memset(resM,0,3);
Played=false;
}
void COpAm300cDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COpAm300cDlg)
DDX_Control(pDX, IDC_EDIT3, m_m_szSendData);
DDX_Control(pDX, IDC_EDIT1, m_Edit);
DDX_Text(pDX, IDC_EDIT1, m_Order);
DDX_Text(pDX, IDC_EDIT2, m_Message);
DDX_Text(pDX, IDC_EDIT3, m_szSendData);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(COpAm300cDlg, CDialog)
//{{AFX_MSG_MAP(COpAm300cDlg)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON1, OnPlay1)
ON_BN_CLICKED(IDC_BUTTON2, OnPlay2)
ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
//}}AFX_MSG_MAP
ON_MESSAGE(WM_COMMRECVNOTIFY,OnCommRecvNotify)
ON_MESSAGE(WM_COMMSENDNOTIFY,OnCommSendNotify)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
int CALLBACK CallBackFun(HWND__ *phWnd,LPARAM lParam)
{
int ret=TRUE;
// COpAm300cDlg *pDlg=(COpAm300cDlg *)(COpAm300cDlg::FromHandle((HWND__*)lParam));
CString szText=L" ";
CWnd *pWnd=(CWnd *)(CWnd::FromHandle(phWnd));
pWnd->GetWindowText(szText);
//pDlg->m_edit+=szText;
//pDlg->m_edit+="\r\n";
//pDlg->UpdateData(false);
if (szText == L"CEAm300")
{
//pDlg->pCwnd = pWnd;
//lParam = LPARAM(pWnd);
pWnd->PostMessage(WM_USER+101, NULL, NULL);
::WaitForSingleObject(pWnd, 5000);
ret = FALSE;
}
return ret;
}
// COpAm300cDlg message handlers
BOOL COpAm300cDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
CenterWindow(GetDesktopWindow()); // center to the hpc screen
CoInitialize(NULL);
//在硬开关机状态下,会导致下位机AM300C软件读状态文件失败而异常退出的问题
//解决方法是:鉴于每次开关机都是在硬开关状态下,即每次都有可能出现这个情况
//则在检测到开机运行本程序时,自动删除错误的状态文件
//因为本程序处于常开状态,硬件开机==软件运行
DeleteFile(L"硬盘\\AM300C\\status.ini");
// DeleteFile(L"硬盘\\AM300C\\tempstatus.ini");
Sleep(50);
//运行下位机程序
StartupAm300c();
Sleep(50);
//在这里创建TCP服务线程
WSADATA wsaData;
WORD wVersionRequested;
wVersionRequested = MAKEWORD( 1, 1 );
if ( WSAStartup( wVersionRequested, &wsaData ) != 0 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
return 0;
}
if ( LOBYTE( wsaData.wVersion ) != 1 ||
HIBYTE( wsaData.wVersion ) != 1 )
{
/* Tell the user that we could not find a usable */
/* WinSock DLL. */
WSACleanup( );
return 0;
}
//网络的初始化
CFile file;
bfile = file.Open(L"硬盘\\setup.txt",CFile::modeRead,NULL);
if (bfile)
{
file.Read(fbuf,256);
file.Close();
// Add code/function to resolve condition
resolveFile(fbuf);
}
AfxBeginThread(StartSockSvr,this);
//启动一个定时器,用于实时监视时间,并重启等
SetTimer(1,60*1000,NULL);
//初始化COM口
hCommDev=NULL;
m_pThread=NULL;
m_nHexCount = 0;
m_bSending=FALSE;
m_bConnected=FALSE;
//CommPortOpen();
/*HANDLE hThread;
DWORD dwThreadId;
CreateThread(NULL,NULL,StartSockSvr,NULL,0,&dwThreadId);*/
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void COpAm300cDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
TimeResetCE();
//StartupIsError();
CDialog::OnTimer(nIDEvent);
}
BOOL COpAm300cDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
CoUninitialize();
WSACleanup( );
CloseAm300c();
return CDialog::DestroyWindow();
}
void charToWCh(TCHAR *wDstString, char *chSrcString)
{
int len;
len = strlen(chSrcString);
MultiByteToWideChar(CP_ACP, 0, chSrcString, -1, wDstString, len);
*(wDstString + len) = 0;
}
void wchToChar(char *chDstString, TCHAR *wSrcString)
{
int len1,len2;
len1 = wcslen(wSrcString);
len2 = WideCharToMultiByte(CP_ACP, 0, wSrcString, len1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, wSrcString, len1, chDstString, len2, NULL, NULL);
*(chDstString + len2) = 0;
}
/*
void wchToChar(char *chDstString, TCHAR *wSrcString)
{
int len1,len2;
len1 = wcslen(wSrcString);
len2 = WideCharToMultiByte(CP_ACP, 0, wSrcString, len1, NULL, 0, NULL, NULL);
WideCharToMultiByte(CP_ACP, 0, wSrcString, len1, chDstString, len2, NULL, NULL);
*(chDstString + len2) = 0;
}*/
/*定时重起下位机,根据下传的参数确定WINCE的重起时间*/
/*
AutoReset:() 为1 自动启动 ;0 不启动
DayReset:() 定时0为返回值,00:00:00格式设置时间
WeekReset:() 0为返回,1-7对应周一至周日,&为不设置
MonthReset:() 0为返回,1-31内设置,32为不使用
*/
//发送消息
LRESULT COpAm300cDlg::OnCommSendNotify(WPARAM wParam,LPARAM lParam)
{
COMSTAT comStat;
DWORD dwErrorFlag;
static DWORD iNum=0;
ClearCommError(hCommDev,&dwErrorFlag,&comStat);
DWORD iSendStrLen=comStat.cbOutQue;
if(iSendStrLen==0)
{
if(m_bSending)
{
if(iNum<(iSendLen/512)+1)
{
DWORD dwBytesWritten;
BOOL bReturn;
if(m_IsFile)
{
strSendData=new char[512];
file.Read(strSendData,min(512L,iSendLen-iNum*512L));
bReturn=WriteFile(hCommDev,strSendData,
min(512L,iSendLen-iNum*512),
&dwBytesWritten,&write_os);
delete[] strSendData;
}
else bReturn=WriteFile(hCommDev,strSendData+iNum*512L,
min(512L,iSendLen-iNum*512),
&dwBytesWritten,&write_os);
if(iNum==iSendLen/512)
{
m_bSending=FALSE;
iNum=0;
if(m_IsFile) file.Close();
}
else iNum++;
}
}
}
SetEvent(postSendEvent);
return TRUE;
}
//接收消息
LRESULT COpAm300cDlg::OnCommRecvNotify(WPARAM wParam,LPARAM lParam)
{
COMSTAT comStat;
DWORD iRecvStrLen,dwErrorFlag;
char *strRecvBuf;
strRecvBuf=new char[1024];
ClearCommError(hCommDev,&dwErrorFlag,&comStat);
iRecvStrLen=comStat.cbInQue;
if(iRecvStrLen>0)
{
ReadFile(hCommDev,strRecvBuf,
iRecvStrLen,&iRecvStrLen,&read_os);
// strRecvBuf.GetBufferSetLength(iRecvStrLen);
MyHandler(strRecvBuf,iRecvStrLen);
}
SetEvent(postRecvEvent);
delete[] strRecvBuf;
return TRUE;
}
//定时处理
void COpAm300cDlg::TimeResetCE()
{
int iAutoReset;
CString sDayReset;
int iWeekReset;
int iMonthReset;
//在以下把相关的配置文件的内容读入
TCHAR mychar[10];
memset(mychar,0,9);
charToWCh(&mychar[0],&resA[0]);
iAutoReset=atoi(mychar);
sDayReset=resD;
memset(mychar,0,9);
charToWCh(&mychar[0],&resW[0]);
iWeekReset=atoi(mychar);
memset(mychar,0,9);
charToWCh(&mychar[0],&resM[0]);
iMonthReset=atoi(mychar);
if (iAutoReset==1)
{
//可以进行自动重启
SYSTEMTIME T;
//GetSystemTime(&T);
memset( &T, 0, sizeof(SYSTEMTIME)) ;
GetLocalTime(&T);
if ((iMonthReset>0) && (iMonthReset<32))
{
//处理每月定的时间重启
if (T.wDay==iMonthReset)
{
//是属于当天的
CString setTime;
setTime.Format(_T("%02d:%02d:%02d"),T.wHour,T.wMinute,T.wSecond);
if (CompareString(LOCALE_SYSTEM_DEFAULT,NULL,setTime,5,sDayReset,5) == CSTR_EQUAL)
{
//重启
CloseAm300c();
//***************关闭音乐通道***************
CommPortOpen();
Mute(L"aa 05 10 00");
Mute(L"aa 06 10 00");
Sleep(1000); //等待1秒
//******************************************
PowerOff(); //挂起
Sleep(3000); //等待3秒
RebootCE(); //Reset
}
}
}
if ((iWeekReset>0) && (iWeekReset<8))
{
//有每周定时时进行的处理
if (((iWeekReset==7) && (T.wDayOfWeek==0)) || (iWeekReset== T.wDayOfWeek))
{
CString setTime;
setTime.Format(_T("%02d:%02d:%02d"),T.wHour,T.wMinute,T.wSecond);
if (CompareString(LOCALE_SYSTEM_DEFAULT,NULL,setTime,5,sDayReset,5) == CSTR_EQUAL)
{
//重启
CloseAm300c();
//***************关闭音乐通道***************
CommPortOpen();
Mute(L"aa 05 10 00");
Mute(L"aa 06 10 00");
Sleep(1000); //等待1秒
//******************************************
PowerOff(); //挂起
Sleep(3000); //等待3秒
RebootCE(); //Reset
}
}
}
if ((!((iMonthReset>0) && (iMonthReset<32))) && (!((iWeekReset>0) && (iWeekReset<8))))
{
//不属于每周定时及每月定时的,则每天进行
CString setTime;
setTime.Format(_T("%02d:%02d:%02d"),T.wHour,T.wMinute,T.wSecond);
//SYSTEMTIME stTime;
//memset( &stTime, 0, sizeof(SYSTEMTIME)) ;
//GetLocalTime(&stTime);
//setTime.Format(_T("%d:%d:%d"),stTime.wHour,stTime.wMinute,stTime.wSecond);
if (CompareString(LOCALE_SYSTEM_DEFAULT,NULL,setTime,5,sDayReset,5) == CSTR_EQUAL)
{
//重启
CloseAm300c();
//***************关闭音乐通道***************
CommPortOpen();
Mute(L"aa 05 10 00");
Mute(L"aa 06 10 00");
Sleep(1000); //等待1秒
//******************************************
PowerOff(); //挂起
Sleep(3000); //等待3秒
RebootCE(); //Reset
}
}
}
}
/*BOOL COpAm300cDlg::LaunchApplication(LPCTSTR pCmdLine, PROCESS_INFORMATION *pProcessInfo)
{
STARTUPINFO stStartUpInfo;
memset(&stStartUpInfo, 0, sizeof(STARTUPINFO));
stStartUpInfo.cb = sizeof(STARTUPINFO);
stStartUpInfo.dwFlags = 0;///STARTF_USESHOWWINDOW;
stStartUpInfo.wShowWindow = 0;//SW_SHOWDEFAULT;
return CreateProcess(NULL, (LPTSTR)pCmdLine, NULL, NULL, FALSE,
NULL, NULL,
NULL, &stStartUpInfo, pProcessInfo);
return FALSE;
} */
/*获取AM300C的状态(0:未启动;1:正在运行) */
int COpAm300cDlg::GetStatus()
{
HWND hwnd=::FindWindow(NULL,L"CEAm300");
if (hwnd==NULL) {
return 0;
}
else{
return 1;
}
}
void COpAm300cDlg::PowerOff()
{
//(虚键)挂起
::keybd_event(VK_OFF, 0, 0, 0);
::keybd_event(VK_OFF, 0, KEYEVENTF_KEYUP, 0);
}
//重新启动PC
void COpAm300cDlg::RebootCE()
{
//硬启动CE --> Reset Power
__asm
{
cli
mov al, 0FEh
out 064h, al
jmp near $
}
}
/*重起AM300C程序。如果正在运行,先要执行(CLOSEAM300C)关闭动作*/
void COpAm300cDlg::StartupAm300c()
{
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
//Startup AM300C_R 子进程
if (!CreateProcess(L"硬盘\\Am300c\\amce300.exe",
NULL, NULL, NULL,
FALSE,
0,
NULL, NULL,
&si, &pi ))
{
//MessageBox(L"Create Process Failed !");
CloseAm300c();
}
}
/*关闭AM300C程序,KILL AM300C PROCESS*/
void COpAm300cDlg::CloseAm300c()
{
//CWnd *pCwnd;
//pCwnd = NULL;
//LPARAM lParam=(long)this->GetSafeHwnd();
/* if(EnumWindows(CallBackFun,NULL))
{
// if (pCwnd != NULL)
// {
// pCwnd->PostMessage(WM_USER+101, NULL, NULL);
// }
}
*/
//HWND hwnd=::FindWindow(NULL,L"CEAm300");
//::SendMessage(hwnd,WM_CLOSE,NULL,NULL);
//::WaitForSingleObject(hwnd,5000); //wait for save data .
::TerminateProcess(pi.hProcess,0);
::TerminateProcess(pi.hThread,0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -