0
votes

I have been trying to write to a serial port using code in Matlab. However everything that I try first results in an error message and then causes Matlab to think that the port is inaccessible.

The matlab code that I am using is as follows:

function test()

TIMEOUT = 5;    %time to wait for data before aborting
XPOINTS = 50;   %number of points along x axis

%create serial object to represent connection to mbed
mbed = serial('COM18','BaudRate', 9600, 'DataBits', 8);   %change depending on mbed configuration

%set(mbed,'Timeout',TIMEOUT);        %adjust timeout to ensure fast response when mbed disconnected

fopen(mbed);        %open serial connection
input = 1;
fprintf(mbed, input);
x=0;
while (x == 0)        
    values = fscanf(mbed, '%d');  
    disp(values);       
end

fclose(mbed);

end

The error message that appears is

Error using serial/fprintf (line 144)
Error: An error occurred during writing.

Error in test (line 14)
fprintf(mbed, input);

My main problem is that from all that I can find online it seems to say that the fprintf command should work. I have also tried the line

fwrite(mbed, input);

which comes up with essentially the same error message.

Once I have tried this once the next error message that I receive is:

Error using serial/fopen (line 72)
Open failed: Port: COM18 is not available. Available ports: COM1.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.

Error in test (line 12)
fopen(mbed);        %open serial connection

Which I can only seem to fix by saving my program and then opening the exact same one. The mbed is definitely connected to the right com port at the time of the attempt.

My question is where am I going wrong with the fprintf line? Is that the correct way to communicate with a serial port or an mbed?

2
"Use INSTRFIND to determine if other instrument objects are connected to the requested device." Did you actually try this? Since you haven't closed the old serial port object, the hardware is already in use.Ben Voigt
I haven't actually, I shall go and properly use that thank you. That's probably what is causing the second error.Jack Schofield

2 Answers

1
votes

There are not very many ways that a serial port write can fail:

  • If the serial port disappeared (for example by unplugging a USB->serial adapter)
  • If the kernel write buffer is full (you wrote a HUGE amount of data at a faster rate than it can flow out the port)
  • If data isn't flowing out the port at all, due to flow control, and the timeout elapses

You need to check your flow control options, if your device doesn't support RTS/CTS, but your code leaves hardware handshaking enabled, communication will fail.

1
votes

The problem has been fixed by adding the following to the line which was trying to originally write the code:

Old line :

fprintf(mbed,input);

New line :

fprintf(mbed, '1', 'async');

I don't know why this has fixed it, but it has. This may well be useful to future people trying to write to an mbed.