Main Content

Use Serial Port Control Pins

Control Pins

As described in Serial Port Signals and Pin Assignments, nine-pin serial ports include six control pins. The functions and properties associated with the serial port control pins are as follows.

FunctionPurpose
getpinstatus

Get serial pin status.

setRTS

Specify the state of the RTS pin.

setDTR

Specify the state of the DTR pin.

FlowControl

Specify the data flow control method to use.

Signaling the Presence of Connected Devices

DTEs and DCEs often use the CD, DSR, RI, and DTR pins to indicate whether a connection is established between serial port devices. Once the connection is established, you can begin to write or read data.

You can monitor the state of the CD, DSR, and RI pins with the getpinstatus function. You can specify the state of the DTR pin with the setDTR function.

The following example illustrates how these pins are used when two modems are connected to each other.

Connect Two Modems

This example (shown on a Windows® machine) connects two modems to each other through the same computer, and illustrates how you can monitor the communication status for the computer-modem connections, and for the modem-modem connection. The first modem is connected to COM1, while the second modem is connected to COM2.

  1. Connect to the instruments — After the modems are powered on, the serial port object s1 is created for the first modem, and the serial port object s2 is created for the second modem. both modems are configured for a baud rate of 9600 bits per second.

    s1 = serialport("COM1",9600);
    s2 = serialport("COM2",9600);

    You can verify that the modems (data sets) are ready to communicate with the computer by examining the value of the Data Set Ready pin using the getpinstatus function.

    getpinstatus(s)
    ans = 
    
      struct with fields:
    
          ClearToSend: 1
         DataSetReady: 1
        CarrierDetect: 0
        RingIndicator: 0

    The value of the DataSetReady field is 1, or true, because both modems were powered on before they were connected to the objects.

  2. Configure properties — Both modems are configured for a carriage return (CR) terminator using the configureTerminator function.

    configureTerminator(s1,"CR")
    configureTerminator(s2,"CR")
  3. Write and read data — Write the atd command to the first modem using the writeline function. This command puts the modem “off the hook,” and is equivalent to manually lifting a phone receiver.

    writeline(s1,'atd')

    Write the ata command to the second modem using the writeline function. This command puts the modem in “answer mode,” which forces it to connect to the first modem.

    writeline(s2,'ata')

    After the two modems negotiate their connection, you can verify the connection status by examining the value of the Carrier Detect pin using the getpinstatus function.

    getpinstatus(s)
    ans = 
    
      struct with fields:
    
          ClearToSend: 1
         DataSetReady: 1
        CarrierDetect: 1
        RingIndicator: 0

    You can also verify the modem-modem connection by reading the descriptive message returned by the second modem.

    s2.NumBytesAvailable
    
    ans =
    
         25
    out = read(s2,25,"uint32")
    out =
    ata
    CONNECT 2400/NONE

    Now break the connection between the two modems by using the setDTR function. You can verify that the modems are disconnected by examining the Carrier Detect pin value using the getpinstatus function.

    setDTR(s1,false)
    getpinstatus(s1)
    
    ans = 
    
      struct with fields:
    
          ClearToSend: 1
         DataSetReady: 1
        CarrierDetect: 0
        RingIndicator: 0
  4. Disconnect and clean up — Clear the objects from the MATLAB® workspace when you are done.

    clear s1 s2

Controlling the Flow of Data: Handshaking

Data flow control or handshaking is a method used for communicating between a DCE and a DTE to prevent data loss during transmission. For example, suppose your computer can receive only a limited amount of data before it must be processed. As this limit is reached, a handshaking signal is transmitted to the DCE to stop sending data. When the computer can accept more data, another handshaking signal is transmitted to the DCE to resume sending data.

If supported by your device, you can control data flow using one of these methods:

Note

Although you might be able to configure your device for both hardware handshaking and software handshaking at the same time, MATLAB does not support this behavior.

You can specify the data flow control method with the FlowControl property. If FlowControl is hardware, then hardware handshaking is used to control data flow. If FlowControl is software, then software handshaking is used to control data flow. If FlowControl is none, then no handshaking is used.

Hardware Handshaking

Hardware handshaking uses specific serial port pins to control data flow. In most cases, these are the RTS and CTS pins. Hardware handshaking using these pins is described in RTS and CTS Pins.

If FlowControl is hardware, then the RTS and CTS pins are automatically managed by the DTE and DCE. You can return the CTS pin value with the getpinstatus function. You can configure the RTS pin value with the setRTS function.

Note

Some devices also use the DTR and DSR pins for handshaking. However, these pins are typically used to indicate that the system is ready for communication, and are not used to control data transmission. In MATLAB, hardware handshaking always uses the RTS and CTS pins.

If your device does not use hardware handshaking in the standard way, then you might need to manually configure the RTS pin using the setRTS function. In this case, configure FlowControl to none. If FlowControl is hardware, then the RTS value that you specify might not be honored. Refer to the device documentation to determine its specific pin behavior.

Software Handshaking

Software handshaking uses specific ASCII characters to control data flow. The following table describes these characters, known as Xon and Xoff (or XON and XOFF).

Software Handshaking Characters

Character

Integer Value

Description

Xon

17

Resume data transmission.

Xoff

19

Pause data transmission.

When you use software handshaking, the control characters are sent over the transmission line the same way as regular data. Therefore, you need only the TD, RD, and GND pins.

The main disadvantage of software handshaking is that you cannot write the Xon or Xoff characters while numerical data is being written to the instrument. This is because numerical data might contain a 17 or 19, which makes it impossible to distinguish between the control characters and the data. However, you can write Xon or Xoff while data is being asynchronously read from the instrument because you are using both the TD and RD pins.

Using Software Handshaking.  Suppose you want to use software flow control in conjunction with your serial port application. To do this, you must configure the instrument and the serial port object for software flow control. For a serial port object s connected to a Tektronix® TDS 210 oscilloscope, this configuration is accomplished with the following commands.

writeline(s,"RS232:SOFTF ON")
s.FlowControl = "software";

To pause data transfer, you write the numerical value 19 (Xoff) to the instrument.

write(s,19,"uint32");

To resume data transfer, you write the numerical value 17 (Xon) to the instrument.

write(s,17,"uint32");