serial monitoring method to test communication via com ports without a serial communication device

╄→гoц情女王★ 提交于 2021-01-29 07:46:42

问题


I have a Verilog code simulated and synthesized on ISE design toolkit. I've got an FPGA spartan 6 device which is to be used for the implementation. But there is a problem with the device (probably a power issue) which makes the device unavailable in any of the COM ports when I connected it to my PC. So I want to check whether my Matlab code which I made for serial communication through the device does the desired job. So I need a method to test serial communication via any of the COM ports without connecting a serial com device to the PC. Is there any such method that I can Tx Rx serial data from Matlab to COM ports? Any software or any other method would be highly appreciated :)


回答1:


I found a way to test Matlab serial communication using virtual serial ports.

Download "Freeware Virtual COM Ports Emulator" from: http://freevirtualserialports.com/
I installed it in Windows 10, and it's working (as trial).

Add a pair of two serial ports:

Execute the following Matlab code sample to verify it's working:

s3 = serial('COM3','BaudRate',115200);
s4 = serial('COM4','BaudRate',115200);

fopen(s3);
fopen(s4);

fwrite(s3, uint8([1, 2, 3, 4, 5]));
%fprintf(s3, '12345');
pause(0.1);

RxBuf = fread(s4, 5)

fclose(s3);
delete(s3);
clear s3

fclose(s4);
delete(s4);
clear s4

The output is:

RxBuf =

     1
     2
     3
     4
     5

Bypassing the problem "it only stays for a single test session".
There is a problem when creating a pair of virtual ports using the software, it only stays for a single test session.
I guess it's a problem with the COM port emulation software.
The following solution, is not a good practice (and not a true solution).

  • Declare the serial object as global, keeping the object persistent.
  • Create the serial object only if it's not created.
  • Don't delete and don't clear the serial object.

See the following code sample:

global s3 s4

if isempty(s3)
    s3 = serial('COM3','BaudRate',115200);
end

if isempty(s4)
    s4 = serial('COM4','BaudRate',115200);
end

fopen(s3);
fopen(s4);

fwrite(s3, uint8([1, 2, 3, 4, 5]));
pause(0.1);

RxBuf = fread(s4, 5)

fclose(s3);
%delete(s3);
%clear s3

fclose(s4);
%delete(s4);
%clear s4

You can also look for a better virtual COM port software.




回答2:


As Rotem suggested, if you need to communicate via serial line between 2 program of your PC you need a virtual COM port emulator.

It seems you are running on Windows OS so I would recommend a completely free emulator (not a trial one). For Windows I use com0com Null-modem emulator (from SourceForge).

In the example below I will show how to communicate with "another" device so Matlab will not handle both side of the communication. The other device will be simulated by a simple terminal. For windows I use RealTerm: Serial/TCP Terminal (also from SourceForge).


Setup:

Execute the setup of both program with all default options. by default com0com will create a virtual pair COM3/COM4 but if these port already exist on your system the program may assign other numbers. Check the numbers before you run the example. (it will also create a CNCA0/CNCB0 pair but you can ignore this one for now).

For RealTerm, once installed (don't forget to activate the server registration at the end of the setup, it should be ticked by default though), it will look like below. Keep all default options, just set the port number and the baud rate if they need to be changed.


Test MATLAB -> Terminal

You are ready to send Ascii characters or binary values from MATLAB to your device. The animation below shows you an example of both option:
you can click on the picture to see it full size. It is running in loop so you may want to wait until it restart from the beginning.

Test Terminal -> MATLAB

Below animation shows you how to test the communication in the other way:

Don't forget to tick [CR] [LF] on RealTerm when you send Ascii characters and want to use the '%s' format specifier on MATLAB, as it needs these characters to detect the end of the string.


Note:

  • If you have another terminal program that you are more used too, it will work the same.
  • If the RealTerm option does not suit you, or if you want to handle both sides of communication from Matlab, then you can use the code provided by Rotem in his first answer. Just install com0com but ignore all the RealTerm part.


来源:https://stackoverflow.com/questions/38663949/serial-monitoring-method-to-test-communication-via-com-ports-without-a-serial-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!