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

📄 test232dlg.cpp

📁 windows ce com port test
💻 CPP
字号:
// test232Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "test232.h"
#include "test232Dlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTest232Dlg dialog

CTest232Dlg::CTest232Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTest232Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTest232Dlg)
		// 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 CTest232Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTest232Dlg)
	DDX_Control(pDX, IDC_MSG, m_stcMsg);
	DDX_Control(pDX, IDC_BAUD, m_cboBaud);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTest232Dlg, CDialog)
	//{{AFX_MSG_MAP(CTest232Dlg)
	ON_BN_CLICKED(IDC_RECEIEVE, OnReceieve)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTest232Dlg message handlers

BOOL CTest232Dlg::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
	m_cboBaud.SetCurSel(4);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}
#define WAITCMD_TIMEOUT	7000
#define ASCII_XON       0x11
#define ASCII_XOFF      0x13
#define PT_ACK			0x06
#define BLOCKSIZE		1000

HANDLE WINAPI OpenComport(DWORD dwComport, DWORD dwBaudRate)
{
    HANDLE  DriverHandle;
    char    Com_Name[10];
    DWORD   DCB_Baud_Rate, retval;
    DCB     Our_DCB;

    //
    // Range check the Comport number
    //

    if (dwComport > 16) {
        return 0;
    }

    //
    // Make a COMPORT name from a number
    //

    wsprintf ((unsigned short*)Com_Name, _T("COM%i:"), dwComport);

    //
    // Convert the Baud Rate to the Com library define for the DCB
    //
	if(dwBaudRate < 300)
		DCB_Baud_Rate = CBR_110;
	else if(dwBaudRate <600)
		DCB_Baud_Rate = CBR_300;
	else if(dwBaudRate <1200)
		DCB_Baud_Rate = CBR_600;
	else if(dwBaudRate <2400)
		DCB_Baud_Rate = CBR_1200;
	else if(dwBaudRate <4800)
		DCB_Baud_Rate = CBR_2400;
	else if(dwBaudRate <9600)
		DCB_Baud_Rate = CBR_4800;
	else if(dwBaudRate <19200)
		DCB_Baud_Rate = CBR_9600;
	else if(dwBaudRate <38400)
		DCB_Baud_Rate = CBR_19200;
	else if(dwBaudRate <57600)
		DCB_Baud_Rate = CBR_38400;
	else if(dwBaudRate <115200)
		DCB_Baud_Rate = CBR_57600;
	else if(dwBaudRate <128000)
		DCB_Baud_Rate = CBR_115200;
	else 
		DCB_Baud_Rate = CBR_128000;


    //
    // Open a channel to the Comport - This example DOES NOT use overlapped I/O
    // Since the benefits of overlapped I/O are few, we suggest not using it.
    //
    DriverHandle = CreateFile ((const unsigned short *)Com_Name, GENERIC_READ | GENERIC_WRITE,
                               0, NULL, OPEN_EXISTING, 0, NULL);

    //
    // Do we have a valid handle? (If not, the driver probably isn't loaded)
    //

    if (DriverHandle == INVALID_HANDLE_VALUE) {
		//MessageBox(0, _T("Open COM error"), _T(""), MB_OK);
		OutputDebugString(_T("Open comport error!!\n"));
        return (HANDLE)0;
    } else {


        //
        // The SetupComm() function establishes the Transmit and Receive 
        // buffer sizes.
        //
//		SetupComm(DriverHandle, 10240, 10240);
        //
        // Obtain the current DCB structure. this can be saved away for restore after
        // the application is done using the Comport
        //
        GetCommState (DriverHandle, &Our_DCB);

        //
        // Fill in the DCB structure with our own settings.
        //
        Our_DCB.BaudRate = DCB_Baud_Rate;
        Our_DCB.fParity = 0;
        Our_DCB.fOutxCtsFlow = 0 ;
        Our_DCB.fOutxDsrFlow = 0;
        Our_DCB.fDtrControl = DTR_CONTROL_ENABLE;
        Our_DCB.fDsrSensitivity = FALSE;
        Our_DCB.fTXContinueOnXoff = 0;
		Our_DCB.fOutX = Our_DCB.fInX = FALSE;     
		Our_DCB.XonChar = ASCII_XON ;
		Our_DCB.XoffChar = ASCII_XOFF ;
        Our_DCB.fErrorChar = 0;
        Our_DCB.fNull = 0;
        Our_DCB.fRtsControl = RTS_CONTROL_DISABLE;
        Our_DCB.fAbortOnError = 0;
        Our_DCB.ByteSize = 8;
        Our_DCB.Parity = NOPARITY;
        Our_DCB.StopBits = ONESTOPBIT;

        //
        // Configure the comport with our new DCB
        //

        retval=SetCommState (DriverHandle, &Our_DCB);

        //
        // Setup a mask that allows us to tell if the port is done transmitting
        // the current buffer of data
        //
        SetCommMask (DriverHandle, EV_TXEMPTY);

	COMMTIMEOUTS CommTimeouts;
	GetCommTimeouts(DriverHandle, &CommTimeouts);
	CommTimeouts.ReadIntervalTimeout = MAXDWORD;
	CommTimeouts.ReadTotalTimeoutMultiplier = MAXDWORD;
	CommTimeouts.ReadTotalTimeoutConstant = 0;
	CommTimeouts.WriteTotalTimeoutMultiplier = 0;
	CommTimeouts.WriteTotalTimeoutConstant = 0;
	SetCommTimeouts(DriverHandle, &CommTimeouts);

 		PurgeComm( DriverHandle, PURGE_TXABORT | PURGE_RXABORT |
								  PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
   }
    return DriverHandle;
}

DWORD WINAPI CloseComport(HANDLE hComport)
{
    // purge any outstanding reads/writes and close device handle
    PurgeComm(hComport, PURGE_TXABORT | PURGE_RXABORT |
                              PURGE_TXCLEAR | PURGE_RXCLEAR ) ;
	CloseHandle(hComport);
	return 0;
}

//单

⌨️ 快捷键说明

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