前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >虚拟串口模拟器和串口调试助手使用教程「建议收藏」

虚拟串口模拟器和串口调试助手使用教程「建议收藏」

作者头像
全栈程序员站长
发布2022-07-01 15:28:20
6.2K0
发布2022-07-01 15:28:20
举报

大家好,又见面了,我是你们的朋友全栈君。

虚拟串口(虚拟 COM 端口),应该很多人都知道,也就是一种模拟物理串行接口的 软件 它完全复制了硬件 COM 接口的功能,并且将被操作系统和串行应用程序识别为真实端口。

以前的电脑,基本标配都包含一个串口。但现在的电脑,基本都没有配置串口了。如果要使用串口的功能,基本就要用一个USB转串口的硬件模块。

现实生活中,虚拟串口用处很多。比如:你的应用程序检测串行输入数据的时候,方便调试。还比如:多个有应用程序之间使用串口通信。

虚拟串口软件推荐:强大的虚拟串口软件


串口调试助手软件有很多,随便选一个自己习惯的即可。


演示一下串口模拟器和串口调试助手使用

打开VSPD,添加虚拟串口

在这里插入图片描述
在这里插入图片描述

打开串口调试助手,设置好必要的参数

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801141400770.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801141400770.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801141420257.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801141420257.png

打开两个串口,在其中一个串口中发送区写入消息,点击发送,在另一个串口的接收区可以看到我们发送的消息

在这里插入图片描述
在这里插入图片描述

两个串口的连接示意图如下图所示

image-20210801143004531
image-20210801143004531

数据传输路线有两条

  • 串口调试助手1–>COM1–>COM2–>串口调试助手2
  • 串口调试助手2–>COM2–>COM1–>串口调试助手1

在Windows下使用C语言调用串口,接收发送数据

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801144941433.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801144941433.png

C语言程序测试接收代码如下:

代码语言:javascript
复制
#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;
}

运行

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801150720719.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801150720719.png

因为这个程序打开的是COM1,因此我么在COM2的串口调试助手中,在发送区输入要发送的值,点击发送

在这里插入图片描述
在这里插入图片描述

这是可以在运行串口看到接收并且打印出我们发送的值

https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801150945647.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801150945647.png

我们继续测试几次

image-20210801151033146
image-20210801151033146
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801151046652.png
https://raw.githubusercontent.com/xkyvvv/blogpic/main/pic1/image-20210801151046652.png

C语言程序测试发送代码如下:

代码语言:javascript
复制
#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);
}

也可以使用下面这段代码

代码语言:javascript
复制
#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的串口调试助手那接收不到数据。 最近发现了是哪里出了问题,串口参数不一致导致的问题。 只需要将代码改成如下

代码语言:javascript
复制
#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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • C语言程序测试接收代码如下:
  • C语言程序测试发送代码如下:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档