Products Consulting Training and Events Support Store
Support Header
 
Solution Number: 5283
Date Last Modified: 2002-05-08
Product:    MATLAB 4.2c.1 ==> 6.0
Platform:    Windows

Problem Description

Is it possible to read from or write to a serial or com port under MS-Windows?

I am trying to use FREAD and FWRITE to read from and write to a serial port; however, when I try to open a connection to the port using FOPEN, it fails. Can this be done with either MATLAB or the Data Acquisition Toolbox?

Solution:

As of MATLAB 6.0 (R12), MATLAB and the Instrument Control Toolbox have ready-made functions or utilities for interfacing data with a computer's COM-ports. Also, MATLAB 6.0 has a SERIAL function that can be used to access serial ports. For more information on this function, please see solution 27303:


Previous versions of MATLAB or the Data Acquisition Toolbox do not have any ready-made functions or utilities for interfacing data with a computer's COM-ports.

The COM-ports may be accessed in one of two ways, either by using MATLAB's FOPEN routine or via a MEX-file.

The MATLAB FOPEN command can be used to open a link to a COM port on machines running Windows 95. However, the only operation which can be performed via FOPEN, is that of reading data from the COM port. There are currently no routines in MATLAB which can control the COM port in any way. For example, the following can take place in MATLAB:

    fid=fopen('com2','r+')
    fread(fid)
    

Under Windows NT, it is not possible to use FOPEN to read, or write to the serial ports. You will receive an error such as:

    You have chosen a reserved DOS device name for your filename.
    Please choose another valid filename.
    

In order to gain more control over the COM port, a MEX-file must be written.

MEX-files are dynamically linked subroutines that the MATLAB interpreter can automatically load and execute. One of the applications for using MEX-files is that A/D cards, D/A cards, and other PC hardware can be accessed directly for data acquisition and control applications. For further information on using MEX-Files, please refer to the Application Program Interface Guide

Additionally:

1) DSP Technology has made a file available at the following URL:


2) The CPORT minitoolbox supplies basic functions to open, configure, read from and write to serial and parallel ports under Microsoft Windows 95/98 and (maybe) NT. The toolbox is implemented using a series of MEX files and M files. It requires Windows95/98/NT and MATLAB 5.x (it was tested on Win95 and MATLAB 5.2).

    Included functions 
    ------------------- 
    cportopen     - opens a serial or parallel port for use. 
    cportconfig   - returns/modifies the configuration of a port. 
    cportwrite    - writes characters to a port. 
    cportgetchar  - reads characters from a port. 
    cportgetline  - reads an entire character line from a port. 
    cportreset    - resets a port, retrieves error codes and strings and 
                   flushes its buffers. 
    cportclose    - closes an open port.
    

It can be found at


or as a Zip file...


PLEASE NOTE: These files are user contributed and therefore are not officially supported by The MathWorks. Any questions/concerns should be directed to the author(s) of these files.

 
  Did this information help? Yes  No  Didn't Apply 
  Is the level of technical detail appropriate? Yes  Too Much  Not Enough 
 
  What did you expect to find on this page, that you want us to consider adding?
  Additional Comments:
 
 

  Related Topics Related Topics
  Related Topics   Demos I  Search I  Contact Support I  Consulting I  News & Notes I  Usability
   
 
The MathWorks, Inc.    Trademarks   Privacy Policy