Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Troubleshooting Serial Port Interface

Serial communication is a low-level protocol for communicating between two or more devices. Normally, one device is a computer, and the other device can be another computer or a:

  • modem

  • printer

  • scientific instrument such as an oscilloscope or a function generator

The serial port sends and receives bytes of information in a serial fashion — 1 bit at a time. These bytes are transmitted using either a binary format or a text (ASCII) format.

For many serial port applications, you can communicate with your instrument without detailed knowledge of how the serial port works. Communication is established through a serial port object, which you create in the MATLAB® workspace.

Supported Platforms

The serial port interface is supported on these platforms:

  • Linux® 64-bit

  • macOS 64-bit

  • Microsoft® Windows® 64-bit

The serial port interface is supported on the same platforms as MATLAB. For updates to the list of currently supported platforms, see http://www.mathworks.com/support/sysreq/current_release/.

Adaptor Requirements

Use RS-232 interface standard with the serial port communication. Over the years, several serial port interface standards for connecting computers to peripheral devices have been developed. These standards include RS-232, RS-422, and RS-485 — all of which are supported by the serial port object. Of these, the most widely used standard is RS-232, which stands for Recommended Standard number 232.

In this guide, it is assumed you are using the RS-232 standard.

You need to connect the two devices with a serial cable. For more information, see Connecting Two Devices with a Serial Cable.

Serial ports consist of two signal types: data signals and control signals. To support these signal types, as well as the signal ground, the RS-232 standard defines a 25-pin connection. However, most PCs and UNIX® platforms use a 9-pin connection. In fact, only three pins are required for serial port communications: one for receiving data, one for transmitting data, and one for the signal ground. For more information, see Serial Port Signals and Pin Assignments.

Configuration and Connection

  1. Make sure that you have the correct instrument driver installed for your device. Refer to your device documentation and the vendor website.

  2. Make sure that your device is supported in Instrument Control Toolbox™. See Is My Hardware Supported?.

  3. Make sure that Instrument Control Toolbox recognizes your serial ports, by using the instrhwinfo function with the serial interface name. For example:

    instrhwinfo('serial')
    ans = 
    
     HardwareInfo with properties:
    
         AvailableSerialPorts: {'COM1'}
               JarFileVersion: 'Version 3.7'
        ObjectConstructorName: {'serial('COM1');'}
                  SerialPorts: {'COM1'}
    

    If your computer has more than one serial port, your output would look like this:

    info = instrhwinfo('serial')
    info = 
    
     HardwareInfo with properties:
    
         AvailableSerialPorts: {2x1 cell}
               JarFileVersion: 'Version 3.7'
        ObjectConstructorName: {2x1 cell}
                  SerialPorts: {2x1 cell}
    
  4. In this example the output listed two ports. List the available serial ports:

    info.AvailableSerialPorts
    ans = 
    
        'COM1'
        'COM2'

    Tip

    You can also use Windows device manager to see a list of available serial ports.

  5. Make sure you can create your serial port object. You must provide one argument to create the object, the name of an available port. For example, create a serial object called s using port COM1.

    s = serial('COM1');

    If you do not get an error, the object was created successfully.

  6. Make sure you can connect to the device, using the fopen function with the object name.

    fopen(s);

    If you do not get an error, the connection was made successfully. If you do get an error, follow the steps in the error message and/or check the previous steps listed here.

  7. When you have connected, you can communicate with your device. If you have problems sending or receiving, you may need to configure communication settings such as BaudRate, DataBits, Parity, StopBits, or Terminator. Make sure you configure these communication parameters to match those of the connected device.

    See Writing and Reading Text Data and Writing and Reading Binary Data for communication examples.

Other Troubleshooting Tips for Serial Port

Verify Port

Verify that the serial (COM) port is listed in Windows Control Panel > Device Manager > Ports.

Sending and Receiving

If you have problems sending or receiving, you may need to configure communication settings such as BaudRate, DataBits, Parity, StopBits, or Terminator. Make sure you configure these communication parameters to match those of the connected device.

Buffer Settings

If you have problems sending or receiving, you may need to configure buffer settings.

The ReadAsyncMode property should always be set to continuous. Using manual usually results in data loss.

InputBufferSize needs to be set to larger than the largest chunk of data the toolbox will receive. If it is too small, data will be dropped. For continuous streams, try using a bigger buffer size. The buffer should be large enough to hold at least one second of data. The buffer should also be at least twice the read size (the number of bytes read by fread/fscanf).

VISA

For serial communication, you can also use VISA with a VISA resource name, as defined in a VISA vendor utility, such as Agilent Connection Expert.

Third-party Software

For troubleshooting serial port communication, you can also use a third-party serial communication software, such as PuTTY or Tera Term, to isolate the issue.

Incorrect Data

When doing binary data communication with fread and fwrite, make sure the correct data type – for example int16, uint16, double – is being used with fread and fwrite. You should use the same data type as the instrument uses.

If reading and writing data types other than uint8 or int8, make sure the ByteOrder is correct.

Was this topic helpful?