当前位置: 首页 > news >正文

选择串口号STC串口收发通讯正常

选择串口号STC串口收发通讯正常

#include "stdafx.h"

#include "newa.h"

#include "newaDlg.h"

#ifdef _DEBUG

#define new DEBUG_NEW

#undef THIS_FILE

static char THIS_FILE[] = __FILE__;

#endif

class CAboutDlg : public CDialog

{

public:

CAboutDlg();

enum { IDD = IDD_ABOUTBOX };

protected:

virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support

protected:

DECLARE_MESSAGE_MAP()

};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)

{

}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)

END_MESSAGE_MAP()

CNewaDlg::CNewaDlg(CWnd* pParent /*=NULL*/)

: CDialog(CNewaDlg::IDD, pParent)

{

//{{AFX_DATA_INIT(CNewaDlg)

m_count = _T("");

m_recmess = _T("");

m_sendmess = _T("");

//}}AFX_DATA_INIT

// Note that LoadIcon does not require a subsequent DestroyIcon in Win32

m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

}

void CNewaDlg::DoDataExchange(CDataExchange* pDX)

{

CDialog::DoDataExchange(pDX);

//{{AFX_DATA_MAP(CNewaDlg)

DDX_Control(pDX, IDC_COMSELECT, m_com);

DDX_Control(pDX, IDC_SENDDATA, m_clrsend);

DDX_Control(pDX, IDC_RECDATA, m_clrrec);

DDX_Control(pDX, IDC_OPENSEND, m_OPENSEND);

DDX_Control(pDX, IDC_MSCOMM1, m_Comm);

DDX_Text(pDX, IDC_COUNT, m_count);

DDX_Text(pDX, IDC_RECMESS, m_recmess);

DDX_Text(pDX, IDC_SENDMESS, m_sendmess);

//}}AFX_DATA_MAP

}

BEGIN_MESSAGE_MAP(CNewaDlg, CDialog)

//{{AFX_MSG_MAP(CNewaDlg)

ON_WM_SYSCOMMAND()

ON_WM_PAINT()

ON_WM_QUERYDRAGICON()

ON_CBN_SELENDOK(IDC_COMSELECT, OnComselect)

ON_BN_CLICKED(IDC_OPENSEND, OnOpensend)

ON_BN_CLICKED(IDC_SENDDATA, OnSenddata)

ON_BN_CLICKED(IDC_RECDATA, OnRecdata)

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////

// CNewaDlg message handlers

BOOL CNewaDlg::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

m_com.SetCurSel(2);

m_Comm.SetInputMode(1); //输入方式为二进制方式

m_Comm.SetInBufferSize(1024); //设置输入缓冲区大小

m_Comm.SetOutBufferSize(1024); //设置输出缓冲区大小,波特率4800无校验,8个数据位,2个停止位

//m_Comm.SetSettings("115200,n,8,1");

m_Comm.SetRThreshold(1); //参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件

m_Comm.SetSThreshold(1);//参数1表示当传输缓冲区完全空时将引发一个接收数据的OnComm事件

m_Comm.SetInputLen(0); //设置当前接收区数据长度为0

m_Comm.GetInput(); //先预读缓冲区以清除残留数据

if(!m_Comm.GetPortOpen())//判断串口的状态,如果是关闭状态,则打开

m_Comm.SetPortOpen(TRUE);//打开串口

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

}

void CNewaDlg::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.

void CNewaDlg::OnPaint()

{

if (IsIconic())

{

CPaintDC dc(this); // device context for painting

SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

// Center icon in client rectangle

int cxIcon = GetSystemMetrics(SM_CXICON);

int cyIcon = GetSystemMetrics(SM_CYICON);

CRect rect;

GetClientRect(&rect);

int x = (rect.Width() - cxIcon + 1) / 2;

int y = (rect.Height() - cyIcon + 1) / 2;

// Draw the icon

dc.DrawIcon(x, y, m_hIcon);

}

else

{

CDialog::OnPaint();

}

}

// The system calls this to obtain the cursor to display while the user drags

// the minimized window.

HCURSOR CNewaDlg::OnQueryDragIcon()

{

return (HCURSOR) m_hIcon;

}

BEGIN_EVENTSINK_MAP(CNewaDlg, CDialog)

//{{AFX_EVENTSINK_MAP(CNewaDlg)

ON_EVENT(CNewaDlg, IDC_MSCOMM1, 1 /* OnComm */, OnComm, VTS_NONE)

//}}AFX_EVENTSINK_MAP

END_EVENTSINK_MAP()

char HexChar(char c)//检测一个字符是不是十六进制字符,若是返回相应的值,否则返回0x10;

{ if((c>='0')&&(c<='9'))

return c-0x30;

else if((c>='A')&&(c<='F'))

return c-'A'+10;

else if((c>='a')&&(c<='f'))

return c-'a'+10;

else return 0x10;

}

int Str2Hex(CString str,CByteArray &data)

{//将一个字符串作为十六进制串转化为一个字节数组,字节间可用空格分隔,返回转换后的字节数组长度,同时字节数

int t,t1;

int rlen=0,len=str.GetLength();

data.SetSize(len/2);

for(int i=0;i<len;)

{char l,h=str[i];

if(h==' ')

{i++;

continue;

}

i++;

if(i>=len)break;

l=str[i];

t=HexChar(h);

t1=HexChar(l);

if((t==16)||(t1==16))

break;

else t=t*16+t1;

i++;

data[rlen]=(char)t;

rlen++;

}

data.SetSize(rlen);

return rlen;

}

void CNewaDlg::OnComm()

{

// TODO: Add your control notification handler code here

VARIANT m_input1;

COleSafeArray m_input2;

long length,i;

BYTE data[1024];

CString str;

m_count = _T("0");

UpdateData(FALSE); //更新编辑框内容

if(m_Comm.GetCommEvent()==2)//接收缓冲区内有字符

{

m_count = _T("1");

UpdateData(FALSE); //更新编辑框内容

m_input1=m_Comm.GetInput();//读取缓冲区内的数据

m_input2=m_input1;//将VARIANT型变量转换为ColeSafeArray型变量

length=m_input2.GetOneDimSize();//确定数据长度

for(i=0;i<length;i++)

m_input2.GetElement(&i,data+i);//将数据转换为BYTE型数组

m_count = _T("2");

UpdateData(FALSE); //更新编辑框内容

for(i=0;i<length;i++)//将数组转换为Cstring型变量

{

BYTE a=* (char *)(data+i);

str.Format("%02X ",a);

m_recmess+=str;

//UpdateData(FALSE); //更新编辑框内容

m_count = _T("3");

UpdateData(FALSE); //更新编辑框内容

}

}

UpdateData(FALSE);//更新编辑框内容

}

void CNewaDlg::OnComselect()

{

// TODO: Add your control notification handler code here

int i=9600;

CString temp;

temp.Format("%d,n,8,1",i);

m_Comm.SetSettings(temp);

if(m_Comm.GetPortOpen()) //

m_Comm.SetPortOpen(FALSE); //

m_Comm.SetCommPort(m_com.GetCurSel()+1); //

}

void CNewaDlg::OnOpensend()

{

if( !m_Comm.GetPortOpen())

m_Comm.SetPortOpen(TRUE);//打开串口

UpdateData(TRUE);

CByteArray data;

int len=Str2Hex(m_sendmess,data);

m_Comm.SetOutput(COleVariant(data));//发送数据

}

void CNewaDlg::OnSenddata()

{

// TODO: Add your control notification handler code here

m_sendmess= _T("");

m_count = _T("");

UpdateData(FALSE);

}

void CNewaDlg::OnRecdata()

{

// TODO: Add your control notification handler code here

m_recmess = _T("");

UpdateData(FALSE);

}

void CNewaDlg::OnOK()

{

// TODO: Add extra validation here

m_Comm.SetPortOpen(false);//关闭串口

CDialog::OnOK();

}

http://www.jsqmd.com/news/1123544/

相关文章:

  • AI绘画中文提示词生成“鬼画符”的根源与优化策略
  • UnityHDRP数字人开发全流程与AI集成实战
  • 基于OpenCV与YOLOv5的实时目标检测:从环境搭建到模型训练全流程实践
  • 3大核心功能揭秘:MathLive如何重塑网页数学公式编辑体验?
  • 量子显微镜技术在皮米级芯片测试中的应用与突破
  • Stable Diffusion中文提示词生成鬼画符的成因与优化策略
  • 话疗的具象化的庖丁解牛
  • Cocos Creator 3.8.7物理系统与动态碰撞体实战
  • 为什么KCC全局卡尔曼滤波器的“侧信道”风险不成立
  • Python Pygame绘制2D坦克图形教程
  • 虚幻引擎蓝图调试与跨设备迁移实战指南
  • Node.js+Vue构建高性能人员信息查询系统实战
  • AI高效使用指南:从新手到专家的思维转变与实践方法
  • 工业二氧化硫排放数据分析方法与技术路线
  • 基于Python和CNN的花卉识别系统开发实践
  • Unity开发高频问题解决方案与性能优化指南
  • Unity PCVR开发与HTC Vive Pro适配实战指南
  • RTX Spark开启真AI PC时代:从本地智能体到全栈重构
  • 无人机植被遥感技术:原理、应用与实战指南
  • Unity游戏性能优化全攻略:从渲染到架构的实战技巧
  • KMX63与PIC18F96J94在HMI设计中的协同应用
  • GPT-5.5不存在:AI模型命名规范与技术事实核查指南
  • Swagger UI未授权访问漏洞:原理、风险与三种主流修复方案详解
  • AI公司技术实力评估四维模型:算力、算法、场景、数据
  • 2026免费图片去水印工具教程:网页端电脑手机无需下载、手机APP用法
  • AI进销存助手:智能采购、销售对账与库存预警实战
  • Windows 服务 Session 0 隔离:3 种方法实现服务与桌面用户界面交互
  • 基于YOLOv8的农业害虫智能识别系统设计与实现
  • 大数据BI工具中的分类预测模型实战指南
  • MLOps测试策略:从实验室到生产的模型质量保障