Code covered by the BSD License  

Highlights from
SerialIO mex

Be the first to rate this file! 10 Downloads (last 30 days) File Size: 5.65 KB File ID: #25478

SerialIO mex

by Lorgio Teodovich

 

03 Oct 2009 (Updated 14 Nov 2009)

SerialIO is written in C and allows you to read/write to a serial or com port.

| Watch this File

File Information
Description

Although MATLAB is supposed to support both writing and reading data from the serial port, reading data seems to either produce no result, generate an error, or crash MATLAB. To remedy the situation SerialIO() has been written. This function will allow you to set/get vectors of data from the serial port buffer.
To help, type "SerialIO('help');

s = SerialIO('open','com2',19200);
SerialIO('write',x,3);
y=SerialIO('read',6);
SerialIO('close');

Note: to compile "mex SerialIO.cpp"

MATLAB release MATLAB 6.5 (R13)
Other requirements Lcc compiler. If I set Visual Studio 2005-8 --> Project Properties --> Project Defaults --> Character set to: "Use Multi-Byte Character set"
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
06 Oct 2009 James

Lorgio.

I am using R2009b on a 64 bit Windows 7 machine with Visual Studio 2008 as my compiler.

When I try to compile I get:

>> mex SerialIO.cpp
SerialIO.cpp
SerialIO.cpp(73) : error C3861: 'sleep': identifier not found
SerialIO.cpp(77) : error C3861: '_endthreadex': identifier not found
SerialIO.cpp(89) : error C3861: 'sleep': identifier not found
SerialIO.cpp(93) : error C3861: 'sleep': identifier not found
SerialIO.cpp(392) : error C3861: '_beginthreadex': identifier not found
SerialIO.cpp(441) : warning C4267: '=' : conversion from 'size_t' to 'int', possible loss of data
SerialIO.cpp(442) : error C2440: '=' : cannot convert from 'void *' to 'char *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
SerialIO.cpp(458) : error C2440: '=' : cannot convert from 'const mxArray *' to 'mxArray *'
        Conversion loses qualifiers
SerialIO.cpp(460) : error C2440: '=' : cannot convert from 'void *' to 'char *'
        Conversion from 'void*' to pointer to non-'void' requires an explicit cast
 
  C:\PROGRA~1\MATLAB\R2009B\BIN\MEX.PL: Error: Compile of 'SerialIO.cpp' failed.
 
??? Error using ==> mex at 221
Unable to complete successfully.
 
It probably is my system confihuration. I'm not a c++ programmer.
Any Suggestions.
Thanks
James

06 Oct 2009 James

Largio.

Made the following changes to the source code and got a clean compile and it runs help ok.
Haven't tried any other functionality yet.

line 32 commented out #define Thread
added cast to lines 442,458,460

Do you think it will work with these changes
Thanks
James

06 Oct 2009 Lorgio Teodovich

James, try to compile http://www.mathworks.com/matlabcentral/fileexchange/21233
. Or add "
// undef needed for LCC compiler
#undef EXTERN_C
#include <process.h>".

I am using R2007a on a 32 bit Windows XP Sp2.

12 Nov 2009 Chris Karl

Hi Lorgio, I gave up compiling SerialIO within matlab. Correct me if I am wrong but the maltab internal lcc compiler won't compile serialIO.cpp correctly and hence a c++ compiler should be used. I set up my vc++ compiler (Visual C++ 2008) to compile the mex file via vc++ (see http://www.icaen.uiowa.edu/~dip/lecture/C++_with_Matlab.pdf). I am getting following errors (seems some of the syntax has c slang that the c++ compiler rejects):

1><path>\visual studio 2008\projects\mexserialio\mexserialio\serialio.cpp(332) : warning C4996: 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> f:\vc++\vc\include\stdio.h(366) : see declaration of 'sprintf'
1><path>\visual studio 2008\projects\mexserialio\mexserialio\serialio.cpp(363) : error C2664: 'CreateFileW' : cannot convert parameter 1 from 'char [255]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1><path>\visual studio 2008\projects\mexserialio\mexserialio\serialio.cpp(403) : error C2664: '_beginthread' : cannot convert parameter 1 from 'unsigned int (__stdcall *)(void)' to 'void (__cdecl *)(void *)'
1> This conversion requires a reinterpret_cast, a C-style cast or function-style cast
1><path>\visual studio 2008\projects\mexserialio\mexserialio\serialio.cpp(453) : error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1><path>\visual studio 2008\projects\mexserialio\mexserialio\serialio.cpp(469) : error C2440: '=' : cannot convert from 'const mxArray *' to 'mxArray *'
1> Conversion loses qualifiers
1><path>\visual studio 2008\projects\mexserialio\mexserialio\serialio.cpp(471) : error C2440: '=' : cannot convert from 'void *' to 'char *'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>Build log was saved at "file://<path>\Visual Studio 2008\Projects\mexSerialIO\mexSerialIO\Debug\BuildLog.htm"
1>mexSerialIO - 5 error(s), 1 warning(s)

Any ideas? Feedback highly appreciated. In the original code, sleep was written lower case. Changing e.g. 'sleep(5)' to 'Sleep(5)' removed 3 errors.... quite picky compiler....

Thanks a lot!

Chris

Please login to add a comment or rating.
Updates
10 Oct 2009

Change #include <process.h>.

31 Oct 2009

C

14 Nov 2009

Add visul 2005-8 compatibility.

Tag Activity for this File
Tag Applied By Date/Time
serial Lorgio Teodovich 05 Oct 2009 11:38:21
communications Lorgio Teodovich 05 Oct 2009 11:38:21
embedded code Lorgio Teodovich 05 Oct 2009 11:38:21
rs232 Lorgio Teodovich 05 Oct 2009 11:38:21
fopen Lorgio Teodovich 05 Oct 2009 11:38:21
com Lorgio Teodovich 05 Oct 2009 11:38:21
com2 Lorgio Teodovich 05 Oct 2009 11:38:21
mex Lorgio Teodovich 05 Oct 2009 11:38:21
serial com Lorgio Teodovich 05 Oct 2009 11:38:21
com1 Lorgio Teodovich 05 Oct 2009 11:38:21
comm Lorgio Teodovich 05 Oct 2009 11:38:21
com Bishnu 21 Mar 2010 02:38:42

Contact us at files@mathworks.com