📄 customerdlg.cpp
字号:
// CustomerDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Afxwin.h"
#include "Customer.h"
#include "CustomerDlg.h"
#include "Setting.h"
#include "Login.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCustomerDlg dialog
/////////////////////////////////////////////////////////////////////////////
// Definition
//
#define SOH 0x01
#define STX 0x02
#define ETX 0x03
/////////////////////////////////////////////////////////////////////////////
// Global Variables
//
BYTE COMBuf[64];
BYTE* pCOMBuf = COMBuf;
HANDLE hComPort = INVALID_HANDLE_VALUE;
int CmdCode = 0;
int TagCode = 0;
BYTE CMDBuf[8] = {'0','1','2','3','6','7','A','B'};
BYTE TAGBuf[7] = {'0','1','2','3','4','5','6'};
CString m_Temp =TEXT("");
CString m_Page;
bool m_Flag=true;
CString m_GoldPrice;
CString m_Fineness;
CString m_MarkUp;
CString m_Currency;
CString m_ExchangeRate;
CString CSItemNo=TEXT("");
CString CSQty=TEXT("");
CString CSUnit=TEXT("");
CString CSFineness=TEXT("");
CString CSGoldWeight=TEXT("");
CString CSLoss=TEXT("");
CString CSGoldCost=TEXT("");
CString CSStoneCost=TEXT("");
CString CSLabourCost=TEXT("");
CString CSTotalCost=TEXT("");
CString CSStoneDesc_1=TEXT("");
CString CSStoneDesc_2=TEXT("");
CString CSStoneDesc_3=TEXT("");
CString CSStoneDesc_4=TEXT("");
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Function Prototypes
//
bool OpenPort(void);
int MyHex(char* s);
double ToRound(double num,int i);
BYTE * string2unchar(CString strValue, int istrLength);
void BufMovBuf (BYTE* pDBuf, BYTE* pSBuf, int length);
BYTE CheckSum(BYTE* pBuf, int length);
void SendCommand(void);
bool RecieveData(void);
char* WChar2SChar(CString &s);
WCHAR* SChar2WChar(char* ansitring, DWORD dwAnsiBytes);
bool AsciiToHex (BYTE* pAscii, BYTE* pHex, int length);
////////////////////////////////////////////////////////////////////////////
CCustomerDlg::CCustomerDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCustomerDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCustomerDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCustomerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCustomerDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCustomerDlg, CDialog)
//{{AFX_MSG_MAP(CCustomerDlg)
ON_WM_TIMER()
ON_BN_CLICKED(BUTTON_Read, OnRead)
ON_BN_CLICKED(BUTTON_Clear, OnClear)
ON_BN_CLICKED(But_ReadBlock, OnReadBlock)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCustomerDlg message handlers
BOOL CCustomerDlg::OnInitDialog()
{
Login DlgLogin;
DlgLogin.m_bFullScreen = false;
DlgLogin.DoModal();
if(DlgLogin.m_flag=="fail")
{
SendMessage(WM_CLOSE);
return false;
}
if (OpenPort()==false)
{
AfxMessageBox(L"Open COM port failed");
}
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
SetTimer(1, 300, 0);
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
void CCustomerDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CDialog::OnTimer(nIDEvent);
KillTimer(1);
Setting SettingDlg;
SetDlgItemText(EDIT_GoldPrice,L"550.00");
SettingDlg.m_bFullScreen = false;
SettingDlg.DoModal();
m_GoldPrice= SettingDlg.m_GoldPrice;
m_Fineness=SettingDlg.m_Fineness;
m_MarkUp=SettingDlg.m_MarkUp ;
m_Currency=SettingDlg.m_Currency ;
m_ExchangeRate=SettingDlg.m_ExchangeRate ;
CWnd *pWnd=(CWnd *)GetDlgItem(Lbl_Symbol);
pWnd->SetWindowText(m_Currency);
}
bool OpenPort()
{
if (hComPort != INVALID_HANDLE_VALUE)
{
return true;
}
// Open the selected COM port.
DCB dcb;
COMMTIMEOUTS cto;
CString m_temp="COM4:";
hComPort = CreateFile ( m_temp, GENERIC_READ | GENERIC_WRITE,
0, NULL, OPEN_EXISTING, 0, NULL );
if (hComPort == INVALID_HANDLE_VALUE)
{
return false;
}
else
{
// Configure com port.
dcb.DCBlength = sizeof (dcb);
GetCommState (hComPort, &dcb);
dcb.BaudRate = CBR_9600;
dcb.fParity = FALSE;
dcb.fNull = FALSE;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.ByteSize = 8;
SetCommState (hComPort, &dcb);
// Set the timeouts. Set infinite read timeout.
cto.ReadIntervalTimeout = 1;
cto.ReadTotalTimeoutMultiplier = 1;
cto.ReadTotalTimeoutConstant = 1;
cto.WriteTotalTimeoutMultiplier = 0;
cto.WriteTotalTimeoutConstant = 0;
SetCommTimeouts (hComPort, &cto);
return true;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//
// 捌祘Α跋
//
//////////////////////////////////////////////////////////////////////////////////////////////
//--------------------------------------------------------------------------------------------
// Buf Mov Buf
//
void BufMovBuf (BYTE* pDBuf, BYTE* pSBuf, int length)
{
if (length < 1)
return;
for (int i = 0; i < length; i++)
*(pDBuf+i) = *(pSBuf+i);
}
//---------------------------------------------------------------------------------------
// Check Sum routine
//
BYTE CheckSum(BYTE* pBuf, int length)
{
BYTE bcc = 0x00;
int i=0;
for(i=0; i< length; i++)
bcc ^= *(pBuf + i);
bcc |= 0x20;
return bcc;
}
//----------------------------------------------------------------------------------
// Send Command
//
void SendCommand(void)
{
BYTE SendBuf[80];
BYTE* pSendBuf = SendBuf;
DWORD cBytes;
int length = (int)COMBuf[0]; // save DATA length
SendBuf[0] = SOH;
SendBuf[1] = 'S'; // PT
SendBuf[2] = '0'; // ID1
SendBuf[3] = '1'; // ID2
SendBuf[4] = COMBuf[1]; // FC1
SendBuf[5] = COMBuf[2]; // FC2
SendBuf[6] = STX;
BufMovBuf ( pSendBuf+7, pCOMBuf+3, length );
SendBuf[length+7] = ETX;
SendBuf[length+8] = CheckSum( pSendBuf, length+8 );
WriteFile ( hComPort, pSendBuf, length+9, &cBytes, 0 );
memset (pCOMBuf+3, '\0', 61); // initial COMBuf
}
//----------------------------------------------------------------------------------------
// Recieve Data
//
bool RecieveData(void)
{
bool RxOK = false; // True if Recieving completes
BYTE InBuf[128]; // Input Buffer
BYTE* pInBuf = InBuf; // Pointer to Input Buffer
int count = 0; // Time Out Counter
int step = 0; // Rxd step
int index = 0; // Input Buffer Offset
DWORD cBytes = 0;
int total = 0; // Recieved Total Data Counter
BYTE bcc = 0x00; // Check Sum
BYTE i = 0x00;
BYTE fc1 = COMBuf[1];
BYTE fc2 = COMBuf[2];
memset (pInBuf, '\0', 128);
while(!RxOK)
{
ReadFile(hComPort, pInBuf+index, 1, &cBytes, NULL);
if (cBytes != 0)
{
total++;
i = *(pInBuf+index);
MessageBox(0,(LPCTSTR)i,0,MB_OK);
switch (step)
{
case 0:
if (i == SOH) { step++; index++; } // Check SOH
break;
case 1:
if (i == 's') { step++; index++; } // Check PT
break;
case 2:
if (i == '0') { step++; index++; } // Check ID1
break;
case 3:
if (i == '1') { step++; index++; } // Check ID2
break;
case 4:
if (i == fc1) { step++; index++; } // Check FC1
break;
case 5:
if (i == fc2) { step++; index++; } // Check FC2
break;
case 6:
if (i == STX) { step++; index++; } // Check STX
break;
case 7:
if (i == ETX) { step++; } // get data & Check ETX
index++;
break;
case 8:
if (i == CheckSum (pInBuf, index)) { RxOK = true; } // Check BCC
else { step = 0; }
break;
default:
step = 0;
break;
}
}
else if (++count == 500)
break;
}
if (RxOK)
{
BufMovBuf ( pCOMBuf+3, pInBuf+7, total-9 );
COMBuf[0] = total-9;
}
return RxOK;
}
///////////////////////////////////////////////////////////////////////////////////////////////
//
// Utility functions
//
///////////////////////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------------
// Ansi to Unicode
//
WCHAR* SChar2WChar(char* ansitring, DWORD dwAnsiBytes)
{
WCHAR* wchStr;
wchStr=(WCHAR*) malloc ((dwAnsiBytes + 1) * 2);
if(wchStr == NULL)
return NULL;
else
memset(wchStr, '\0', ((dwAnsiBytes + 1) * 2));
MultiByteToWideChar(CP_ACP, MB_USEGLYPHCHARS, ansitring, -1, wchStr, ((dwAnsiBytes+1)*2));
return wchStr;
}
//------------------------------------------------------------------------------------
// Unicode to Ansi
//
char* WChar2SChar(CString &s)
{
int nSize = s.GetLength();
char *pAnsiString = new char[nSize+1];
wcstombs(pAnsiString, s, nSize+1);
return pAnsiString;
}
//------------------------------------------------------------------------------------
// Ascii to Hex code
//
bool AsciiToHex (BYTE* pAscii, BYTE* pHex, int length)
{
int i = 0;
BYTE ASC_H, ASC_L;
for (i = 0; i< length; i++)
{
ASC_H = *(pAscii+i*2);
ASC_L = *(pAscii+i*2+1);
if ((ASC_H >= '0') && (ASC_H <= '9')) ASC_H = ASC_H - 0x30;
else if ((ASC_H >= 'A') && (ASC_H <= 'F')) ASC_H = ASC_H - 0x37;
else return false;
if ((ASC_L >= '0') && (ASC_L <= '9')) ASC_L = ASC_L - 0x30;
else if ((ASC_L >= 'A') && (ASC_L <= 'F')) ASC_L = ASC_L - 0x37;
else return false;
ASC_H = ASC_H << 4; // オ簿
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -