当前位置:网站首页>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

原网站

版权声明
本文为[Full stack programmer webmaster]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/182/202207011611026976.html