📄 omt.cpp
字号:
#include <windows.h>
#include <commdlg.h>
#include <stdio.h>
#include <process.h> // _beginthread, _endthread
#include <time.h> //引用time/strftime/localtime
#include "resource.h"
#define ID_EDIT_OUT 1 //定义子窗口标识,该区交换机输出报告
#define ID_EDIT_LINE 2 //定义子窗口标识,该区写入向交换机发出的指令
#define ID_LISTBOX_HIS 3 //定义子窗口标识,该区保存指令历史
#define ID_STATUS 4 //定义子窗口标识,做状态栏,输出程序当前动作
#define MAX_CMD_LEN 200 //指令输入区容纳最大字符长度
#define ID_TIMER_HALFMINUTE 10 //计时器,用来每日生成LOG文件
void PopFontInitialize (HWND) ; //字体设置相关函数,在Font.cpp中实现
BOOL PopFontChooseFont (HWND) ; //字体设置相关函数,在Font.cpp中实现
void PopFontSetFont (HWND) ; //字体设置相关函数,在Font.cpp中实现
void PopFontDeinitialize (void) ; //字体设置相关函数,在Font.cpp中实现
int AskConfirmation (HWND) ;
void GenerateDate(void) ; //Result saved in szTimeBuf
BOOL OpenComm(void) ;
void CloseComm(void) ;
void AppendEditOut(char *) ;
void SetReadEvent(void) ;
void ReadThread( void *dummy ) ;
void FormatInBuf(void) ;
BOOL ReadData(void) ;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK EditLineProc (HWND, UINT, WPARAM, LPARAM) ;
BOOL CALLBACK AboutDlgProc (HWND, UINT, WPARAM, LPARAM) ;
char szAppName[] = "Siemens MSC OMT" ;
char szClassName[] = "OMT" ;
WNDPROC OldList ; //子窗口函数,用来处理EditLine区消息,如实现回车键响应
static HWND hwndEditOut, hwndEditLine, hwndListBoxHis, hwndStatus ; //几个子窗口的句柄
int EditLen ; //获取edit当前最大值,用来向后面追加字符信息
char szTimeBuf[13], tmpTimeStr[8] ;
time_t tTime ;
struct tm * tmTime ;
FILE * LogFile ;
BOOL FileOpened = FALSE; //log文件打开标志
HWND hwnd ; //主窗口句柄
enum CommState
{
Comm_Open,
Comm_Idle,
Comm_Writing,
Comm_Reading,
Comm_Close
}emCommState ;
struct BufLen //设置端口的参数
{
long InLen;
long OutLen;
}MyBufLen;
//BOOL CommOpened ;
COMSTAT CommStat; //端口读写缓冲区状态
OVERLAPPED osRead,osWrite;
char BELL = 7; //振铃信号,每次发送数据前要先发送
char SHAKE = 6; //握手信号,每次发送指令前要先发送
//char EOF = 4;
char NEXTPARA=3;
BOOL JobEnd, ReadTerminated;
HANDLE CommPort;
DCB CommDCB, OldCommDCB ;
COMMTIMEOUTS CommTimeouts,OldCommTimeouts; //设置端口的参数
char inbuff[10*1024]; //读缓冲区
char OutBuf[128]; //写缓冲区
DWORD WriteLen=0; //重叠写的变量
DWORD nByteRead=0,nByteReaded=0; //重叠读的变量
DWORD dwError; //
char szInBuf[1024*4];
char szSave[1024*4];
DWORD dwReadLen;
DWORD dwSaveLen;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
MSG msg ;
WNDCLASS wndclass ;
HACCEL hAccel ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, "OMT1") ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = "MainMenu" ;
wndclass.lpszClassName = szClassName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, "This program requires Windows NT/2000/XP!",
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szClassName, szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hInstance, NULL) ;
ShowWindow (hwnd, SW_MAXIMIZE) ;
UpdateWindow (hwnd) ;
hAccel = LoadAccelerators (hInstance, "OMT") ;
while (GetMessage (&msg, NULL, 0, 0))
{
if (!TranslateAccelerator (hwnd, hAccel, &msg)) //拦截加速键消息
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HINSTANCE hInstance ;
static UINT messageFindReplace ;
switch (message)
{
case WM_CREATE :
hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
SetTimer (hwnd, ID_TIMER_HALFMINUTE, 30000, NULL) ;
hwndEditOut = CreateWindow ("edit", NULL, WS_CHILD | WS_VISIBLE |
WS_VSCROLL | ES_WANTRETURN | WS_BORDER | ES_LEFT
| ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 0, 0, hwnd, (HMENU) ID_EDIT_OUT,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
hwndEditLine = CreateWindow ("edit", NULL, WS_CHILD | WS_VISIBLE
| ES_UPPERCASE | ES_LEFT | ES_AUTOHSCROLL,
0, 0, 0, 0, hwnd, (HMENU) ID_EDIT_LINE,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
hwndListBoxHis = CreateWindow ("listbox", NULL, WS_CHILD | WS_VISIBLE |
WS_BORDER | ES_LEFT | ES_AUTOVSCROLL | WS_VSCROLL,
0, 0, 0, 0, hwnd, (HMENU) ID_LISTBOX_HIS,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
hwndStatus = CreateWindow ("static", NULL,WS_CHILD | WS_VISIBLE |
SS_SUNKEN,0, 0, 0, 0, hwnd, (HMENU) ID_STATUS,
((LPCREATESTRUCT) lParam) -> hInstance, NULL) ;
OldList = (WNDPROC) SetWindowLong (hwndEditLine, GWL_WNDPROC,
(LPARAM) EditLineProc) ;
SendMessage(hwndEditOut, EM_SETLIMITTEXT, 3 * 1024 * 1024, 0);
SendMessage(hwndEditLine, EM_SETLIMITTEXT, MAX_CMD_LEN, 0);
SetWindowText(hwndStatus, "Omtv prepare to start!Please set CommOption first and ConnectMsc.") ;
PopFontInitialize (hwndEditOut) ;
PopFontInitialize (hwndEditLine) ;
emCommState = Comm_Close ;
GenerateDate() ;
LogFile = fopen(strcat(szTimeBuf, ".txt\0"),"a+");
if(LogFile != NULL)
FileOpened = TRUE ;
else
MessageBox(hwnd, "Can't open log file!", "Abort", NULL) ;
return 0 ;
//case WM_SETFOCUS :
// SetFocus (hwndEditOut) ;
// return 0 ;
case WM_SIZE :
MoveWindow (hwndEditOut, 0, 0, LOWORD (lParam), HIWORD (lParam) - 106, TRUE) ;
MoveWindow (hwndEditLine, 0, HIWORD (lParam) - 106, LOWORD (lParam), 20, TRUE) ;
MoveWindow (hwndListBoxHis, 0, HIWORD (lParam) - 86, LOWORD (lParam), 66, TRUE) ;
MoveWindow (hwndStatus, 0, HIWORD (lParam) - 20, LOWORD (lParam), 20, TRUE) ;
return 0 ;
case WM_COMMAND :
switch(LOWORD (wParam))
{
case ID_EDIT_OUT: //输出缓冲区满时重置
if (HIWORD (wParam) == EN_ERRSPACE || HIWORD (wParam) == EN_MAXTEXT)
SetWindowText(hwndEditOut, "Out area full,New page:\r\n") ;
return 0 ;
case IDM_CONNMSC:
if(emCommState == Comm_Close)
{
if(OpenComm())
{
SetReadEvent() ;
ReadTerminated = false ;
_beginthread( ReadThread, 0, NULL );
}
}
else
MessageBox(hwnd, "Comm has been opened!", "Notice", NULL) ;
return 0 ;
case IDM_DISCONNMSC:
if(emCommState != Comm_Close)
{
ReadTerminated = true ;
CloseComm() ;
}
else
MessageBox(hwnd, "Comm has not been opened!", "Notice", NULL) ;
return 0 ;
case IDM_EXIT:
SendMessage (hwnd, WM_CLOSE, 0, 0) ;
return 0 ;
case IDM_SELECTALL:
SendMessage (hwndEditOut, EM_SETSEL, 0, -1) ;
return 0 ;
case IDM_COPY:
SendMessage (hwndEditOut, WM_COPY, 0, 0) ;
return 0 ;
case IDM_FIND:
return 0 ;
case IDM_FONT:
if (PopFontChooseFont (hwnd))
{
PopFontSetFont (hwndEditOut) ;
PopFontSetFont (hwndEditLine) ;
}
return 0 ;
case IDM_ABOUT:
DialogBox (hInstance, "OmtAbout", hwnd, AboutDlgProc) ;
return 0 ;
case IDM_MANUAL:
DialogBox (hInstance, "OmtManual", hwnd, AboutDlgProc) ;
return 0 ;
}
break ;
case WM_TIMER:
switch(wParam)
{
case ID_TIMER_HALFMINUTE:
time(&tTime) ;
tmTime = localtime( &tTime ) ;
memset(tmpTimeStr, 0, 8) ;
strftime( tmpTimeStr, 8, "%H%M", tmTime ) ;
if(!strcmp(tmpTimeStr, "0001\0\0\0\0")) //每日凌晨00:01生成新Log文件
{
//MessageBox(hwnd, tmpTimeStr, tmpTimeStr, NULL) ;
SetWindowText(hwndEditOut, "New day log start:") ;
GenerateDate() ;
if(FileOpened)
fclose(LogFile) ;
LogFile = fopen(strcat(szTimeBuf, ".txt\0"),"a+");
}
if(LogFile != NULL)
FileOpened = TRUE ;
else
MessageBox(hwnd, "Can't create new log file!", "Abort", NULL) ;
break ;
}
return 0 ;
case WM_CLOSE :
if (IDYES == AskConfirmation (hwnd))
DestroyWindow (hwnd) ;
return 0 ;
case WM_DESTROY :
if(FileOpened)
fclose(LogFile) ;
if(emCommState != Comm_Close)
CloseComm() ;
PopFontDeinitialize () ;
KillTimer (hwnd, ID_TIMER_HALFMINUTE) ;
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}
LRESULT CALLBACK EditLineProc (HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
if (message == WM_KEYDOWN && wParam == VK_RETURN) //加速键不能用^M,否则与回车冲突
{
char EditLineStr[MAX_CMD_LEN] ;
SendMessage(hwndEditLine, EM_GETLINE, 0, (LPARAM)EditLineStr);
if(emCommState != Comm_Close)
{
if (emCommState != Comm_Writing)
{
emCommState = Comm_Writing ;
//TransmitCommChar(CommPort,BELL);
Sleep(10);
if (!JobEnd)
{
if (strlen(EditLineStr) == 0) //继续要下一个参数的提示
{
TransmitCommChar(CommPort, NEXTPARA);
//goto end ;
}
WriteLen = 1;
memset(OutBuf, 0, sizeof(OutBuf));
strcpy(OutBuf,EditLineStr);
AppendEditOut(EditLineStr) ;
AppendEditOut("\r\n") ;
SendMessage(hwndEditLine, EM_SETSEL, 0, -1);
SendMessage (hwndListBoxHis, LB_INSERTSTRING, 0, (LPARAM)EditLineStr) ;
strcat(OutBuf,"\x3");
WriteFile(CommPort, OutBuf, strlen(OutBuf), &WriteLen, &osWrite);
if(GetLastError() == ERROR_IO_PENDING)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -