Cannot create serial port: Error using serial (line 120)?

Hi, I have been using MATLAB to connect to a virtual USB serial port for the past few months. However, today I have started to get this error message:
Error using serial (line 120) Cannot create: No constructor with appropriate signature exists in Java class com.mathworks.toolbox.instrument.SerialComm
.
Below is the code I am using:
AComPort = GetAvailableComPort; % Get all available com ports. (borrowed function, can be found online)
y = length(AComPort); % Get how many com ports are available
SPort = AComPort(y); % By default choose last port on list
BRate = 115200; % By default choose 115200 baud rate
myComPort = InitPort(SPort,BRate); % Initialise and open com port
.
This is my own function for initialising ports:
function [ myComPort ] = InitPort( SPort, BRate )
%InitPort - Initialises a com port
obj=instrfind; % Clear all com ports ensuring they are available
if ~isempty(obj);
delete(obj);
end;
myComPort = serial(SPort,'BaudRate',BRate,'DataBits',8,'StopBits',1,'Terminator',0);
% Initialise a port called myComPort
fopen(myComPort); % Open port for read/write
end
.
Neither logging off and logging back in again or re-starting the computer have helped.
This is making it impossible for me to connect to the serial device. Does anyone have any ideas on how to solve this issue?
Thanks,
Jasmine

7 个评论

What is SPort defined to be? Consider putting a breakpoint at line 118 of serial.m and then re-run the code. When the debugger stops at this line, type varargin{1} in the Command Window and see what appears.
SPort is calculated to be the maximum available ComPort. Currently this is 'COM5' however I have designed a PCB with a FTDI chip on it so I need my code to be able to automatically choose a sensible port. (don't worry I have also made it possible for the user to define a port, should something go wrong with the GetAvailableComPort function.
The result of making a breakpoint and typing varargin{1} at the command line is:
ans =
[]
I'll upload my code.
Won't that be a problem, if the port is empty/null?
Yes, it would be.
I have tried again by using user-defined port settings and it worked. I have also now tried again letting the program decide which port to use, and today it is working.
However, every time I restart MATLAB I have to start by using user defined settings and then it works.
Any ideas about why this error still occurs if trying to use GetAvailableComPort first would be much appreciated.
Are you sure that the serial ports, once opened, are being closed via your ClosePort(myComPort)? If still open, then they won't be "seen" by the GetAvailableComPort call.
It would be interesting to see which ports are being used through the run of your GUI. Is it the same one, every time you invoke myComPort = InitPort(SPort,BRate); or is it a different one? Just add an fprintf('port: %s\n',SPort); statement before or after each InitPort to see what is happening.
I've worked out why it wasn't working. My own silly mistake.
I was using a cut-down version of my code as I wanted to debug a simple version before re-adding in the extra features, including a user set port. Only I had a variable called SettingsPressed which went high if the user had set port settings. Otherwise it was low and an if statement said 'if low run GetAvailableComPort '.
However, since my cut-down code didn't set SettingsPressed, GetAvailableComPort didn't get run and the user hadn't set up the port.
I think this fully explains why ans = [] when typing varargin{1} at the command line.
Thank you for suggesting that, I don't think I would have realised what my issue was otherwise.

请先登录,再进行评论。

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Programming 的更多信息

产品

提问:

2014-7-16

Community Treasure Hunt

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

Start Hunting!

Translated by