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

📄 sdsddlg.cpp

📁 RS232
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// sdsdDlg.cpp : implementation file
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include "sdsd.h"
#include "sdsdDlg.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// 为 DirectSound 函数添加头文件
#include <dsound.h>
// 为 DirectSound 函数添加库文件
#pragma comment ( lib, "dsound.lib" )
#pragma comment ( lib, "dxguid.lib" )

CString m_ReceiveBuffer;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	m_SendData = _T("");
	m_ReceiveData = _T("");
	m_nChannels=1;//音频通道数
	m_nSamplesPerSec=11025;//采样频率
	m_wBitsPerSample=16;//采样大小
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMyDlg)
	DDX_Control(pDX, IDC_MSCOMM, m_Com);
	DDX_Text(pDX, IDC_EDIT_SENDDATA, m_SendData);
	DDX_Text(pDX, IDC_EDIT_RECEIVEDATA, m_ReceiveData);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_OPEN_COM, OnOpenCom)
	ON_BN_CLICKED(IDC_SEND_DATA, OnSendData)
	ON_BN_CLICKED(IDC_BUTTON_PLAY, OnButtonPlay)
	ON_BN_CLICKED(IDC_BUTTON_REC, OnButtonRec)
	ON_BN_CLICKED(IDC_BUTTON_SEND, OnButtonSend)
	ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
	ON_CBN_SELCHANGE(IDC_COMBO_BaudRate, OnSelchangeCOMBOBaudRate)
	ON_CBN_SELCHANGE(IDC_COMBO_ByteSize, OnSelchangeCOMBOByteSize)
	ON_CBN_SELCHANGE(IDC_COMBO_Parity, OnSelchangeCOMBOParity)
	ON_CBN_SELCHANGE(IDC_COMBO_StopBits, OnSelchangeCOMBOStopBits)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_CLEAR_RXWND, OnClearRxwnd)
	ON_BN_CLICKED(IDC_RADIO_HEXSEND, OnRadioHexsend)
	ON_BN_CLICKED(IDC_RADIO_HEXSHOW, OnRadioHexshow)
	ON_BN_CLICKED(IDC_RADIO_STRSEND, OnRadioStrsend)
	ON_BN_CLICKED(IDC_RADIO_STRSHOW, OnRadioStrshow)
	ON_EN_UPDATE(IDC_EDIT_SENDDATA, OnUpdateEditSenddata)
	ON_WM_SIZE()
	ON_WM_TIMER()
	ON_CBN_SELCHANGE(IDC_COMBO_PORT, OnSelchangeComboPort)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

BOOL CMyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here
	
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM1");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM2");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM3");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM4");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM5"); 
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM6");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM7");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM8");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM9");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM10"); 
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM11");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM12");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM13");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM14");
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM15"); 
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->AddString("COM16");

	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("110");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("300");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("600");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("1200");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("2400");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("4800");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("9600");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("14400");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("19200");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("28800");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("38400");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("56000");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("57600");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("115200");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("128000");
	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->AddString("256000");
	
	((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->AddString("(NONE)无校验");
	((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->AddString("(ODD)奇校验");
	((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->AddString("(EVENT)偶校验");
	((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->AddString("(MARK)标号校验");
	((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->AddString("(SPACE)空格校验");

	((CComboBox*)GetDlgItem(IDC_COMBO_ByteSize))->AddString("5");
	((CComboBox*)GetDlgItem(IDC_COMBO_ByteSize))->AddString("6");
	((CComboBox*)GetDlgItem(IDC_COMBO_ByteSize))->AddString("7");
	((CComboBox*)GetDlgItem(IDC_COMBO_ByteSize))->AddString("8");

	((CComboBox*)GetDlgItem(IDC_COMBO_StopBits))->AddString("1");
	((CComboBox*)GetDlgItem(IDC_COMBO_StopBits))->AddString("1.5");
	((CComboBox*)GetDlgItem(IDC_COMBO_StopBits))->AddString("2");

	((CComboBox*)GetDlgItem(IDC_COMBO_SamplesPerSec))->AddString("8000");
	((CComboBox*)GetDlgItem(IDC_COMBO_SamplesPerSec))->AddString("11025");
	((CComboBox*)GetDlgItem(IDC_COMBO_SamplesPerSec))->AddString("44100");

	((CComboBox*)GetDlgItem(IDC_COMBO_BitsPerSample))->AddString("8");
	((CComboBox*)GetDlgItem(IDC_COMBO_BitsPerSample))->AddString("16");

	((CComboBox*)GetDlgItem(IDC_COMBO_Channels))->AddString("单声道");
	((CComboBox*)GetDlgItem(IDC_COMBO_Channels))->AddString("双声道");

	((CComboBox*)GetDlgItem(IDC_COMBO_SamplesPerSec))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_COMBO_BitsPerSample))->SetCurSel(1);
	((CComboBox*)GetDlgItem(IDC_COMBO_Channels))->SetCurSel(0);

	GetDlgItem(IDC_BUTTON_SEND)->EnableWindow ( FALSE );
	GetDlgItem(IDC_BUTTON_STOP)->EnableWindow ( FALSE );
	GetDlgItem(IDC_BUTTON_PLAY)->EnableWindow ( FALSE );


	CString str;

	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->SetCurSel(0);

	((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->SetCurSel
		(
			((CComboBox*)GetDlgItem(IDC_COMBO_BaudRate))->FindString
				(
					0,
					ComState.Mid(0,ComState.Find(","))
				)
		);

	((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->SetCurSel
		(
			((CComboBox*)GetDlgItem(IDC_COMBO_Parity))->FindString
				(
					0,
					'(' + ComState.Mid(ComState.Find(",")+1,1)
				)
		);

	((CComboBox*)GetDlgItem(IDC_COMBO_ByteSize))->SetCurSel
		(

			((CComboBox*)GetDlgItem(IDC_COMBO_ByteSize))->FindString
				(
					0,
					ComState.Mid(ComState.Find(",")+3,1)
				)
		);

	((CComboBox*)GetDlgItem(IDC_COMBO_StopBits))->SetCurSel
		(

			((CComboBox*)GetDlgItem(IDC_COMBO_StopBits))->FindStringExact
				(
					0,
					ComState.Mid(ComState.Find(",")+5,strlen(ComState)-ComState.Find(",")-5)
				)
		);
	SetTimer(1,200,NULL);
	((CEdit*)GetDlgItem(IDC_EDIT_RECEIVEDATA))->SetReadOnly(TRUE);

	((CButton*)GetDlgItem(IDC_RADIO_STRSHOW))->SetCheck(1);
	((CButton*)GetDlgItem(IDC_RADIO_STRSEND))->SetCheck(1);
	m_HexShow =	FALSE;
	m_HexSend = FALSE;
	OnOpenCom();


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.


// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMyDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMyDlg::OnOpenCom() 
{
	// TODO: Add your control notification handler code here
//	char OpenComBut[9];
//	((CEdit*)GetDlgItem(IDC_OPEN_COM))->GetWindowText(OpenComBut,9);
		//0 == strcmp(OpenComBut,"打开串口")
	if(!m_Com.GetPortOpen())
	{
		GetPortNum();
		m_Com.SetCommPort(m_Port);
		m_Com.SetInBufferSize(4096);
		m_Com.SetOutBufferSize(4096);
		m_Com.SetPortOpen(TRUE);
		m_Com.SetInputMode(1);
		SetComState();
		//m_Com.SetSettings(ComState);
		m_Com.SetRThreshold(1);
		m_Com.SetInputLen(0);
		if(m_Com.GetPortOpen())
			((CEdit*)GetDlgItem(IDC_OPEN_COM))->SetWindowText("关闭串口");
		else
			m_Com.SetPortOpen(FALSE);
	}
	else
	{
		m_Com.SetPortOpen(FALSE);
		((CEdit*)GetDlgItem(IDC_OPEN_COM))->SetWindowText("打开串口");		
	}
}

void CMyDlg::OnSendData() 
{
	// TODO: Add your control notification handler code here
	unsigned int i=0,j=0;
	char SendData[1024],*strbuf;

	if(m_Com.GetPortOpen())
	{
		UpdateData();
		if(m_HexSend)
		{	
			for(j=0;j<strlen(m_SendData);)
			{
				if(m_SendData.GetAt(j) == VK_SPACE)		j++;
				else
				{
					if(m_SendData.GetAt(j) >= '0' && m_SendData.GetAt(j) <= '9')
						SendData[i]=m_SendData.GetAt(j)-'0';
					else if(m_SendData.GetAt(j) >= 'A' && m_SendData.GetAt(j) <= 'F')
						SendData[i]=m_SendData.GetAt(j)-'A'+10;
					else if(m_SendData.GetAt(j) >= 'a' && m_SendData.GetAt(j) <= 'f')
						SendData[i]=m_SendData.GetAt(j)-'a'+10;
					else if(m_SendData.GetAt(j) != VK_SPACE)
					{
						::MessageBox(NULL,"您的输入包含非法的十六进制字符!","提示!",MB_OK | MB_ICONWARNING);
						m_SendData="";
						i=0;
						break;
					}
					j++;
					if(j==strlen(m_SendData))
					{
						i++;
						break;
					}
					if(m_SendData.GetAt(j) == VK_SPACE)
						i++;
					if(m_SendData.GetAt(j) >= '0' && m_SendData.GetAt(j) <= '9')
					{	SendData[i]=(SendData[i]<<4)+m_SendData.GetAt(j)-'0';i++;}
					else if(m_SendData.GetAt(j) >= 'A' && m_SendData.GetAt(j) <= 'F')
					{	SendData[i]=(SendData[i]<<4)+m_SendData.GetAt(j)-'A'+10;i++;}
					else if(m_SendData.GetAt(j) >= 'a' && m_SendData.GetAt(j) <= 'f')
					{	SendData[i]=(SendData[i]<<4)+m_SendData.GetAt(j)-'a'+10;i++;}
					else if(m_SendData.GetAt(j) != VK_SPACE)
					{
						::MessageBox(NULL,"您的输入包含非法的十六进制字符!","提示!",MB_OK | MB_ICONWARNING);
						m_SendData="";
						i=0;
						break;
					}
					j++;
				}
			}
			strbuf=SendData;
		}
		else
		{
//			for(j=0;j<strlen(m_SendData);j++)
			strbuf=m_SendData.GetBuffer(1024);
			i=strlen(m_SendData);
		}
		CByteArray array;
		array.RemoveAll();
	//		i=strlen(strbuf);
		array.SetSize(i);
		for(j=0;j<i;j++)
			array.SetAt(j,strbuf[j]);

⌨️ 快捷键说明

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