This is post is about to Develop understanding with serial port as well as how to access the serial port in MATLAB.
The contents of this article are taken from MATLAB Site(Maths.com) and some other sites.
Introduction
For many serial port applications, you can communicate with your device without detailed knowledge of how the serial port works. If your application is straightforward, or if you are already familiar with the topics mentioned above, you might want to begin with The Serial Port Session to see how to use your serial port device with MATLAB.
What Is Serial Communication?
Serial communication is the most common low-level protocol for communicating between two or more devices. Normally, one device is a computer, while the other device can be a modem, a printer, another computer, or a scientific instrument such as an oscilloscope or a function generator.
As the name suggests, the serial port sends and receives bytes of information in a serial fashion — one bit at a time. These bytes are transmitted using either a binary (numerical) format or a text format.
The Serial Port Interface Standard
The serial port interface for connecting two devices is specified by the TIA/EIA-232C standard published by the Telecommunications Industry Association.
The original serial port interface standard was given by RS-232, which stands for Recommended Standard number 232. The term RS-232 is still in popular use, and is used in this guide when referring to a serial communication port that follows the TIA/EIA-232 standard. RS-232 defines these serial port characteristics:
· The maximum bit transfer rate and cable length
· The names, electrical characteristics, and functions of signals
· The mechanical connections and pin assignments
Primary communication is accomplished using three pins: the Transmit Data pin, the Receive Data pin, and the Ground pin. Other pins are available for data flow control, but are not required.
Other standards such as RS-485 define additional functionality such as higher bit transfer rates, longer cable lengths, and connections to as many as 256 devices.
Connecting Two Devices with a Serial Cable
The RS-232 standard defines the two devices connected with a serial cable as the Data Terminal Equipment (DTE) and Data Circuit-Terminating Equipment (DCE). This terminology reflects the RS-232 origin as a standard for communication between a computer terminal and a modem.
Throughout this guide, your computer is considered a DTE, while peripheral devices such as modems and printers are considered DCE's. Many scientific instruments function as DTEs.
Because RS-232 mainly involves connecting a DTE to a DCE, the pin assignments are defined such that straight-through cabling is used, where pin 1 is connected to pin 1, pin 2 is connected to pin 2, and so on. A DTE to DCE serial connection using the transmit data (TD) pin and the receive data (RD) pin is shown below.
Refer to Serial Port Signals and Pin Assignments for more information about serial port pins.
If you connect two DTEs or two DCEs using a straight serial cable, the TD pins on each device are connected to each other, and the RD pins on each device are connected to each other. Therefore, to connect two like devices, you must use a null modem cable. As shown below, null modem cables cross the transmit and receive lines in the cable.
Serial Port Signals and Pin Assignments
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.
The pin assignment scheme for a 9-pin male connector on a DTE is shown below.
The pins and signals associated with the 9-pin connector are described below. Refer to the RS-232 standard for a description of the signals and pin assignments used for a 25-pin connector.
Pin | Label | Signal Name | Signal Type |
1 | CD | Carrier Detect | Control |
2 | RD | Received Data | Data |
3 | TD | Transmitted Data | Data |
4 | DTR | Data Terminal Ready | Control |
5 | GND | Signal Ground | Ground |
6 | DSR | Data Set Ready | Control |
7 | RTS | Request to Send | Control |
8 | CTS | Clear to Send | Control |
9 | RI | Ring Indicator | Control |
The term data set is synonymous with modem or device, while the term data terminal is synonymous with computer.
Signal States
Signals can be in either an active state or an inactive state. An active state corresponds to the binary value 1, while an inactive state corresponds to the binary value 0. An active signal state is often described as logic 1, on, true, or a mark. An inactive signal state is often described as logic 0, off, false, or a space.
For data signals, the on state occurs when the received signal voltage is more negative than -3 volts, while the off state occurs for voltages more positive than 3 volts. For control signals, the on state occurs when the received signal voltage is more positive than 3 volts, while the off state occurs for voltages more negative than -3 volts. The voltage between -3 volts and +3 volts is considered a transition region, and the signal state is undefined.
To bring the signal to the on state, the controlling device unasserts (or lowers) the value for data pins and asserts (or raises) the value for control pins. Conversely, to bring the signal to the off state, the controlling device asserts the value for data pins and unasserts the value for control pins.
The on and off states for a data signal and for a control signal are shown below.
The Data Pins
Most serial port devices support full-duplex communication meaning that they can send and receive data at the same time. Therefore, separate pins are used for transmitting and receiving data. For these devices, the TD, RD, and GND pins are used. However, some types of serial port devices support only one-way or half-duplex communications. For these devices, only the TD and GND pins are used. This guide assumes that a full-duplex serial port is connected to your device.
The TD pin carries data transmitted by a DTE to a DCE. The RD pin carries data that is received by a DTE from a DCE.
The Control Pins
The control pins of a 9-pin serial port are used to determine the presence of connected devices and control the flow of data. The control pins include
· The RTS and CTS Pins
· The DTR and DSR Pins
· The CD and RI Pins
The RTS and CTS Pins. The RTS and CTS pins are used to signal whether the devices are ready to send or receive data. This type of data flow control—called hardware handshaking—is used to prevent data loss during transmission. When enabled for both the DTE and DCE, hardware handshaking using RTS and CTS follows these steps:
2. The DCE asserts the CTS pin indicating that it is clear to send data over the TD pin. If data can no longer be sent, the CTS pin is unasserted.
3. The data is transmitted to the DTE over the TD pin. If data can no longer be accepted, the RTS pin is unasserted by the DTE and the data transmission is stopped.
To enable hardware handshaking in MATLAB, refer to Controlling the Flow of Data: Handshaking.
The DTR and DSR Pins. Many devices use the DSR and DTR pins to signal if they are connected and powered. Signaling the presence of connected devices using DTR and DSR follows these steps:
The DTR and DSR pins were originally designed to provide an alternative method of hardware handshaking. However, the RTS and CTS pins are usually used in this way, and not the DSR and DTR pins. Refer to your device documentation to determine its specific pin behavior.
The CD and RI Pins. The CD and RI pins are typically used to indicate the presence of certain signals during modem-modem connections.
CD is used by a modem to signal that it has made a connection with another modem, or has detected a carrier tone. CD is asserted when the DCE is receiving a signal of a suitable frequency. CD is unasserted if the DCE is not receiving a suitable signal.
RI is used to indicate the presence of an audible ringing signal. RI is asserted when the DCE is receiving a ringing signal. RI is unasserted when the DCE is not receiving a ringing signal (e.g., it is between rings).
Serial Data Format
The serial data format includes one start bit, between five and eight data bits, and one stop bit. A parity bit and an additional stop bit might be included in the format as well. The diagram below illustrates the serial data format.
The format for serial port data is often expressed using the following notation
number of data bits - parity type - number of stop bits
For example, 8-N-1 is interpreted as eight data bits, no parity bit, and one stop bit, while 7-E-2 is interpreted as seven data bits, even parity, and two stop bits.
The data bits are often referred to as a character because these bits usually represent an ASCII character. The remaining bits are called framing bits because they frame the data bits.
Bytes Versus Values
The collection of bits that comprise the serial data format is called a byte. At first, this term might seem inaccurate because a byte is 8 bits and the serial data format can range between 7 bits and 12 bits. However, when serial data is stored on your computer, the framing bits are stripped away, and only the data bits are retained. Moreover, eight data bits are always used regardless of the number of data bits specified for transmission, with the unused bits assigned a value of 0.
When reading or writing data, you might need to specify a value, which can consist of one or more bytes. For example, if you read one value from a device using the int32 format, that value consists of four bytes. For more information about reading and writing values, refer to Writing and Reading Data.
Synchronous and Asynchronous Communication
The RS-232 standard supports two types of communication protocols: synchronous and asynchronous.
Using the synchronous protocol, all transmitted bits are synchronized to a common clock signal. The two devices initially synchronize themselves to each other, and continually send characters to stay synchronized. Even when actual data is not really being sent, a constant flow of bits allows each device to know where the other is at any given time. That is, each bit that is sent is either actual data or an idle character. Synchronous communications allows faster data transfer rates than asynchronous methods, because additional bits to mark the beginning and end of each data byte are not required.
Using the asynchronous protocol, each device uses its own internal clock, resulting in bytes that are transferred at arbitrary times. So, instead of using time as a way to synchronize the bits, the data format is used.
In particular, the data transmission is synchronized using the start bit of the word, while one or more stop bits indicate the end of the word. The requirement to send these additional bits causes asynchronous communications to be slightly slower than synchronous. However, it has the advantage that the processor does not have to deal with the additional idle characters. Most serial ports operate asynchronously.
How Are the Bits Transmitted?
By definition, serial data is transmitted one bit at a time. The order in which the bits are transmitted is as follows
2. The data bits are transmitted. The first data bit corresponds to the least significant bit (LSB), while the last data bit corresponds to the most significant bit (MSB).
The number of bits transferred per second is given by the baud rate. The transferred bits include the start bit, the data bits, the parity bit (if defined), and the stop bits.
Start and Stop Bits
As described in Synchronous and Asynchronous Communication, most serial ports operate asynchronously. This means that the transmitted byte must be identified by start and stop bits. The start bit indicates when the data byte is about to begin; the stop bit(s) indicate(s) when the data byte has been transferred. The process of identifying bytes with the serial data format follows these steps:
2. When data is about to be transmitted, the serial port pin switches to an off state due to the start bit.
3. The serial port pin switches back to an on state due to the stop bit(s). This indicates the end of the byte.
Data Bits
The data bits transferred through a serial port might represent device commands, sensor readings, error messages, and so on. The data can be transferred as either binary data or ASCII data.
Most serial ports use between five and eight data bits. Binary data is typically transmitted as eight bits. Text-based data is transmitted as either seven bits or eight bits. If the data is based on the ASCII character set, a minimum of seven bits is required because there are 27 or 128 distinct characters. If an eighth bit is used, it must have a value of 0. If the data is based on the extended ASCII character set, eight bits must be used because there are 28 or 256 distinct characters.
The Parity Bit
The parity bit provides simple error (parity) checking for the transmitted data. The types of parity checking are shown below.
Parity Type | Description |
Even | The data bits plus the parity bit result in an even number of 1s. |
Mark | The parity bit is always 1. |
Odd | The data bits plus the parity bit result in an odd number of 1s. |
Space | The parity bit is always 0. |
Mark and space parity checking are seldom used because they offer minimal error detection. You might choose to not use parity checking at all.
The parity checking process follows these steps:
1. The transmitting device sets the parity bit to 0 or to 1, depending on the data bit values and the type of parity-checking selected.
2. The receiving device checks if the parity bit is consistent with the transmitted data. If it is, the data bits are accepted. If it is not, an error is returned.
For example, suppose the data bits 01110001 are transmitted to your computer. If even parity is selected, the parity bit is set to 0 by the transmitting device to produce an even number of 1s. If odd parity is selected, the parity bit is set to 1 by the transmitting device to produce an odd number of 1s.
Finding Serial Port Information for Your Platform
This section describes the ways to find serial port information for Windows and UNIX platforms.
Windows Platform
You can access serial port information through the System Properties dialog. To access this in Windows XP,
2. In the System Properties dialog, click the Hardware tab.
3. Click Device Manager.
4. In the Device Manager dialog, expand the Ports node.
5. Double-click the Communications Port (COM1) node.
6. Select the Port Settings tab.
The resulting Ports dialog box is shown below.
Creating a Serial Port Object
Overview of a Serial Port Object
The serial function requires the name of the serial port connected to your device as an input argument. Additionally, you can configure property values during object creation. For example, to create a serial port object associated with the serial port COM1, enter
s = serial('COM1');
The serial port object s now exists in the MATLAB workspace. You can display the class of s with the whos command.
whos s
Name Size Bytes Class
s 1x1 512 serial object
Grand total is 11 elements using 512 bytes
Once the serial port object is created, the properties listed below are automatically assigned values. These general-purpose properties provide descriptive information about the serial port object based on the object type and the serial port.
Property Name | Description |
Name | Specify a descriptive name for the serial port object |
Port | Indicate the platform-specific serial port name |
Type | Indicate the object type |
Display the values of these properties for s with the get function.
get(s,{'Name','Port','Type'})
ans =
'Serial-COM1' 'COM1' 'serial'
Configuring Properties During Object Creation
You can configure serial port properties during object creation. serial accepts property names and property values in the same format as the set function. For example, you can specify property name/property value pairs.
s = serial('COM1','BaudRate',4800,'Parity','even');
If you specify an invalid property name, the object is not created. However, if you specify an invalid value for some properties (for example, BaudRate is set to 50), the object might be created but you are not informed of the invalid value until you connect the object to the device with the fopen function.
The Serial Port Object Display
The serial port object provides you with a convenient display that summarizes important configuration and state information. You can invoke the display summary these three ways:
· Type the serial port object variable name at the command line.
· Exclude the semicolon when creating a serial port object.
· Exclude the semicolon when configuring properties using the dot notation.
The display summary for the serial port object s is as follows:
Serial Port Object : Serial-COM1
Communication Settings
Port: COM1
BaudRate: 9600
Terminator: 'LF'
Communication State
Status: closed
RecordStatus: off
Read/Write State
TransferStatus: idle
BytesAvailable: 0
ValuesReceived: 0
ValuesSent: 0
Creating an Array of Serial Port Objects
In MATLAB, you can create an array from existing variables by concatenating those variables together. The same is true for serial port objects. For example, suppose you create the serial port objects s1 and s2.
s1 = serial ('COM1');
s2 = serial ('COM2');
You can now create a serial port object array consisting of s1 and s2 using the usual MATLAB syntax. To create the row array x, enter
x = [s1 s2]
Instrument Object Array
Index: Type: Status: Name:
1 serial closed Serial-COM1
2 serial closed Serial-COM2
To create the column array y, enter
y = [s1;s2];
Note that you cannot create a matrix of serial port objects. For example, you cannot create the matrix
z = [s1 s2; s1 s2];
??? Error using ==> serial/vertcat
Only a row or column vector of instrument objects can be created.
Depending on your application, you might want to pass an array of serial port objects to a function. For example, to configure the baud rate and parity for s1 and s2 using one call to set:
Set(x,'BaudRate', 19200,'Parity','even')
Refer to the Serial Port Devices functional reference to see which functions accept a serial port object array as an input.
Configuring Communication Settings
Before you can write or read data, both the serial port object and the instrument must have identical communication settings. Configuring serial port communications involves specifying values for properties that control the baud rate and the Serial Data Format. These properties are as follows:
Property Name | Description |
BaudRate | Specify the rate at which bits are transmitted. |
Specify the number of data bits to transmit. | |
Specify the type of parity checking. | |
Specify the number of bits used to indicate the end of a byte. | |
Specify the terminator character. |
Note If the serial port object and the instrument communication settings are not identical, you cannot successfully read or write data. |
Refer to your instrument documentation for an explanation of its supported communication settings.
You can display the communication property values for the serial port object s created in The Serial Port Object with the get function.
get(s,{'BaudRate','DataBits','Parity','StopBits','Terminator'})
ans =
[9600] [8] 'none' [1] 'LF'
Writing and Reading Data
Asynchronous Write and Read Operations
Asynchronous write and read operations do not block access to the MATLAB command line. Additionally, while an asynchronous operation is in progress you can
· Execute a read (write) operation while an asynchronous write (read) operation is in progress. This is because serial ports have separate pins for reading and writing.
· Make use of all supported callback properties. Refer to Events and Callbacks for more information about the callback properties supported by serial port objects.
The process of writing data asynchronously is given in Synchronous Versus Asynchronous Write Operations.
For a general overview about writing and reading data, as well as a list of all associated functions and properties, refer to Communicating with Your Instrument.
Asynchronous Read Operations
For serial port objects, you specify whether read operations are synchronous or asynchronous with the ReadAsyncMode property. You can configure ReadAsyncMode to continuous or manual.
If ReadAsyncMode is continuous (the default value), the serial port object continuously queries the instrument to determine if data is available to be read. If data is available, it is asynchronously stored in the input buffer. To transfer the data from the input buffer to MATLAB, you use one of the synchronous (blocking) read functions such as fgetl, fgets, fscanf, or fread. If data is available in the input buffer, these functions will return quickly.
s = serial('COM1');
fopen(s)
s.ReadAsyncMode = 'continuous';
fprintf(s,'*IDN?')
s.BytesAvailable
ans =
56
out = fscanf(s);
If ReadAsyncMode is manual, the serial port object does not continuously query the instrument to determine if data is available to be read. To read data asynchronously, you use the readasync function. You then use one of the synchronous read functions to transfer data from the input buffer to MATLAB.
s.ReadAsyncMode = 'manual';
fprintf(s,'*IDN?')
s.BytesAvailable
ans =
0
readasync(s)
s.BytesAvailable
ans =
56
out = fscanf(s);
Rules for Completing Write and Read Operations
The rules for completing synchronous and asynchronous read and write operations are described below.
Completing Write Operations
· The specified data is written.
· The time specified by the Timeout property passes.
In addition to these rules, you can stop an asynchronous write operation at any time with the stopasync function.
A text command is processed by the instrument only when it receives the required terminator. For serial port objects, each occurrence of \n in the ASCII command is replaced with the Terminator property value. Because the default format for fprintf is %s\n, all commands written to the instrument will end with the Terminator value. The default value of Terminator is the line feed character. The terminator required by your instrument will be described in its documentation.
Completing Read Operations
A read operation with fgetl, fgets, fscanf, or readasync completes when one of these conditions is satisfied:
· The terminator specified by the Terminator property is read.
· The time specified by the Timeout property passes.
· The input buffer is filled.
· The specified number of values is read (fscanf and readasync only).
A read operation with fread completes when one of these conditions is satisfied:
· The time specified by the Timeout property passes.
· The specified number of values is read.
In addition to these rules, you can stop an asynchronous read operation at any time with the stopasync function.
Example: Writing and Reading Text Data
This example illustrates how to communicate with a serial port instrument by writing and reading text data.
The instrument is a Tektronix TDS 210 two-channel oscilloscope connected to the serial port COM1. Therefore, many of the commands given below are specific to this instrument. A sine wave is input into channel 2 of the oscilloscope, and your job is to measure the peak-to-peak voltage of the input signal.
s = serial('COM1');
2. Connect to the instrument — Connect s to the oscilloscope. Because the default value for the ReadAsyncMode property is continuous, data is asynchronously returned to the input buffer as soon as it is available from the instrument.
fopen(s)
3. Write and read data — Write the *IDN? Command to the instrument using fprintf, and then read back the result of the command using fscanf.
4. fprintf(s,'*IDN?')
5. s.BytesAvailable
6. ans =
7. 56
8. idn = fscanf(s)
9. idn =
TEKTRONIX,TDS 210,0,CF:91.1CT FV:v1.16 TDS2CM:CMV:v1.04
You need to determine the measurement source. Possible measurement sources include channel 1 and channel 2 of the oscilloscope.
fprintf(s,'MEASUREMENT:IMMED:SOURCE?')
source = fscanf(s)
source =
CH1
The scope is configured to return a measurement from channel 1. Because the input signal is connected to channel 2, you must configure the instrument to return a measurement from this channel.
fprintf(s,'MEASUREMENT:IMMED:SOURCE CH2')
fprintf(s,'MEASUREMENT:IMMED:SOURCE?')
source = fscanf(s)
source =
CH2
You can now configure the scope to return the peak-to-peak voltage, and then request the value of this measurement.
fprintf(s,'MEASUREMENT:MEAS1:TYPE PK2PK')
fprintf(s,'MEASUREMENT:MEAS1:VALUE?')
Transfer data from the input buffer to MATLAB using fscanf.
ptop = fscanf(s)
ptop =
2.0199999809E0
10. Disconnect and clean up — When you no longer need s, you should disconnect it from the instrument, and remove it from memory and from the MATLAB workspace.
11. fclose(s)
12. delete(s)
clear s
Using Control Pins
Control Pins
As described in Serial Port Signals and Pin Assignments, 9-pin serial ports include six control pins. The properties associated with the serial port control pins are as follows:
Property Name | Description |
Specify the state of the DTR pin. | |
Specify the data flow control method to use. | |
Indicate the state of the CD, CTS, DSR, and RI pins. | |
Specify the state of the RTS pin. |
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 PinStatus property. You can specify or monitor the state of the DTR pin with the DataTerminalReady property.
The following example illustrates how these pins are used when two modems are connected to each other.
Example: Connecting Two Modems
This example connects two modems to each other via 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. Create the instrument objects — 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.
2. s1 = serial('COM1');
s2 = serial('COM2');
3. Connect to the instruments — s1 and s2 are connected to the modems. Because the default value for the ReadAsyncMode property is continuous, data is asynchronously returned to the input buffers as soon as it is available from the modems.
4. fopen(s1)
fopen(s2)
Because the default value of the DataTerminalReady property is on, the computer (data terminal) is now ready to exchange data with the modems. 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 thePinStatus property.
s1.Pinstatus
ans =
CarrierDetect: 'off'
ClearToSend: 'on'
DataSetReady: 'on'
RingIndicator: 'off'
The value of the DataSetReady field is on because both modems were powered on before they were connected to the objects.
5. Configure properties — Both modems are configured for a baud rate of 2400 bits per second and a carriage return (CR) terminator.
6. s1.BaudRate = 2400;
7. s1.Terminator = 'CR';
8. s2.BaudRate = 2400;
s2.Terminator = 'CR';
9. Write and read data — Write the atd command to the first modem. This command puts the modem "off the hook," which is equivalent to manually lifting a phone receiver.
fprintf(s1,'atd')
Write the ata command to the second modem. This command puts the modem in "answer mode," which forces it to connect to the first modem.
fprintf(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 PinStatus property.
s1.PinStatus
ans =
CarrierDetect: 'on'
ClearToSend: 'on'
DataSetReady: 'on'
RingIndicator: 'off'
You can also verify the modem-modem connection by reading the descriptive message returned by the second modem.
s2.BytesAvailable
ans =
25
out = fread(s2,25);
char(out)'
ans =
ata
CONNECT 2400/NONE
Now break the connection between the two modems by configuring the DataTerminalReady property to off. You can verify that the modems are disconnected by examining the Carrier Detect pin value.
s1.DataTerminalReady = 'off';
s1.PinStatus
ans =
CarrierDetect: 'off'
ClearToSend: 'on'
DataSetReady: 'on'
RingIndicator: 'off'
10. Disconnect and clean up — Disconnect the objects from the modems, and remove the objects from memory and from the MATLAB workspace.
11. fclose([s1 s2])
12. delete([s1 s2])
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:
· Hardware Handshaking
· Software Handshaking
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 The 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 PinStatus property. You can configure or return the RTS pin value with the RequestToSend property.
If your device does not use hardware handshaking in the standard way, then you might need to manually configure the RequestToSend property. In this case, you should configure FlowControl to none. If FlowControl is hardware, then the RequestToSend 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. These characters, known as Xon and Xoff (or XON and XOFF), are described below.
Character | Integer Value | Description |
Xon | 17 | Resume data transmission. |
Xoff | 19 | Pause data transmission. |
When using 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.
Example: 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.
fprintf(s,'RS232:SOFTF ON')
s.FlowControl = 'software';
To pause data transfer, you write the numerical value 19 (Xoff) to the instrument.
fwrite(s,19)
To resume data transfer, you write the numerical value 17 (Xon) to the instrument.
fwrite(s,17)
No comments:
Post a Comment