当前位置:网站首页>Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
Virtual serial port simulator and serial port debugging assistant tutorial "suggestions collection"
2022-07-01 16:21:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Virtual serial port ( fictitious COM port ), A lot of people should know , That is, a kind of analog physical serial interface Software . It completely replicates the hardware COM Function of interface , And will be recognized as a real port by the operating system and serial application .
Previous computers , The basic standard configuration includes a serial port . But now the computer , Basically, there is no serial port configured . If you want to use the function of serial port , Basically, you need to use one USB To serial port hardware module .
in real life , Virtual serial port is of many uses . such as : When your application detects serial input data , Convenient debugging . Also like : Serial port communication is used between multiple applications .
Virtual serial port software recommendation : Powerful virtual serial port software
There are many serial port debugging assistant software , Just choose one you are used to .
Demonstrate the use of serial port simulator and serial port debugging assistant
open VSPD, Add virtual serial port
Open the serial debugging assistant , Set the necessary parameters
Open two serial ports , Write a message in the sending area in one of the serial ports , Click Send , The message we sent can be seen in the receiving area of another serial port
The connection diagram of the two serial ports is shown in the figure below
There are two data transmission routes
- Serial debugging assistant 1–>COM1–>COM2–> Serial debugging assistant 2
- Serial debugging assistant 2–>COM2–>COM1–> Serial debugging assistant 1
stay Windows Next use C Language call serial port , Receive and send data
C The receiving code of language program test is as follows :
#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;
}function
Because this program opens COM1, So I'm here COM2 Serial port debugging assistant , Enter the value to be sent in the sending area , Click Send
This can be seen in the running serial port to receive and print out the value we sent
We continue to test several times
C The sending code of language program test is as follows :
#include <Windows.h>
#include <stdio.h>
HANDLE hCom;
int main(void)
{
hCom = CreateFile(TEXT("COM1"),//COM1 mouth
GENERIC_READ, // Allow to read
0, // Specify shared properties , Because the serial port cannot be shared , So this parameter must be 0
NULL,
OPEN_EXISTING, // Open instead of create
FILE_ATTRIBUTE_NORMAL, // Property description , The value is FILE_FLAG_OVERLAPPED, Means to use asynchrony I/O, The parameter is 0, Synchronous representation I/O operation
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
printf(" open COM Failure !\n");
return FALSE;
}
else
{
printf("COM Open the success !\n");
}
SetupComm(hCom, 1024, 1024); // The size of the input buffer and the output buffer are 1024
/********************************* timeout **************************************/
COMMTIMEOUTS TimeOuts;
// Set read timeout
TimeOuts.ReadIntervalTimeout = MAXDWORD;// Read interval timeout
TimeOuts.ReadTotalTimeoutMultiplier = 0;// Read time coefficient
TimeOuts.ReadTotalTimeoutConstant = 0;// Read the time constant
// Set write timeout
TimeOuts.WriteTotalTimeoutMultiplier = 1;// Write the time factor
TimeOuts.WriteTotalTimeoutConstant = 1;// Write a time constant
SetCommTimeouts(hCom, &TimeOuts); // Set timeout
/***************************************** Configure serial port ***************************/
DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate = 9600; // The baud rate is 9600
dcb.ByteSize = 8; // Each byte has 8 position
dcb.Parity = NOPARITY; // No parity bit
dcb.StopBits = ONESTOPBIT; // A stop bit
SetCommState(hCom, &dcb);
DWORD wCount;// The number of bytes actually read
bool bReadStat;
char str[2] = { 0 };
while (1)
{
int i;
unsigned char sendData[256] = {0};// An array written to the serial port buffer
for(i=0; i<16; i++)
{
sendData[i] = i;
}
DWORD dwWriteLen = 0;
if(!WriteFile(hCom, sendData, 16, &dwWriteLen, NULL))
{
printf(" Serial port failed to send data !\n");
}
Sleep(1000);
}
CloseHandle(hCom);
}You can also use the following code
#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;
}But I don't know why , These two pieces of code can work properly , however COM2 The serial port debugging assistant of cannot receive data . Recently, I found out what went wrong , Problems caused by inconsistent serial port parameters . Just change the code as follows
#include <Windows.h>
#include <stdio.h>
HANDLE hCom;
int main(void)
{
hCom = CreateFile(TEXT("COM1"),//COM1 mouth
GENERIC_READ | GENERIC_WRITE, // Allow reading and writing
0, // Specify shared properties , Because the serial port cannot be shared , So this parameter must be 0
NULL,
OPEN_EXISTING, // Open instead of create
FILE_ATTRIBUTE_NORMAL, // Property description , The value is FILE_FLAG_OVERLAPPED, Means to use asynchrony I/O, The parameter is 0, Synchronous representation I/O operation
NULL);
if (hCom == INVALID_HANDLE_VALUE)
{
printf(" open COM Failure !\n");
return FALSE;
}
else
{
printf("COM Open the success !\n");
}
SetupComm(hCom, 1024, 1024); // The size of the input buffer and the output buffer are 1024
/********************************* timeout **************************************/
COMMTIMEOUTS TimeOuts;
// Set read timeout
TimeOuts.ReadIntervalTimeout = MAXDWORD;// Read interval timeout
TimeOuts.ReadTotalTimeoutMultiplier = 0;// Read time coefficient
TimeOuts.ReadTotalTimeoutConstant = 0;// Read the time constant
// Set write timeout
TimeOuts.WriteTotalTimeoutMultiplier = 1;// Write the time factor
TimeOuts.WriteTotalTimeoutConstant = 1;// Write a time constant
SetCommTimeouts(hCom, &TimeOuts); // Set timeout
/***************************************** Configure serial port ***************************/
DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate = 115200; // The baud rate is 115200
dcb.ByteSize = 8; // Each byte has 8 position
dcb.Parity = NOPARITY; // No parity bit
dcb.StopBits = ONESTOPBIT; // A stop bit
SetCommState(hCom, &dcb);
DWORD wCount;// The number of bytes actually read
bool bReadStat;
char str[2] = { 0 };
while (1)
{
int i;
unsigned char sendData[256] = {0};// An array written to the serial port buffer
for(i=0; i<16; i++)
{
sendData[i] = i;
}
DWORD dwWriteLen = 0;
if(!WriteFile(hCom, sendData, 16, &dwWriteLen, NULL))
{
printf(" Serial port failed to send data !\n");
}
Sleep(1000);
}
CloseHandle(hCom);
}After running , Found in the serial port 2 Debugging assistant , The displayed received data is increasing ,
But it will not be displayed on the interface , I don't know why .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/131062.html Link to the original text :https://javaforall.cn
边栏推荐
- 数据库系统原理与应用教程(004)—— MySQL 安装与配置:重置 MySQL 登录密码(windows 环境)
- 2023届春招实习-个人面试过程和面经分享
- Korean AI team plagiarizes shock academia! One tutor with 51 students, or plagiarism recidivist
- Principle of SSM framework
- There is a difference between u-standard contract and currency standard contract. Will u-standard contract explode
- 动作捕捉系统用于苹果采摘机器人
- 分享在大疆DJI(深圳总部)工作的日常和福利
- 智慧党建: 穿越时空的信仰 | 7·1 献礼
- 电脑屏幕变色了怎么调回来,电脑屏幕颜色怎么改
- Introduction to RT thread env tool (learning notes)
猜你喜欢

Nuxt. JS data prefetching

How to adjust the color of the computer screen and how to change the color of the computer screen

Problems encountered in IM instant messaging development to maintain heartbeat

Problèmes rencontrés dans le développement de la GI pour maintenir le rythme cardiaque en vie

【观察】数字化时代的咨询往何处走?软通咨询的思与行

ADS算力芯片的多模型架构研究

Submission lottery - light application server essay solicitation activity (may) award announcement

Malaysia's Star: Sun Yuchen is still adhering to the dream of digital economy in WTO MC12

【php毕业设计】基于php+mysql+apache的教材管理系统设计与实现(毕业论文+程序源码)——教材管理系统
![[IDM] IDM downloader installation](/img/2b/baf8852b422c1c4a18e9c60de864e5.png)
[IDM] IDM downloader installation
随机推荐
全面看待企业数字化转型的价值
IM即時通訊開發實現心跳保活遇到的問題
Learn selenium to simulate mouse operation, and you can be lazy a little bit
【Hot100】19. Delete the penultimate node of the linked list
实现数字永生还有多久?元宇宙全息真人分身#8i
July 1, 2022 Daily: Google's new research: Minerva, using language models to solve quantitative reasoning problems
【Hot100】20. 有效的括号
基于PHP的轻量企业销售管理系统
开机时小键盘灯不亮的解决方案
2023 spring recruitment Internship - personal interview process and face-to-face experience sharing
Use Tencent cloud to build a map bed service
Where should older test / development programmers go? Will it be abandoned by the times?
电脑照片尺寸如何调整成自己想要的
Action after deleting laravel's model
制造业数字化转型究竟是什么
idea启动Command line is too long问题处理
使用腾讯云搭建图床服务
圈铁发音,动感与无噪强强出彩,魔浪HIFIair蓝牙耳机测评
How to write good code - Defensive Programming Guide
Crypto Daily: Sun Yuchen proposed to solve global problems with digital technology on MC12