当前位置:网站首页>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
边栏推荐
- 用手机在同花顺上开户靠谱吗?这样有没有什么安全隐患
- [daily news]what happened to the corresponding author of latex
- 怎么用MySQL语言进行行列装置?
- Seate中用了shardingjdbc 就不能用全局事务了吗?
- Huawei issued hcsp-solution-5g security talent certification to help build 5g security talent ecosystem
- What is the digital transformation of manufacturing industry
- 數據庫系統原理與應用教程(006)—— 編譯安裝 MySQL5.7(Linux 環境)
- vim用户自动命令示例
- 电脑照片尺寸如何调整成自己想要的
- vscode 查找 替换 一个文件夹下所有文件的数据
猜你喜欢

【Hot100】19. 删除链表的倒数第 N 个结点

IM即时通讯开发实现心跳保活遇到的问题
![[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes](/img/73/98e4847783be26d86d147425ce3ecd.jpg)
[open source data] open source data set for cross modal (MRI, Meg, eye movement) human spatial memory research based on virtual reality scenes

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

怎麼用MySQL語言進行行列裝置?

瑞典公布决定排除华为5G设备,但是华为已成功找到新出路

高端程序员上班摸鱼指南

Idea start command line is too long problem handling

怎么用MySQL语言进行行列装置?

【LeetCode】43. String multiplication
随机推荐
Authentication processing in interface testing framework
Zabbix2.2监控之系统及应用日志监控报警
【Hot100】20. 有效的括号
process. env. NODE_ ENV
Can't global transactions be used when shardingjdbc is used in seate?
苹果自研基带芯片再次失败,说明了华为海思的技术领先性
Analysis of PostgreSQL storage structure
Go 语言源码级调试器 Delve
Go language learning notes - Gorm use - table addition, deletion, modification and query | web framework gin (VIII)
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
高端程序员上班摸鱼指南
Problèmes rencontrés dans le développement de la GI pour maintenir le rythme cardiaque en vie
Smart Party Building: faith through time and space | 7.1 dedication
超视频时代,什么样的技术会成为底座?
学会了selenium 模拟鼠标操作,你就可以偷懒点点点了
When ABAP screen switching, refresh the previous screen
瑞典公布决定排除华为5G设备,但是华为已成功找到新出路
程序员职业生涯真的很短吗?
Share the daily work and welfare of DJI (Shenzhen headquarters) in Dajiang
电脑照片尺寸如何调整成自己想要的