当前位置:网站首页>虚拟串口模拟器和串口调试助手使用教程「建议收藏」
虚拟串口模拟器和串口调试助手使用教程「建议收藏」
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
边栏推荐
- AVL balanced binary search tree
- vim用户自动命令示例
- 周少剑,很少见
- 苹果自研基带芯片再次失败,说明了华为海思的技术领先性
- 嵌入式开发:5个修订控制最佳实践
- 【LeetCode】43. String multiplication
- 【Hot100】19. 删除链表的倒数第 N 个结点
- Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
- 【Hot100】17. Letter combination of telephone number
- The picgo shortcut is amazing. This person thinks exactly the same as me
猜你喜欢

圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评

Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知

Embedded development: five revision control best practices

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

IM即时通讯开发万人群聊消息投递方案

STM32F1与STM32CubeIDE编程实例-PWM驱动蜂鸣器生产旋律

How does win11 set user permissions? Win11 method of setting user permissions

China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District

自動、智能、可視!深信服SSLO方案背後的八大設計

How to use MySQL language for row and column devices?
随机推荐
Principle of motion capture system
Crypto Daily:孙宇晨在MC12上倡议用数字化技术解决全球问题
圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
表格存储中tablestore 目前支持mysql哪些函数呢?
【php毕业设计】基于php+mysql+apache的教材管理系统设计与实现(毕业论文+程序源码)——教材管理系统
Nuxt.js数据预取
Smart Party Building: faith through time and space | 7.1 dedication
How to use MySQL language for row and column devices?
部门来了个拿25k出来的00后测试卷王,老油条表示真干不过,已被...
大龄测试/开发程序员该何去何从?是否会被时代抛弃?
从 MLPerf 谈起:如何引领 AI 加速器的下一波浪潮
China's intelligent transportation construction from the perspective of "one hour life circle" in Dawan District
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
Im instant messaging develops a message delivery scheme for 10000 people
vscode 查找 替换 一个文件夹下所有文件的数据
分享在大疆DJI(深圳总部)工作的日常和福利
Automatique, intelligent, visuel! Forte conviction des huit conceptions derrière la solution sslo
Problems encountered in IM instant messaging development to maintain heartbeat
Please, stop painting star! This has nothing to do with patriotism!
最新NLP赛事实践总结!