当前位置:网站首页>虚拟串口模拟器和串口调试助手使用教程「建议收藏」
虚拟串口模拟器和串口调试助手使用教程「建议收藏」
2022-07-01 16:12:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
虚拟串口(虚拟 COM 端口),应该很多人都知道,也就是一种模拟物理串行接口的软件。 它完全复制了硬件 COM 接口的功能,并且将被操作系统和串行应用程序识别为真实端口。
以前的电脑,基本标配都包含一个串口。但现在的电脑,基本都没有配置串口了。如果要使用串口的功能,基本就要用一个USB转串口的硬件模块。
现实生活中,虚拟串口用处很多。比如:你的应用程序检测串行输入数据的时候,方便调试。还比如:多个有应用程序之间使用串口通信。
虚拟串口软件推荐:强大的虚拟串口软件
串口调试助手软件有很多,随便选一个自己习惯的即可。
演示一下串口模拟器和串口调试助手使用
打开VSPD,添加虚拟串口
打开串口调试助手,设置好必要的参数
打开两个串口,在其中一个串口中发送区写入消息,点击发送,在另一个串口的接收区可以看到我们发送的消息
两个串口的连接示意图如下图所示
数据传输路线有两条
- 串口调试助手1–>COM1–>COM2–>串口调试助手2
- 串口调试助手2–>COM2–>COM1–>串口调试助手1
在Windows下使用C语言调用串口,接收发送数据
C语言程序测试接收代码如下:
#include<stdio.h>
#include<windows.h>
int main()
{
FILE *fp;
if ((fp = fopen("com1", "r")) == NULL)
{
printf("cannot open com!\n");
}
else
printf("open com successful!\n");
char str;
while (1)
{
fscanf(fp, "%c", &str);
printf("%c ", str);
Sleep(100);
}
return 0;
}
运行
因为这个程序打开的是COM1,因此我么在COM2的串口调试助手中,在发送区输入要发送的值,点击发送
这是可以在运行串口看到接收并且打印出我们发送的值
我们继续测试几次
C语言程序测试发送代码如下:
#include <Windows.h>
#include <stdio.h>
HANDLE hCom;
int main(void)
{
hCom = CreateFile(TEXT("COM1"),//COM1口
GENERIC_READ, //允许读
0, //指定共享属性,由于串口不能共享,所以该参数必须为0
NULL,
OPEN_EXISTING, //打开而不是创建
FILE_ATTRIBUTE_NORMAL, //属性描述,该值为FILE_FLAG_OVERLAPPED,表示使用异步I/O,该参数为0,表示同步I/O操作
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
printf("打开COM失败!\n");
return FALSE;
}
else
{
printf("COM打开成功!\n");
}
SetupComm(hCom, 1024, 1024); //输入缓冲区和输出缓冲区的大小都是1024
/*********************************超时设置**************************************/
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout = MAXDWORD;//读间隔超时
TimeOuts.ReadTotalTimeoutMultiplier = 0;//读时间系数
TimeOuts.ReadTotalTimeoutConstant = 0;//读时间常量
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier = 1;//写时间系数
TimeOuts.WriteTotalTimeoutConstant = 1;//写时间常量
SetCommTimeouts(hCom, &TimeOuts); //设置超时
/*****************************************配置串口***************************/
DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate = 9600; //波特率为9600
dcb.ByteSize = 8; //每个字节有8位
dcb.Parity = NOPARITY; //无奇偶校验位
dcb.StopBits = ONESTOPBIT; //一个停止位
SetCommState(hCom, &dcb);
DWORD wCount;//实际读取的字节数
bool bReadStat;
char str[2] = { 0 };
while (1)
{
int i;
unsigned char sendData[256] = {0};//写入串口缓存区的数组
for(i=0; i<16; i++)
{
sendData[i] = i;
}
DWORD dwWriteLen = 0;
if(!WriteFile(hCom, sendData, 16, &dwWriteLen, NULL))
{
printf("串口发送数据失败!\n");
}
Sleep(1000);
}
CloseHandle(hCom);
}
也可以使用下面这段代码
#include<stdio.h>
#include<windows.h>
int main()
{
FILE *fp;
if ((fp = fopen("com1", "r")) == NULL)
{
printf("cannot open com!\n");
}
else
printf("open com successful!\n");
char str = 'x';
while (1)
{
fprintf(fp, "%s", &str);
Sleep(1000);
}
return 0;
}
不过不知道为什么,这两段代码都可以正常运行,但是COM2的串口调试助手那接收不到数据。 最近发现了是哪里出了问题,串口参数不一致导致的问题。 只需要将代码改成如下
#include <Windows.h>
#include <stdio.h>
HANDLE hCom;
int main(void)
{
hCom = CreateFile(TEXT("COM1"),//COM1口
GENERIC_READ | GENERIC_WRITE, //允许读和写
0, //指定共享属性,由于串口不能共享,所以该参数必须为0
NULL,
OPEN_EXISTING, //打开而不是创建
FILE_ATTRIBUTE_NORMAL, //属性描述,该值为FILE_FLAG_OVERLAPPED,表示使用异步I/O,该参数为0,表示同步I/O操作
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
printf("打开COM失败!\n");
return FALSE;
}
else
{
printf("COM打开成功!\n");
}
SetupComm(hCom, 1024, 1024); //输入缓冲区和输出缓冲区的大小都是1024
/*********************************超时设置**************************************/
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout = MAXDWORD;//读间隔超时
TimeOuts.ReadTotalTimeoutMultiplier = 0;//读时间系数
TimeOuts.ReadTotalTimeoutConstant = 0;//读时间常量
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier = 1;//写时间系数
TimeOuts.WriteTotalTimeoutConstant = 1;//写时间常量
SetCommTimeouts(hCom, &TimeOuts); //设置超时
/*****************************************配置串口***************************/
DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate = 115200; //波特率为115200
dcb.ByteSize = 8; //每个字节有8位
dcb.Parity = NOPARITY; //无奇偶校验位
dcb.StopBits = ONESTOPBIT; //一个停止位
SetCommState(hCom, &dcb);
DWORD wCount;//实际读取的字节数
bool bReadStat;
char str[2] = { 0 };
while (1)
{
int i;
unsigned char sendData[256] = {0};//写入串口缓存区的数组
for(i=0; i<16; i++)
{
sendData[i] = i;
}
DWORD dwWriteLen = 0;
if(!WriteFile(hCom, sendData, 16, &dwWriteLen, NULL))
{
printf("串口发送数据失败!\n");
}
Sleep(1000);
}
CloseHandle(hCom);
}
运行之后,发现在串口2的调试助手处,显示的接收数据在增加,
但是却不会在界面上显示出来,这个不知道是啥原因。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131062.html原文链接:https://javaforall.cn
边栏推荐
- [daily question] 1175 Prime permutation
- Can't global transactions be used when shardingjdbc is used in seate?
- 投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
- How does win11 set user permissions? Win11 method of setting user permissions
- StoneDB 为国产数据库添砖加瓦,基于 MySQL 的一体化实时 HTAP 数据库正式开源!
- 2022-07-01日报:谷歌新研究:Minerva,用语言模型解决定量推理问题
- Does 1.5.1 in Seata support mysql8?
- [IDM] IDM downloader installation
- 电脑照片尺寸如何调整成自己想要的
- Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12
猜你喜欢
分享在大疆DJI(深圳总部)工作的日常和福利
嵌入式开发:5个修订控制最佳实践
How does win11 set user permissions? Win11 method of setting user permissions
部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
How to adjust the size of computer photos to what you want
In the era of super video, what kind of technology will become the base?
The picgo shortcut is amazing. This person thinks exactly the same as me
周少剑,很少见
Comment win11 définit - il les permissions de l'utilisateur? Win11 comment définir les permissions de l'utilisateur
随机推荐
The latest NLP game practice summary!
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
【IDM】IDM下载器安装
Hardware development notes (9): basic process of hardware development, making a USB to RS232 module (8): create asm1117-3.3v package library and associate principle graphic devices
超视频时代,什么样的技术会成为底座?
July 1, 2022 Daily: Google's new research: Minerva, using language models to solve quantitative reasoning problems
[nodemon] app crashed - waiting for file changes before starting...解决方法
全面看待企业数字化转型的价值
基于PHP的轻量企业销售管理系统
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12
In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
智慧党建: 穿越时空的信仰 | 7·1 献礼
China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
周少剑,很少见
When ABAP screen switching, refresh the previous screen
ATSS:自动选择样本,消除Anchor based和Anchor free物体检测方法之间的差别
电脑照片尺寸如何调整成自己想要的
Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
Sales management system of lightweight enterprises based on PHP
从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮