?? comm2410dlg.cpp
字號:
// Comm2410Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Comm2410.h"
#include "Comm2410Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
DWORD BaudRate[]={CBR_300,CBR_600,CBR_1200,CBR_2400,CBR_4800,CBR_9600,CBR_19200,CBR_38400,CBR_57600,CBR_115200};
DWORD Parity[]={NOPARITY,ODDPARITY,EVENPARITY,MARKPARITY,SPACEPARITY};
DWORD Data[]={5,6,7,8};
DWORD Stop[]={ONESTOPBIT,ONE5STOPBITS,TWOSTOPBITS};
static long receivebyte;
static long sendbyte;
static BOOL StopSend=TRUE;
int iErrCount = 0;
/////////////////////////////////////////////////////////////////////////////
// CComm2410Dlg dialog
int SvrConvertUni2Mbcs(CString& strUni,BYTE*& pBuff)
{
//_CrtSetDbgFlag(ON);
#ifndef UNICODE
long iLen = strUni.GetLength();
pBuff = new BYTE[iLen];
memcpy(pBuff,strUni.GetBuffer(iLen),iLen);
strUni.ReleaseBuffer();
return iLen;
#else
ASSERT(NULL == pBuff);
long iLen = strUni.GetLength() * sizeof(TCHAR);//unicode版本
int iRequireByte = WideCharToMultiByte(CP_ACP,
0,
(LPCWSTR)strUni.GetBuffer(iLen),
iLen/sizeof(TCHAR),
NULL,
0,
NULL,
NULL);
pBuff = new BYTE[iRequireByte];
WideCharToMultiByte(CP_ACP,
0,
(LPCWSTR)strUni.GetBuffer(iLen),
iLen/sizeof(TCHAR),
(char*)pBuff,
iRequireByte,
NULL,
NULL);
strUni.ReleaseBuffer();
//pBuff沒有刪除,調用者負責刪除
return iRequireByte;
#endif
}
CComm2410Dlg::CComm2410Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CComm2410Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CComm2410Dlg)
m_iCom1 = _T("");
m_iCom2 = _T("");
m_iBaud = -1;
m_iData = -1;
m_iParity = -1;
m_iPort = -1;
m_iStop = -1;
m_senddata = _T("^#$@#^*(g)&^^**&^%^$%^&**(())*9*&^&%@&*()&^()*^(_(_)*()&&^$%#@$#!@@#*(&%^#$@#$%^&**(())**&^&!@#$%^&*(_(_)*()&#");
m_count = 20;
m_iCharhex = -1;
m_iEndhex = _T("0");
m_iStarthex = _T("0");
m_iForEver = FALSE;
m_iInterval = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_iBaud = 5;
m_iStop = 0;
m_iPort =0;
m_iParity = 0;
m_count =20;
m_iData = 3;
m_iInterval=200;
receivebyte = 0;
bOpen=FALSE;
m_iCharhex=0;
strportname = _T("");
hPort = NULL;
m_iCom1 = _T("OFF");
m_iCom2 = _T("OFF");
// m_iRecCount = _T("0 Byte");
Tempchar.SetSize(0);
usedtime = 0;
}
void CComm2410Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CComm2410Dlg)
DDX_Control(pDX, IDC_EDIT_HAVESEND, m_iSendCount);
DDX_Control(pDX, IDC_EDIT_TEMP, m_iTemp);
DDX_Control(pDX, IDC_EDIT_RECEIVE, m_iReceiveData);
DDX_Text(pDX, IDC_STATIC_COM1, m_iCom1);
DDX_Text(pDX, IDC_STATIC_COM2, m_iCom2);
DDX_CBIndex(pDX, IDC_COMBO_BAUTE, m_iBaud);
DDX_CBIndex(pDX, IDC_COMBO_DATA, m_iData);
DDX_CBIndex(pDX, IDC_COMBO_PARITY, m_iParity);
DDX_CBIndex(pDX, IDC_COMBO_PORT, m_iPort);
DDX_CBIndex(pDX, IDC_COMBO_STOP, m_iStop);
DDX_Text(pDX, IDC_EDIT_SEND, m_senddata);
DDX_Text(pDX, IDC_EDIT_SENDNUMBER, m_count);
DDX_Radio(pDX, IDC_RADIO_FASHION, m_iCharhex);
DDX_Text(pDX, IDC_EDIT_ENDHEX, m_iEndhex);
DDX_Text(pDX, IDC_EDIT_STARTHEX, m_iStarthex);
DDX_Check(pDX, IDC_CHECK_FOREVER, m_iForEver);
DDX_Text(pDX, IDC_EDIT_INTERVAL, m_iInterval);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CComm2410Dlg, CDialog)
//{{AFX_MSG_MAP(CComm2410Dlg)
ON_BN_CLICKED(IDC_BUTTON_STATUS, OnButtonStatus)
ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
ON_BN_CLICKED(IDC_BUTTON_CLEAR, OnButtonClear)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_RADIO_FASHION, OnRadioFashion)
ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
ON_EN_KILLFOCUS(IDC_EDIT_SENDNUMBER, OnKillfocusEditSendnumber)
ON_EN_KILLFOCUS(IDC_EDIT_STARTHEX, OnKillfocusEditStarthex)
ON_EN_KILLFOCUS(IDC_EDIT_ENDHEX, OnKillfocusEditEndhex)
ON_BN_CLICKED(IDC_RADIO_FOREVER, OnRadioForever)
ON_BN_CLICKED(IDC_CHECK_FOREVER, OnCheckForever)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CComm2410Dlg message handlers
BOOL CComm2410Dlg::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
// TODO: Add extra initialization here
SetTimer(2,123,0);
GetDlgItem(IDC_EDIT_STARTHEX)->EnableWindow(FALSE);
GetDlgItem(IDC_EDIT_ENDHEX)->EnableWindow(FALSE);
CString str(_T("^#$@#$%^&**(())*9*&^&%@$#!@#$%^&*(_(_)*()&&^$%#@$#!@@#*(&%^#$@#$%^&**(())**&^&%@$#!@#$%^&*(_(_)*()&&"));
m_iReceiveData.SetWindowText(str); ///顯示接收到的字符
return TRUE; // return TRUE unless you set the focus to a control
}
void CComm2410Dlg::SendData()
{
DWORD dwlen;
if (hPort == NULL) return ;
DWORD Err;
COMSTAT Comm;
if (!ClearCommError(hPort,&Err,&Comm))
return;
if (Err != 0)
{
CString str;
str.Format(_T("Err1%d %d\n"),iErrCount ++,Err);
GetDlgItem(IDC_STATIC123)->SetWindowText(str);
}
dwlen =0;
int uu = GetTickCount();
if (!WriteFile(hPort,Tempchar.GetData(),
Tempchar.GetSize(),
&dwlen,
NULL))
{
AfxMessageBox(_T("發送串口數據失敗!"));
}
sendbyte+=dwlen;
if (!ClearCommError(hPort,&Err,&Comm))
return;
if (Err != 0)
{
CString str;
str.Format(_T("Err3%d %d\n"),iErrCount ++,Err);
GetDlgItem(IDC_STATIC123)->SetWindowText(str);
}
if (dwlen == 16)
{
usedtime = 0;
CString str;
str.Format(_T("T %d"),GetTickCount () - uu);
GetDlgItem(IDC_STATIC123)->SetWindowText(str);
}
return;
}
void CComm2410Dlg::OnButtonStatus()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
KillTimer(1);
KillTimer(3);
GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
GetDlgItem(IDC_EDIT_SENDNUMBER)->EnableWindow(TRUE);
if (bOpen==TRUE)
{
CloseHandle(hPort);
hPort = NULL;
// m_portstatus = strportname+_T("已關閉");
if (strportname ==_T("COM1:"))
m_iCom1 = _T("OFF");
else
m_iCom2 = _T("OFF");
GetDlgItem(IDC_BUTTON_STATUS)->SetWindowText(_T("打開串口"));
UpdateData(FALSE);
bOpen=FALSE;
return;
}
strportname.Format(_T("COM%d:"),m_iPort+1);
if (hPort != NULL)
{
CloseHandle(hPort);
hPort = NULL;
}
hPort = CreateFile(strportname,// Pointer to the name of the port
GENERIC_READ | GENERIC_WRITE,
// Access (read-write) mode
0, // Share mode
NULL, // Pointer to the security attribute
OPEN_EXISTING,// How to open the serial port
0, // Port attributes
NULL); // Handle to port with attribute
// to copy
if (NULL == hPort)
{
//m_portstatus = strportname +_T("打開失敗");
UpdateData(FALSE);
return ;
}
else
{
DCB PortDCB;
if (!GetCommState(hPort,&PortDCB))
{
//m_portstatus = strportname+_T("打開失敗");
UpdateData(FALSE);
return;
}
PortDCB.DCBlength = sizeof(PortDCB);
PortDCB.fBinary = 1; ///binary mode, no EOF check
PortDCB.fParity = 0; ///enable parity checking
// PortDCB.fOutxCtsFlow = m_iDsrCts; // CTS output flow control
// PortDCB.fOutxDsrFlow = m_iDsrCts; //// DSR output flow control
// PortDCB.fOutX = m_iXonOff;////// XON/XOFF out flow control
// PortDCB.fInX = m_iXonOff;///// XON/XOFF in flow control
PortDCB.fErrorChar = 0; /// enable error replacement
PortDCB.fNull = 0; // enable null stripping
// PortDCB.fRtsControl = m_iRtsOn; // RTS flow control
// PortDCB.fDtrControl = m_iDtrOn; //DTR flow control
PortDCB.fAbortOnError = 0; // abort reads/writes on error
PortDCB.BaudRate = BaudRate[m_iBaud]; // Current baud
// m_iBau.Format(_T("%d"),BaudRate[m_iBaud]);
PortDCB.StopBits = Stop[m_iStop];
PortDCB.Parity = Parity[m_iParity];
PortDCB.ByteSize = Data[m_iData];
if (!SetCommState(hPort,&PortDCB))
{
::CloseHandle(hPort);
hPort = NULL;
AfxMessageBox(_T("串口參數設置失敗"));
return;
}
/*
COMMTIMEOUTS CommTimeOuts;
COMMTIMEOUTS CommTimeouts;
memset(&CommTimeOuts,0,sizeof(COMMTIMEOUTS));
CommTimeOuts.ReadIntervalTimeout = 0xffff0000;
CommTimeOuts.ReadTotalTimeoutConstant = 0xffff0000;
CommTimeOuts.ReadTotalTimeoutMultiplier = 100;
CommTimeOuts.WriteTotalTimeoutConstant = MAXDWORD;
CommTimeOuts.WriteTotalTimeoutMultiplier = 1000;
if (!SetCommTimeouts (hPort, &CommTimeouts))
{
CloseHandle(hPort);
hPort = NULL ;
//m_portstatus = strportname+_T("打開失敗");
UpdateData(FALSE);
return ;
}
*/
SetupComm(hPort,8192,8192);
//m_portstatus = strportname+_T("已打開");
if (strportname ==_T("COM1:"))
m_iCom1 = _T("ON");
else m_iCom2 = _T("ON");
bOpen=TRUE;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -