Why do I get 'An error occurred during writing' error when writing data to a virtual serial port?

31 views (last 30 days)
I am a using a virtual COM serial port (created with drivers from an USB-RS232 adapter, Bluetooth adapter, or TCP/IP COM port redirector), and the serial communication object in MATLAB version R2013a (or equivalent serial object in the Instrument Control Toolbox).
However, upon writing data to the port with FPRINTF or FWRITE I get an 'Unexpected Error: An error occurred during writing.', even though the data has been written successfully to the device.
s=serial('COM4');
fopen(s);
fprintf(s, 'abc');
 
Error using serial/fprintf (line 144)
Unexpected Error: An error occurred during writing.
A similar behavior and error message is observed with FWRITE:
s=serial('COM4');
fopen(s);
fwrite(s, [97 98 99]);
 
Error using serial/fwrite (line 199)
Unsuccessful write: An error occurred during writing.
I have tested that a 3rd party software can write data to the virtual serial port with no error.
Why do I get these errors and what can I do about it?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Jun 2017
This error is caused by some virtual serial port drivers not supporting functionality equivalent to a physical serial port, and the way MATLAB handles communication with the virtual serial port.
A possible solution would be to upgrade to the latest drivers (provided by the vendor of the serial port adapter).
If the error persists even with the latest drivers for the virtual serial port adapter, there is the possibility to use an alternate means of communication with serial COM ports through the VISA interface available through the Instrument Control Toolbox.
The VISA software libraries provided by Agilent/Keysight (Agilent IO Libraries) handle serial COM port communication separately from the MATLAB serial port interface.
The Agilent (Keysight) IO Libraries can be installed by installing the "Instrument Control Toolbox Support Package for Keysight IO Libraries and VISA Interface" with MATLAB support packgage installer or downloaded at the following URL:
To add the virtual serial port as a VISA resource name in Agilent Connection Expert:
1. Open Agilent Connection Expert
2. Right click on on the desired serial port item ('COMxx') located in the 'Instrument I/O on ths PC' column, and select "Add Instrument" from the pop-up context menu.
3. In the following dialog box, uncheck "Auto-identify this instrument" and click OK.
4. Verify that the virtual serial port has been assigned a VISA resource name (for example ASRL4::INSTR)
5. Open MATLAB and run the following commands in the command window to show the VISA object constructor name corresponding to the VISA resource name:
visaInfo=instrhwinfo('visa', 'agilent')
visaInfo.ObjectConstructorName
An example code for virtual serial port communication with MATLAB Instrument Control Toolbox and the VISA interface:
v=visa('agilent', 'ASRL4::INSTR');
fopen(v);
fprintf(v, 'abc');
fwrite(v, [97 98 99]);
fclose(v);
% clean up
delete(v);
clear v;

More Answers (0)

Categories

Find more on Instrument Control Toolbox in Help Center and File Exchange

Products


Release

R2013a

Community Treasure Hunt

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

Start Hunting!