COM(n) -- Interfaces Through Serial Port

1,147 views
Skip to first unread message

KE...@comcast.net

unread,
Feb 3, 2012, 2:42:24 PM2/3/12
to freemat
Hi,

I use WindowsXT and I wish to talk to an external device (it is a home
built device). The computer has two serial ports and I would like to
control by sending and receiving through the port. Does FreeMAT talk
to the comm port? In Basic there is a COM statement (OPEN”COM1:); is
there a similar command in FreeMAT? I looked up the PCI serial port
for a COM 3 and it gives an I/O range of A800 – A807.

Thank you,
Jim Bull
KE...@comcast.net

KE...@comcast.net

unread,
Feb 1, 2012, 11:21:56 PM2/1/12
to freemat

jonw0224

unread,
Feb 13, 2012, 9:56:19 AM2/13/12
to freemat, KE...@comcast.net
Jim,

I quickly put together an implementation of a serial.dll (written in
C) for you with a serial.m to import the functions into FreeMAT (this
is based on code I wrote for another project). Then you would simply
call the functions according to the comments contained in serial.h.
I've not tested the intergration into FreeMAT, beyond opening and
closing the serial port. Everything I'm seeing indicates it should
work.

You can download a zip file with the dll, m file, and all of the
source files, etc here:

http://jonathanweaver.net/SerialDll.zip

-Jonathan

Jonathan Weaver

unread,
Nov 4, 2012, 11:45:36 PM11/4/12
to fre...@googlegroups.com, KE...@comcast.net
I'm retiring my website and I've posted the file at jonw0224.tripod.com/SerialDll.zip

-Jonathan

Jonathan Weaver

unread,
Feb 11, 2013, 11:57:47 AM2/11/13
to fre...@googlegroups.com, KE...@comcast.net
A year later, I finally got around to testing this.  I made some changes to get serialRXChar to work correctly.  Sorry to anyone who downloaded it before.  I also added an example.m file to help people know how to use it.  The link is the same:

jonw0224.tripod.com/SerialDll.zip

Thanks,

Jonathan

briank...@gmail.com

unread,
Mar 7, 2013, 2:44:12 PM3/7/13
to fre...@googlegroups.com, KE...@comcast.net

briank...@gmail.com

unread,
Mar 7, 2013, 2:48:38 PM3/7/13
to fre...@googlegroups.com, KE...@comcast.net
On Monday, February 11, 2013 4:57:47 PM UTC, Jonathan Weaver wrote:
Thanks Jonathan, this is really useful. How difficult would it be to add EscapeComm to control DTR/RTS? I use these to power simple serial boards. Cheers

Jonathan Weaver

unread,
Mar 8, 2013, 4:51:08 PM3/8/13
to fre...@googlegroups.com, KE...@comcast.net
Brian,

I made an attempt at providing some functions to control DTR/RTS.  I've not had a chance to test it, but you can download the changes I've made (same link).  I may get a chance to test it over the weekend or next week.  Feel free to try it and let me know if it works.

Thanks,

Jonathan

briank...@gmail.com

unread,
Mar 14, 2013, 5:10:18 AM3/14/13
to fre...@googlegroups.com, KE...@comcast.net
Thanks Jonathan, sorry for the delay I appear to have mangled my machine but will test and report as soon as I am operational again!

briank...@gmail.com

unread,
Mar 14, 2013, 8:49:24 AM3/14/13
to fre...@googlegroups.com, KE...@comcast.net
My machine is now working (I think).  I tried the dll from the source directory - the other does not seem to be updated - and got the following error:-
//..................
In docli(builtin) at line 66
    In base(base)
    In base()
    In global()
Error: malformed import function prototype - error starting at
//..................
It did stop at 'at'.
I have had a quick look at the dll etc and cannot see anything wrong, I wonder if import is working properly.
I am running Win7-64 and FreeMat 4.2 beta.
I can try on MATLAB at the weekend.  Any ideas?

Thanks

Jonathan Weaver

unread,
Mar 15, 2013, 1:57:11 PM3/15/13
to fre...@googlegroups.com, KE...@comcast.net
Brian,

It seems that since I've posted this twice without testing, I'd learn that doesn't work ;-)

I did a complete testing cycle and found two errors which you've pointed out.  The import commands were not correct in the serialload.m file.  Also, I had not included an update to the newly compiled Serial.dll (the one in the source directory was updated as you observed).

I got out my voltmeter and carefully probed the DTR and RTS lines and observed the successful toggle of the state of those lines from the FreeMat command line:

serialload
serialPortOpen('COM1', '57600,n,8,1')
setPortRTS(1) % Measured 7.62 V on pin 7 (I did this on a laptop with a 9 pin serial port, so the serial voltages are low)
setPortRTS(0) % Measured -5.91 V on pin 7
setPortRTS(1) % Measured 7.62 V on pin 7
setPortDTR(1) % Measured 7.62 V on pin 4
setPortDTR(0) % Measured -5.91 V on pin 4
setPortDTR(1) % Measured 7.62 V on pin 4
serialPortClose

Everything appears to work (at least on Windows7 64bit with FreeMat 4.1), if you will simply download it again.

Sorry, I didn't put forth the appropriate level of effort earlier.

Jonathan

briank...@gmail.com

unread,
Mar 16, 2013, 4:12:32 PM3/16/13
to fre...@googlegroups.com
Thanks Jonathan, I probably won't get chance to try it till Monday.
Please don't apologise, your generosity in time has helped me learn something - I rate that as very valuable!
Cheers

briank...@gmail.com

unread,
Mar 18, 2013, 3:47:13 AM3/18/13
to fre...@googlegroups.com
Jonathan
I can confirm that it does work now, thanks again.

bepa...@gmail.com

unread,
Jan 9, 2014, 3:25:14 PM1/9/14
to fre...@googlegroups.com, KE...@comcast.net
Dear Jonathan,

I am a new user of FreeMat and I have tried your serial.dll I figure out that it is properly working with FreeMat 4.2 32 bit, but not with FreeMat 4.2 64 bit. Could recompile your serial.dll for Win7 64 bits ?

Secondly, I need to manage two or more serial interfaces with the same instance of FreeMat (multiple devices control). In Matlab, I was using the handle returned by the serial() function for opening, interfacing or closing the port. How can I manage it with your dll ? If needed could you add a return handle in your dll similar to Matlab ?

Cheers,

Patrick 

Jonathan Weaver

unread,
Jan 10, 2014, 10:42:56 AM1/10/14
to fre...@googlegroups.com
Patrick,

I updated the library to return the handle when you open the serial port.  Now each function expects the handle to be passed so the library knows which serial port to use.

Also, I compiled both 32-bit and 64-bit versions.  I can confirm that the 32-bit version works in Freemat 4.1 and the 64-bit version works in Freemat 4.2.  I'm running 64-bit Windows 7.

Thanks for the catch, I didn't think about the 32-bit versus 64-bit machine code causing a problem.

The updated file is at http://jonw0224.tripod.com/SeriaDll.zip and overwrites the prior version.  Let me know how it works for you.

Jonathan
Message has been deleted

bepa...@gmail.com

unread,
Jan 18, 2014, 2:28:58 AM1/18/14
to fre...@googlegroups.com
Dear Jonathan,

First let me apologize for my delay in answering you. I only recently tested your last version in 64-bit: it properly works. For the 32-bit, I will do it later. The handle to the serial port is also fine: now I can open and manage several ports for interfacing multiple equipment. Only one remark with the hyperlink you gave in your last message: there should a typo in the link, so I used one of your previous message with the correct link which points on your latest dll release.

If I may, let me ask you if it would be possible to add a function to your dll allowing to read the number of available bytes in the RX buffer, but without clearing the RX buffer. I am used to use this property in the Matlab Serial function. As I get spontanous message from the equipment, I must know when a new message has arrived and if it is completed. Presently with your serialRXchar function, I cannot read the RX buffer status without retrieving data. If the message is complete this is fine, but if the message is partial it is more complicated to reconstructed after. 

By the way do you know what the is RX buffer size ? Is there any SW limitation ? If yes is it at least 1024 ?

Thanks a lot for your support and help.

Sincerly

Patrick

bepa...@gmail.com

unread,
Jan 18, 2014, 4:55:29 PM1/18/14
to fre...@googlegroups.com
Dear Jonathan,

First let me apologize for my delay in answering you. I tested your 64-bit DLL and it works fine. For the 32-bit I'll do it later. The handle for the COM part is fine too. Now I can interface multiple equipment with multiple COM port open. Only a minor remark: the hyperlink to your DLL is not correct in your last post. I could however get your DLL by using the hyperlink from a previous post.

If I may, I would like to ask you if you could add another function for reading the number of available bytes in the RX buffer, but not removing data from the buffer (this will be done by your serialRXchar when the expected number of byte is reaches). This feature is available in the Matlab Serial function and is very useful when getting spontaneous and repetitive data from an equipment, as I have.

Sincerly,

Patrick 

bepa...@gmail.com

unread,
Jan 18, 2014, 5:01:39 PM1/18/14
to fre...@googlegroups.com

Timothy Cyders

unread,
Jan 19, 2014, 12:55:27 PM1/19/14
to fre...@googlegroups.com
Apologies in the delay of posting these messages - we've had a problem with spam in the past, so we have to moderate all the messages that get sent to the board. I have been away from my computer for a couple of days, so they got a bit backed up. In the future, just send one message, so long as it's not obvious spam, it will get posted.


--
You received this message because you are subscribed to the Google Groups "freemat" group.
To unsubscribe from this group and stop receiving emails from it, send an email to freemat+u...@googlegroups.com.
To post to this group, send email to fre...@googlegroups.com.
Visit this group at http://groups.google.com/group/freemat.
For more options, visit https://groups.google.com/groups/opt_out.

bepa...@gmail.com

unread,
Jan 20, 2014, 2:03:09 PM1/20/14
to fre...@googlegroups.com
No problem, I just didn't know about your moderation. I thought I badly sent my post. Next time I will only post it once. Cheers. Patrick

Gab riel

unread,
Feb 17, 2014, 2:30:55 PM2/17/14
to fre...@googlegroups.com, KE...@comcast.net
Jonathan, thanks a ton for your serial code!  Though it only reads in a byte at a time, I have spent many hrs getting it to do what I want, and now I am reading in and logging data from my Arduino directly to a file.  If you want my code I can share it once I get it more tweaked and better documented/cleaned up.

Gab riel

unread,
Mar 12, 2014, 12:29:06 AM3/12/14
to fre...@googlegroups.com, KE...@comcast.net
Jonathan, in MATLAB, when I create a serial object, there are a bunch of fields that can be accessed.  For example, if I create a new serial object by calling "s = serial('COM4','BaudRate',115200,'Parity','none','DataBits',8,'StopBits',1,'terminator','LF');", I can then call s.BytesAvailable to see how many bytes are in MATLAB's serial buffer waiting to be read.  This is REALLY useful.  Do you have a similar function on your serial code for FreeMat?  If not, can you add it please?  The items available under an open serial object, in MATLAB, include the following, where "s" is my open serial object:
s.Port
s.BaudRate
s.Terminator
s.Status  (ex: closed)
s.RecordStatus
s.TransferStatus
s.BytesAvailable
s.ValuesReceived
s.ValuesSent

I would really appreciate you adding some or all of those features, more particularly BytesAvailable, Status, Port, BaudRate, Terminator, ValuesReceived, and ValuesSent.  

What do you think?  Also, I haven't found any other options for using serial in FreeMat, other than your code. If no better options exist I highly recommend you contact FreeMat and get your code added as a permanent addition to the FreeMat core.  

Please respond so I know you got this.

Sincerely,
Gabriel



On Sunday, November 4, 2012 11:45:36 PM UTC-5, Jonathan Weaver wrote:

Jonathan Weaver

unread,
Mar 13, 2014, 4:48:29 PM3/13/14
to fre...@googlegroups.com, KE...@comcast.net
Gabriel,

I had been planning on adding a function to return BytesAvailable per your request a few months ago and your latest message prompted me to push it up to the top for the day.  No worries, I liked the functionality myself, and I could think of a use for it if I could get the BytesAvailable without going through a communications timeout for an empty byte.  It turns out a call to the Windows function ClearCommError returns that data without a communications timeout and without affecting the buffer, so it was perfect for this application.

So, today, I added three functions, serialPortBytesAvailable, serialTXString, and serialRXString.  The new upload is available at:

http://jonw0224.tripod.com/freematlib.html

along with a few other functions I decided to share.  Obviously, this download is called "Serial".

This project is just a quick and dirty way to gain access to a serial port and read data into FreeMat and have FreeMat transmit data over a serial port.  I hope the new functionality will meet your needs.

Jonathan

Jonathan Weaver

unread,
Mar 17, 2014, 4:39:50 PM3/17/14
to fre...@googlegroups.com, KE...@comcast.net
Gabriel,

I added the bytesavailable functionality to the download.  I also added the ability to send and receive multiple characters at a time.

For convenience the link is http://jonw0224.tripod.com/SerialDll.zip or more generally, I have posted all of my FreeMat scripts to http://jonw0224.tripod.com/freematlib.html.

Have a good day and enjoy.

-Jonathan


On Wednesday, March 12, 2014 12:29:06 AM UTC-4, Gab riel wrote:
Message has been deleted

Gab riel

unread,
Mar 23, 2014, 10:38:12 PM3/23/14
to fre...@googlegroups.com, KE...@comcast.net
Jonathan, thanks a ton!  I look forward to using your new serial library.  I have learned how to interface Arduino to MATLAB really well, via serial, and I have made a MATLAB GUI that uses this serial interface for live data-logging and plotting from the Arduino, so now I need to get back to interfacing Arduino and FreeMat, for use by the general masses.  This, of course, depends on your serial library, so I greatly appreciate your help on this.  On a side note, do you know if FreeMat can make GUIs?

Sincerely,
Gabriel

Jonathan Weaver

unread,
Mar 24, 2014, 8:27:32 AM3/24/14
to fre...@googlegroups.com, KE...@comcast.net
I don't know much about the GUI tools in Freemat, but I know it has them.  Check out this post.

https://groups.google.com/forum/#!searchin/freemat/gui/freemat/_PGog_gSV5s/taIK3HGE5gwJ

Good luck on your data logging project.

Jonathan

Patrick Berthoud

unread,
Apr 17, 2014, 5:54:23 AM4/17/14
to fre...@googlegroups.com, KE...@comcast.net
Dear Jonathan,

Thanks for adding ByteAvailable function and possibility to send and receive strings (TXstring and RXstring). In my case everything works fine when working with character code 0 to 127. When I send characters or string with higher code (128 to 255), I read those characters with two' complement. For example sending 127, I receive 127, sending 128, I receive -128, sending 129 I receive -127 and so forth up to sending 255 I receive -1. This is the same for the RXchar function. I guess this is a casting problem within your dll. Would it be possible to get the RX on [0;255] = uint8 in order when sending e.g. 255 I receive 255.

Best regards,

Patrick

Patrick Berthoud

unread,
Apr 25, 2014, 2:56:14 AM4/25/14
to fre...@googlegroups.com, KE...@comcast.net
Dear Jonathan,

I have used your updated DLL. I noticed that if I send characters (char or string) with ASCII code greater than 127 (e.g 128 to 255), the read characters are interpreted as -128 to -1. Would it be possible to modify your DLL to allow reading character from 0 to 255 ? Maybe just a casting issue in the RX function (for char and string) by changing int ou unsigned int ?

Kind regards,

Patrick

Jonathan Weaver

unread,
Apr 25, 2014, 5:05:06 PM4/25/14
to fre...@googlegroups.com, KE...@comcast.net
Patrick,

Nice catch!  I fixed the DLL by ensuring the character declarations were unsigned.  Also, I had been working on some m file wrappers for the functions and automatic loading of the correct DLL for 32 bit or 64 bit FreeMat versions (borrowed from ideas in Philippe's threads), so those changes are included too.  All of the function calls are compatible with the prior version, but there's more error handling in the m file wrappers.  Hopefully I didn't break anything!

Jonathan

Patrick Berthoud

unread,
Apr 27, 2014, 7:04:57 AM4/27/14
to fre...@googlegroups.com, KE...@comcast.net
Dear Jonathan,

I've tested your new dll. The good points are: 
1) serialTXChar(): OK for all character codes (0 to 255, but also characters outside this range).
2) serialRXChar(): OK for all character codes. For characters within range [0;255] I got same RX characters as sent characters by TX. For characters outside this range, there are limited to 0 and 255, which is correct.
3) serialRXString(): OK. I have tested this function by sending repetitive serialTXChar() and reading all characters in the buffer by serialTXString(). In particular I send the following chars [-2] [-1] [0] [1] [2] [254] [255] [256] [257] individually by the serialTXChar() and I got the answer [0 0 0 1 2 254 255 255 255] by the serialRXString() function which is what I expected. This means that the serialRXString() function in correct also for unsigned char array.

However the bad point is:
serialTXStrin() is not correct. I tried to send the same characters as above [-2 -1 0 1 2 254 255 256 257] in a single command by serial command serialTXString(). 9 bytes are effectively sent (I checked it by the ByteAvailable() function). However the answer read by serialRXString() is not what I expected [0   0 240   4   0   0   0   0 157]. Moreover repetive tests with the same sent string does not provide repetitive answers. 

Any ideas what could be wrong in the serialTXString() function ?

For information I tried to change the port baud rate (57600 and 9600): no effect. I also tried to change some timeouts: no effect (actually I don't know exactly what they are doing).

BR

Patrick

Jonathan Weaver

unread,
May 2, 2014, 2:50:22 PM5/2/14
to fre...@googlegroups.com, KE...@comcast.net
Patrick,

I changed two lines in the serialload.m file to correct this:

    import('serial64.dll', 'sTXString', 'serialTXString_internal', 'int16', 'int32 hComm, string strToWrite, int32 noOfBytesToWrite');

was changed to

    import('serial64.dll', 'sTXString', 'serialTXString_internal', 'int16', 'int32 hComm, uint8 &strToWrite, int32 noOfBytesToWrite');

It turns out the string data type corresponds to char* and we want unsigned char*.

Thanks for testing.

-Jonathan

Patrick Berthoud

unread,
May 5, 2014, 2:33:09 AM5/5/14
to fre...@googlegroups.com, KE...@comcast.net
Dear Jonathan,

Now everything works fine me: I can sent and read unsigned array of characters with *String* functions. Thanks a lot for your work and your support. 

BR

Patrick

Jonathan Weaver

unread,
Aug 25, 2014, 10:21:02 AM8/25/14
to fre...@googlegroups.com, KE...@comcast.net
I'm changing my website.  The FreeMat serial port download is now available at http://jonw0224.weebly.com/freemat-library.html
Reply all
Reply to author
Forward
0 new messages