communication with multiple serial ports

hello,
I have issues for the communication with two serial ports. Each port works fine when tested alone while, when tested together, one of the ports has a significant delay in the transmission of the data..does anybody have a solution to this?

5 Comments

How are you communicating with them? Are you using callbacks? Are you using Simulink blocks?
Are these real serial ports, or are they serial being emulated over USB? If they are on USB, then are they on the same USB controller?
Hi Walter,
I am using Matlab for the communication. I acquired the data from the two USB ports and I read them like serials. I don't know what you mean with controller, but I have created one different object for each serial.
Here there is my code:
%%Create serial object for dongle
sHex = serial('COM3','BaudRate',250000); % change the COM Port number as needed
%%Connect the serial port
try
fopen(sHex);
catch err
fclose(instrfind);
error('Make sure you select the correct COM Port where the Hex dongle is connected.');
end
%%Opening communication
sHex.Terminator = 'CR';
command='M&f22\ss1$\r';
dec=sum(double(command));
checksum=dec2hex(dec);
fprintf(sHex,strcat(['M&f22\ss1$\r/',num2str(checksum)]));
pause(5)
command='T&d=1F40\sn=04\r';
dec=sum(double(command));
checksum=dec2hex(dec);
fprintf(sHex,strcat(['T&d=1F40\sn=04\r',num2str(checksum)]));
pause(2)
sHex.Terminator = '%';
%%Create serial object for Arduino
sModel = serial('COM4','BaudRate',9600); % change the COM Port number
as needed
%%Connect the serial port to Arduino
try
fopen(sModel)
catch err
fclose(instrfind);
error('Make sure you select the correct COM Port where the Arduino is connected.');
end
while 1
dataModel=fscanf(sModel,'%e');
inputString = fscanf(sHex,'%s');
%processing data here:
.........
end
%%Closing communication
%Dongle
sHex.Terminator = 'CR';
pause(1)
command='!&%\r';
dec=sum(double(command));
checksum=dec2hex(dec);
fprintf(sHex,strcat(['!&%\r',num2str(checksum)]));
fclose(sHex)
%arduino
fclose(sModel);
Please download HWiNFO64 or HWiNFO32, such as from http://www.majorgeeks.com/files/details/hwinfo64.html
When you install the program and run it, it will give you a system summary, which you can close and ignore for this purpose. But it will also give a device tree, probably towards the left. Expand the Ports section and expand the USB section after that:
Notice the top-level items below that, listed as "host controller". Each of those represents a different bus that can transfer data independently of the other busses. However, if you have multiple devices on the same controller, then they will share the bus and cannot transmit (or receive) at the same time as the other devices on the same controller.
In the diagram, notice that [Port2] of the Root Hub of the first controller already says "No Device Connected". That is a device (or lack of device) directly connected to the controller, such as a typical USB connector in a laptop. But notice that [Port1] says "USB Hub". That is a series of devices sharing a single USB connector. In my case, that particular hub has 6 slots, and the built-in laptop camera is connected to one of the slots.
Not visible in this screen snapshot is a third Host Controller (third independent USB bus.) That one has attached a bluetooth controller, an infrared controller, and the keyboard (with trackpad).
You might notice that on the second Host Controller (second independent USB bus), that [Port1] is also a "USB Hub". That one happens to be a small third-party USB hub that I have attached to one of my laptop USB connectors; one of the ports there has an external mouse plugged into it. But on that same Host Controller, [Port6] is "USB Attached SCSI (UAS) Mass Storage Device" -- an external hard drive that I have plugged into my laptop. The external mouse attached to [Port1] of that controller cannot send or receive data at the same time as the hard drive on [Port6] because they are sharing the single serial bus that is managed by the second Host Controller. However, if I happened to be sending something through bluetooth, which is on Controller 3 [Port8] USB Hub [Port1] USB Hub [Port3], then it would be able to send and receive at the same time as the hard drive on Controller 2 [Port6] because they are on different busses (different controllers.)
Now, you need to find the two USB serial objects you are using for your devices. If they are attached to the same Host Controller, then they cannot send and receive at the same time, which might explain the delays you are encountering.
As a guideline: if your two USB devices are plugged into USB connectors that are directly adjacent to each other, then chances are that they are on the same USB controller. If there is a different kind of device (such as a microphone, SD card, or firewire) between the two slots then the chances go up that they are not on the same USB controller; also if the two slots are on different sides of the same laptop then the chances go up that they are not on the same USB controller. If you are using a desktop, then the same kinds of guidelines apply. However, the less expensive your computer is, the more likely that they cut corners by using a single USB controller for all of the accessible slots.
Hi Walter,
thanks for the answer. I have installed the HWiNFO64 program, and I found out that I have 3 host controllers. I have attached the snapshot of the HWiNFO64 program.
However, every USB port seems to be connected to the third host controller only. Even when I connect the USB device to a USB port on a different side of the deskop, the USB device get always connected to the third host controller.
Is there a way to assign a USB port to a specific host controller?
thanks Fra
Unfortunately the connection between devices and controller is electrical not under program control.
As you have a desktop you could try adding a USB controller card or two. The USB2.0 ones should be inexpensive, but if you plan to keep the system around for a while then USB 3.0 or 3.1 are more likely to be useful in future. Be careful about what kind of connector you get. And research the card before buying: the inexpensive mass market USB cards tend to be suitable for consumer goods like printers and webcam not designed for low latency high performance.

Sign in to comment.

Answers (0)

Asked:

on 14 Dec 2016

Commented:

on 9 Jan 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!