VC6.0与34401建立串口通信,设置电压1V量程,读取电压并打印
时间: 2024-05-05 09:20:37 浏览: 118
首先需要确保你的计算机连接了34401A万用表,并且已经安装了相应的驱动程序。接下来,可以按照以下步骤进行串口通信:
1. 在VC6.0中创建一个空白项目;
2. 在工具箱中找到MSComm控件,将其拖拽到窗体上;
3. 双击MSComm控件,在其属性窗口中设置串口参数,如波特率、数据位、停止位、校验位等;
4. 在代码中调用MSComm控件的Open方法打开串口;
5. 向串口发送读取电压的指令;
6. 等待万用表返回电压值,然后使用MessageBox等控件打印结果;
7. 关闭串口。
具体代码如下:
```cpp
#include "stdafx.h"
#include "Commctrl.h"
#include "MSComm.h"
CMSComm Comm;
void CSerialPortDlg::OnOpen()
{
// 打开串口
Comm.SetCommPort(1);
Comm.SetSettings("9600,N,8,1");
Comm.SetInputMode(1);
Comm.SetPortOpen(TRUE);
}
void CSerialPortDlg::OnRead()
{
// 发送读取电压指令
Comm.SetOutput(COleVariant("MEASure:VOLTage:DC 1,1"));
// 等待万用表返回结果
Sleep(1000);
// 读取电压值
COleVariant var = Comm.GetInput();
// 打印结果
CString str;
str.Format("电压值为:%s V", var.bstrVal);
MessageBox(str);
}
void CSerialPortDlg::OnClose()
{
// 关闭串口
Comm.SetPortOpen(FALSE);
}
```
需要注意的是,在发送指令和读取结果时,需要根据34401A万用表的协议格式进行处理。具体协议格式可以参考34401A万用表的用户手册。
阅读全文
相关推荐


















