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, modem, printer, or 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.
If you are having trouble connecting to or communicating with your serial port device, follow these troubleshooting steps.
The serial port interface is supported on these platforms:
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 System Requirements for MATLAB.
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.
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.
Make sure that you have the correct instrument driver installed for your device. Refer to your device documentation and the vendor website.
Make sure that your device is supported in Instrument Control Toolbox™. See Is My Hardware Supported?.
Make sure that Instrument Control Toolbox recognizes your serial ports, by using the
serialportlist function. For example, if your computer has more
than one serial port, your output would look like this:
ans = 1×3 string array "COM1" "COM3" "COM4"
You can also use Windows device manager to see a list of available serial ports.
Make sure you can create your serial port object. You must provide two arguments to create the object. For example, create a serial object called
s using port
COM1 and baud rate
s = serialport("COM1",9600);
If you do not get an error, the object was created successfully.
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
Terminator. Make sure you configure these communication parameters to match those of the connected device.
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
Terminator. Make sure you configure these communication parameters to
match those of the connected device.
VISA. For serial communication, you can also use VISA with a VISA resource name, as defined in a VISA vendor utility, such as Keysight 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
write, make sure the correct data type – for example
double – is
being used with
write. You should use the
same data type as the instrument uses.
If reading and writing data types other than
int8, make sure the
ByteOrder is correct.