⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comm2410dlg.cpp.bak

📁 wince下用evc写的一个串口通讯的程序
💻 BAK
📖 第 1 页 / 共 2 页
字号:
// 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;
/////////////////////////////////////////////////////////////////////////////
// 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("");
	m_count = 1;
	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 =1;
	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);

}

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,100,0);
	GetDlgItem(IDC_EDIT_STARTHEX)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_ENDHEX)->EnableWindow(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CComm2410Dlg::SendData()
{
   DWORD dwlen;

   if (hPort == NULL)  return ;
    dwlen =0; 

    COMSTAT ComStat;
	DWORD dwErrorFlags = 0;
	ClearCommError(hPort, &dwErrorFlags, &ComStat);
	if (dwErrorFlags > 0 )
	{
		ClearCommBreak(hPort);
		TRACE(_T("Error\n"));
		return ;
	}			
	DWORD dwLength =  ComStat.cbOutQue ;

	while (!(dwLength ==0))
	{
		Sleep(200);
			ClearCommError(hPort, &dwErrorFlags, &ComStat);
    	dwLength =  ComStat.cbOutQue ;
	}

   if (!WriteFile(hPort,Tempchar.GetData(),
	             Tempchar.GetSize(),
				 &dwlen,
				 NULL))

	  AfxMessageBox(_T("发送串口数据失败!"));
     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 = 100;
	   
	  if (!SetCommTimeouts (hPort, &CommTimeouts))
	  {
			CloseHandle(hPort);
			hPort = NULL ;
			//m_portstatus = strportname+_T("打开失败");
		     UpdateData(FALSE);
			return ;
	  }
          //m_portstatus = strportname+_T("已打开");
	  if (strportname ==_T("COM1:"))
		  m_iCom1 = _T("ON");
	  else m_iCom2 = _T("ON");
		  bOpen=TRUE;
		  UpdateData(FALSE);
  }
    GetDlgItem(IDC_BUTTON_STATUS)->SetWindowText(_T("关闭串口"));
}

void CComm2410Dlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	 if  (hPort == NULL) 
	{
     AfxMessageBox(_T("还没有串口被打开!"));
	 return;
	}

    if (!StopSend)
	{
		KillTimer(3);
		KillTimer(1);
	    GetDlgItem(IDC_EDIT_SENDNUMBER)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_SEND)->SetWindowText(_T("发送"));
		StopSend=TRUE;
		return;
    }
   sendbyte=0;
   UpdateData(TRUE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -