Mbed and serial matlab communication

29 May 2012

I want to send and receive data between the mbed and matlab. That should not be a problem, just using the serial drivers (which are installed yes).

So simple Matlab script: s1=serial('COM5'); fopen(s1); fprintf(s1, 'Bwah'); fclose(s1);

On computer 1, windows Vista, that works fine. On computer 2, windows XP, it does not work. On computer 3, windows 7, it does not work.

In both cases Matlab tells me: Error using serial/fprintf (line 144) Unexpected Error: An error occurred during writing.

A similar error happens when using fwrite instead.

If I use TeraTerm it works fine. Now before you tell me to go to the Matlab forums, Matlab works fine if I write to another serial port. So the problem is really between Mbed and Matlab. The writing part also does occur, the error happens after the (virtual) serial port gets its data. But with matlab programs exitting due to an error that does not really help me. Reading seems to be working normally.

With some googling the only relevant information I could find was:

Quote:

To determine if the data has been written completely, the MATLAB serial object tries to flush the data out after writing. Some older USB->RS232 drivers do not properly support this operation resulting in an error. Make sure that you have the latest driver from the vendor of your USB->RS232 dongle as these drivers have improved significantly in recent years.

Which is nice and all, but it does not explain why it works on computer #1, and it really doesnt help me with how to solve this. Also computer 2 and 3 have nothing in common (one is a home laptop, other one is work computer, also different matlab versions, but all pretty up to date).

So has anyone encountered this before, and/or any clue how to fix this?

Edit: Well I was already searching a while, but of course right after I post a topic I find a kinda 'solution'. In matlab code use:

try
    fprintf(s1, 'Vroem');
end

(And you can add a catch if you want, and then do nothing in it, Matlab now tells you you are a bad person for not using catch). It isn't exactly what you call nice way of programming, but it does work. Sending one string, and receiving two strings for over 3000 times without opening/closing the comport in matlab does not generate any errors (I was scared of bufferoverflows, if the original error prevented a buffer from being cleared). Still it would be nice if anyone knows a better way to deal with this.

btw, formatting on these forums is horrible...

15 Sep 2012

Thanks. Your fix worked for me.

07 Dec 2012

Thanks, but here it so happens that the try statements sometimes fail and never succeeds for that data set. Which means i lose the required data.

Any idea how to resend the same data ? I have tried pause and etc.. It doesn't seem to be working. My setup include, Windows8, Matlab 2012a and Windows7, Matlab 2011a.

Its urgent, any help is appreciated..

Thanks again

07 Dec 2012

If it sometimes fails and not always that seems like a whole different issue.

The idea here was that the try statements always fail, but the data is still send (the printfs throw an exception, but only after the data is sent to the mbed). So does the data not arrive at your mbed? Then it is another problem. Do take into account you can only use one printf per try statement

For resending the same data you can add some kind of handshaking, if the mbed hasnt received all data, it asks for it to be resent.

07 Dec 2012

Hi Erik,

Yeah. It fails always in my case too if I don'y use try, catch. After putting try, catch it started working sometime. But the problem there is if I am sending data one-after the other at high rate, it results in failure. However, I found that at 460800 baud, this problem doesn't occur.. and mbed shouldn't do any file operations.

This looks to me like timing issue and possible problem in the driver.

07 Dec 2012

That we have to use such a workaround in the first place is of course not normal, that shouldnt be required. So something is going wrong, and probably until someone figures out how to fix it we should be happy it does something at all.

I have to say that I have had it working at max baudrate without issues. But that could be a completely seperate issue.

07 Dec 2012

Thanks for the quick response. Hopefully, someone would fix it. Can we report the bug to any bug trakcing system ? I am new to mbed. Please let me know if there is anytihng of that sort.

07 Dec 2012

There is a bug forum also. Although with such issues it is unclear who's fault it is. It actually works fine with matlab 2009 and earlier, so matlab is at least partially responsible.

07 Dec 2012

May be that's the case. Not sure. I am using 2012a and i have tried even 2011a

04 Mar 2013

have the same problem. is there a better solution available or do you guys still use your workaround ?

04 Mar 2013

Still using the same workaround, not ideal, but it works. Granted I haven't tried to fix it in a better way, but I also doubt that is easy for us. I expect that either Matlabs serial handling has to be changed, which probably won't happen, or the mbed serial drivers, which is probably a fairly low priority.

04 Mar 2013

okay. just for record: I'm using matlab2012b and it seems to work fine with your workaround. thank you!

20 Jan 2015

Alright, I know this is an old issue, but there is a better workaround. It seems that the issue arrives from the "mode" being used in fprintf. When calling fprintf, use 4 arguments as follows "fprintf(serialObj,format,cmd,mode)" where:

serialObj - is an open serial COM port, e.g. serialObj = serial('COM4'); format - formatting string for your output, e.g. format = '%d\n'; cmd - value(s) to be written, e.g. cmd = 5; mode - this can be set to mode = 'sync' or mode = 'async'. For some reason, mode = 'async' seems to eliminate this error.

20 Jan 2015

I doubt the fixes the issue. All mode does is handle whether the fprintf command is blocking or non-blocking. Shouldn't impact any serial port errors.

23 Jan 2015

The MATLAB "fprintf" error (Line 144) is associated with the mode selection (see line 144 of "fprintf.m"). Regardless of the selected mode, the message is sent. The error occurs when MATLAB attempts to execute the following line: "fprintf(igetfield(obj, 'jobject'), formattedCmd, mode);"

This line attempts to print the formatted message to the java handle associated with the serial port, and specifies the mode for printing. When the mode is set to 'sync', an error is thrown. When the mode is set to 'async' no error is thrown. The easiest thing to do is try it for yourself.

25 May 2015

@Michael : Great. Thanks. Your solution seems to work. But as soon as i read whatever i have sent to mbed i get a timeout warning. Other than that it work fine. Any fix for this ?

03 Aug 2015

Do you get a timeout warning on the MATLAB side? If so, it sounds like the problem may be with the definition/use of terminators.