当前位置:网站首页>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
边栏推荐
- 揭秘慕思“智商税”:狂砸40亿搞营销,发明专利仅7项
- Where should older test / development programmers go? Will it be abandoned by the times?
- Programming examples of stm32f1 and stm32subeide - production melody of PWM driven buzzer
- How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
- 从大湾区“1小时生活圈”看我国智慧交通建设
- Origin2018安装与使用(整理中)
- [每日一氵]Latex 的通讯作者怎么搞
- PostgreSQL 存储结构浅析
- [nodemon] app crashed - waiting for file changes before starting...解决方法
- 【Hot100】20. 有效的括号
猜你喜欢
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes
In the past six months, it has been invested by five "giants", and this intelligent driving "dark horse" is sought after by capital
Win11如何设置用户权限?Win11设置用户权限的方法
实现数字永生还有多久?元宇宙全息真人分身#8i
Zero copy technology of MySQL
How long will it take to achieve digital immortality? Metacosmic holographic human avatar 8i
Vscode find and replace the data of all files in a folder
从大湾区“1小时生活圈”看我国智慧交通建设
Talking from mlperf: how to lead the next wave of AI accelerator
The picgo shortcut is amazing. This person thinks exactly the same as me
随机推荐
Go 语言怎么使用对称加密?
从大湾区“1小时生活圈”看我国智慧交通建设
Problèmes rencontrés dans le développement de la GI pour maintenir le rythme cardiaque en vie
Learn selenium to simulate mouse operation, and you can be lazy a little bit
全面看待企业数字化转型的价值
Sqlserver query: when a.id is the same as b.id, and the A.P corresponding to a.id cannot be found in the B.P corresponding to b.id, the a.id and A.P will be displayed
虚拟串口模拟器和串口调试助手使用教程「建议收藏」
Zero copy technology of MySQL
EndeavourOS移动硬盘安装
IM即時通訊開發實現心跳保活遇到的問題
She is the "HR of others" | ones character
Comprehensively view the value of enterprise digital transformation
The Department came to a Post-00 test paper king who took out 25K. The veteran said it was really dry, but it had been
数据库系统原理与应用教程(006)—— 编译安装 MySQL5.7(Linux 环境)
I'm a senior test engineer who has been outsourced by Alibaba and now has an annual salary of 40w+. My two-year career changing experience is sad
Task. Run(), Task. Factory. Analysis of behavior inconsistency between startnew() and new task()
Comment utiliser le langage MySQL pour les appareils de ligne et de ligne?
【LeetCode】43. String multiplication
程序员职业生涯真的很短吗?
基于PHP的轻量企业销售管理系统