Class

# SerialConnection

<div class="rst-class">

forsearch

</div>

Serial

<div class="rst-class">

forsearch

</div>

Hardware

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

## Description

<span class="title-ref">SerialConnection</span> controls are used to perform serial communications with serial devices.

## Properties

<div class="rst-class">

table-centered_columns_3_and_4

</div>

| Name                                                    | Type                                       | Read-Only | Shared |
|---------------------------------------------------------|--------------------------------------------|-----------|--------|
| `Baud<serialconnection.baud>`                           | `Integer</api/data_types/integer>`         |           |        |
| `Bits<serialconnection.bits>`                           | `Integer</api/data_types/integer>`         |           |        |
| `BytesAvailable<serialconnection.bytesavailable>`       | `Integer</api/data_types/integer>`         | ✓         |        |
| `BytesLeftToSend<serialconnection.byteslefttosend>`     | `Integer</api/data_types/integer>`         | ✓         |        |
| `ClearToSend<serialconnection.cleartosend>`             | `Boolean</api/data_types/boolean>`         | ✓         |        |
| `CTS<serialconnection.cts>`                             | `Boolean</api/data_types/boolean>`         |           |        |
| `DataCarrierDetect<serialconnection.datacarrierdetect>` | `Boolean</api/data_types/boolean>`         | ✓         |        |
| `DataSetReady<serialconnection.datasetready>`           | `Boolean</api/data_types/boolean>`         | ✓         |        |
| `DataTerminalReady<serialconnection.dataterminalready>` | `Boolean</api/data_types/boolean>`         |           |        |
| `Device<serialconnection.device>`                       | `SerialDevice</api/hardware/serialdevice>` |           |        |
| `DTR<serialconnection.dtr>`                             | `Boolean</api/data_types/boolean>`         |           |        |
| `Handle<serialconnection.handle>`                       | `Integer</api/data_types/integer>`         | ✓         |        |
| `Parity<serialconnection.parity>`                       | `Parities<serialconnection.parities>`      |           |        |
| `RequestToSend<serialconnection.requesttosend>`         | `Boolean</api/data_types/boolean>`         |           |        |
| `RingIndicator<serialconnection.ringindicator>`         | `Boolean</api/data_types/boolean>`         | ✓         |        |
| `StopBit<serialconnection.stopbit>`                     | `StopBits<serialconnection.stopbits>`      |           |        |
| `XON<serialconnection.xon>`                             | `Boolean</api/data_types/boolean>`         |           |        |

## Methods

<div class="rst-class">

table-centered_column_4

</div>

| Name                                                                            | Parameters                                                                                                                                   | Returns                            | Shared |
|---------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------|--------|
| `ClearBreak<serialconnection.clearbreak>`                                       |                                                                                                                                              |                                    |        |
| `Close<serialconnection.close>`                                                 |                                                                                                                                              |                                    |        |
| `Connect<serialconnection.connect>`                                             |                                                                                                                                              |                                    |        |
| `EndOfFile<serialconnection.endoffile>`                                         |                                                                                                                                              | `Boolean</api/data_types/boolean>` |        |
| `Flush<serialconnection.flush>`                                                 |                                                                                                                                              |                                    |        |
| `LeaveDTROnClose<serialconnection.leavedtronclose>`                             |                                                                                                                                              |                                    |        |
| `LookAhead<serialconnection.lookahead>`                                         | encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`                                                  | `String</api/data_types/string>`   |        |
| `Poll<serialconnection.poll>`                                                   |                                                                                                                                              |                                    |        |
| `Read<serialconnection.read>`                                                   | byteCount As `Integer</api/data_types/integer>`, encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>` | `String</api/data_types/string>`   |        |
| `ReadAll<serialconnection.readall>`                                             | encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`                                                  | `String</api/data_types/string>`   |        |
| `ReadError<serialconnection.readerror>`                                         |                                                                                                                                              | `Boolean</api/data_types/boolean>` |        |
| `RequestLineChangeNotification<serialconnection.requestlinechangenotification>` | linesToWatch() As `SerialConnection.LineStates<serialconnection.linestates>`                                                                 |                                    |        |
|                                                                                 | `ParamArray</api/language/paramarray>` linesToWatch As `SerialConnection.LineStates<serialconnection.linestates>`                            |                                    |        |
| `Reset<serialconnection.reset>`                                                 |                                                                                                                                              |                                    |        |
| `SetBreak<serialconnection.setbreak>`                                           |                                                                                                                                              |                                    |        |
| `Write<serialconnection.write>`                                                 | Data As `String</api/data_types/string>`                                                                                                     |                                    |        |
| `WriteError<serialconnection.writeerror>`                                       |                                                                                                                                              | `Boolean</api/data_types/boolean>` |        |
| `XmitWait<serialconnection.xmitwait>`                                           |                                                                                                                                              |                                    |        |

## Events

<div class="rst-class">

table-centered_column_4

</div>

| Name                                                  | Parameters                                                                   | Returns |
|-------------------------------------------------------|------------------------------------------------------------------------------|---------|
| `DataReceived<serialconnection.datareceived>`         |                                                                              |         |
| `Error<serialconnection.error>`                       | e As `RuntimeException</api/exceptions/runtimeexception>`                    |         |
| `LineStateChanged<serialconnection.linestatechanged>` | changedLines() As `SerialConnection.LineStates<serialconnection.linestates>` |         |

## Enumerations

<div id="serialconnection.linestates">

<div class="rst-class">

forsearch

</div>

</div>

SerialConnection.LineStates

### LineStates

> Specifies the available line states that can be watched for with the `RequestLineChangeNotification<serialconnection.requestlinechangenotification>` method.
>
> | LineStates        |
> |-------------------|
> | ClearToSend       |
> | DataCarrierDetect |
> | DataSetReady      |
> | DataTerminalReady |
> | RequestToSend     |
> | RingIndicator     |

<div id="serialconnection.parities">

<div class="rst-class">

forsearch

</div>

</div>

SerialConnection.Parities

### Parities

> Specifies the type of parity to be used.
>
> | Parities |
> |----------|
> | Even     |
> | Mark     |
> | None     |
> | Odd      |
> | Space    |

<div id="serialconnection.stopbits">

<div class="rst-class">

forsearch

</div>

</div>

SerialConnection.StopBits

### StopBits

> Specifies the available stop bit values.
>
> | Stop Bits    |
> |--------------|
> | None         |
> | One          |
> | OnePointFive |
> | Two          |

## Property descriptions

<div id="serialconnection.baud">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Baud

**Baud** As `Integer</api/data_types/integer>`

> The rate at which data will be sent and received (see the Baud Rate chart in the Notes section, below).
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.
>
> </div>
>
> The “Baud” class constants can be used to get or set the Baud rate. To do this, check the value of Baud against one or more class constants. See the table in the section "Baud Rates", below.
>
> To set the Baud rate, assign the desired class constant to the Baud property. To get the baud rate, compare the value of the Baud property to the constants in this table.
>
> | Baud Rate | Value | Class Constant |
> |-----------|-------|----------------|
> | 300       | 0     | Baud300        |
> | 600       | 1     | Baud600        |
> | 1200      | 2     | Baud1200       |
> | 1800      | 3     | Baud1800       |
> | 2400      | 4     | Baud2400       |
> | 3600      | 5     | Baud3600       |
> | 4800      | 6     | Baud4800       |
> | 7200      | 7     | Baud7200       |
> | 9600      | 8     | Baud9600       |
> | 14400     | 9     | Baud14400      |
> | 19200     | 10    | Baud19200      |
> | 28800     | 11    | Baud28800      |
> | 38400     | 12    | Baud38400      |
> | 57600     | 13    | Baud57600      |
> | 115200    | 14    | Baud115200     |
> | 230400    | 15    | Baud230400     |
>
> Setting nonstandard baud rates is supported only on Windows and macOS. On macOS, the system supports arbitrary baud rates by passing the request along to the driver. If the driver supports the passed baud rate, then it is set (or approximated).
>
> On Linux, some non-standard baud rates are possible to achieve by using the Setserial system call and setting your baud rate to a special value.
>
> The following example sets the Baud rate in the Opening event of the window. The Baud rate can also be set in the IDE.
>
> ``` xojo
> SerialConnection1.Baud = SerialConnection.Baud9600
> ```

<div id="serialconnection.bits">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Bits

**Bits** As `Integer</api/data_types/integer>`

> The number of bits.
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.
>
> </div>
>
> The following class constants are available:
>
> | Value | Class Constant |
> |-------|----------------|
> | 0     | Bits5          |
> | 1     | Bits6          |
> | 2     | Bits7          |
> | 3     | Bits8          |
>
> The number of bits can be set in the IDE or by code, for example, in the Opening event of the window. For example:
>
> ``` xojo
> SerialConnection1.Bits = SerialConnection.Bits7
> ```

<div id="serialconnection.bytesavailable">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.BytesAvailable

**BytesAvailable** As `Integer</api/data_types/integer>`

> The number of bytes of data are available in the internal receive buffer.
>
> This property is read-only.
>
> ``` xojo
> TextField1.Text = Me.BytesAvailable.ToString
> ```

<div id="serialconnection.byteslefttosend">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.BytesLeftToSend

**BytesLeftToSend** As `Integer</api/data_types/integer>`

> The number of bytes left in the queue remaining to be sent.
>
> This property is read-only.
>
> ``` xojo
> TextField1.Text = Me.BytesLeftToSend.ToString
> ```

<div id="serialconnection.cleartosend">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.ClearToSend

**ClearToSend** As `Boolean</api/data_types/boolean>`

> Use to read the state of the ClearToSend line.
>
> This property is read-only.
>
> The code below is from Examples/Communication/Hardware/Serial/Line State Change Tester:
>
> ``` xojo
> ' Loop over each line that has changed and print
> ' out the new state of the line.
> Var i As Integer
>
> For Each i In changedLines
>   Select Case i
>   Case SerialConnection.LineCTS
>     MessageBox("CTS is now " + HighLow(Me.ClearToSend))
>   Case SerialConnection.LineRTS
>     MessageBox("RTS is now " + HighLow(Me.RequestToSend))
>   Case SerialConnection.LineDCD
>     MessageBox("DCD is now " + HighLow(Me.DataCarrierDetect))
>   Case SerialConnection.LineDSR
>     MessageBox("DSR is now " + HighLow(Me.DataSetReady))
>   Case SerialConnection.LineDTR
>     MessageBox("DTR is now " + HighLow(Me.DataTerminalReady))
>   Case SerialConnection.LineRI
>     MessageBox("RI is now " + HighLow(Me.RingIndicator))
>   End Select
> Next
> ```
>
> The HighLow function returns the printable message. It is:
>
> ``` xojo
> If b Then
>   Return "asserted"
> Else
>   Return "negated"
> End If
> ```

<div id="serialconnection.cts">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.CTS

**CTS** As `Boolean</api/data_types/boolean>`

> Enables CTS (Clear to Send) flow control. Signal to the far end that there is space in the receive buffer. Allows DTS to transmit.
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.
>
> </div>

<div id="serialconnection.datacarrierdetect">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.DataCarrierDetect

**DataCarrierDetect** As `Boolean</api/data_types/boolean>`

> Enables you to read the state of the DataCarrierDetect line.
>
> This property is read-only.

<div id="serialconnection.datasetready">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.DataSetReady

**DataSetReady** As `Boolean</api/data_types/boolean>`

> Enables you to read the state of the DataSetReady line.
>
> This property is read-only.

<div id="serialconnection.dataterminalready">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.DataTerminalReady

**DataTerminalReady** As `Boolean</api/data_types/boolean>`

> Sets the state of the data terminal line.

<div class="note">

<div class="title">

Note

</div>

Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.

</div>

<div id="serialconnection.device">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Device

**Device** As `SerialDevice</api/hardware/serialdevice>`

> Used to identify the serial device to which the <span class="title-ref">SerialConnection</span> control will communicate.
>
> The code below is from Examples/Communication/Hardware/Serial/Line State Change Tester:
>
> ``` xojo
> ' Populate the popup menu with all of the
> ' serial devices the system has installed.
>
> For i As Integer = 0 To SerialDevice.LastIndex
>   PopupMenu1.AddRow(SerialDevice.At(i).Name)
> Next
> ```

<div id="serialconnection.dtr">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.DTR

**DTR** As `Boolean</api/data_types/boolean>`

> Enables DTR (Data Terminal Ready) flow control.
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.
>
> </div>

<div id="serialconnection.handle">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Handle

**Handle** As `Integer</api/data_types/integer>`

> This is the control's internal descriptor and it can be used with `Declare</api/language/declare>` statements. This replaces Win32DriverHandler, MacInDriverRefNum, and MacOutDriverRefNum.
>
> This property is read-only.

<div id="serialconnection.parity">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Parity

**Parity** As `Parities<serialconnection.parities>`

> The parity that is being used.
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.
>
> </div>
>
> This code sets the parity in the `Opening<desktopwindow.opening>` event of the window:
>
> ``` xojo
> SerialConnection1.Parity = SerialConnection.Parities.Odd
> ```

<div id="serialconnection.requesttosend">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.RequestToSend

**RequestToSend** As `Boolean</api/data_types/boolean>`

> Sets the state of the RequestToSend line.
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.
>
> </div>

<div id="serialconnection.ringindicator">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.RingIndicator

**RingIndicator** As `Boolean</api/data_types/boolean>`

> Enables you to read the state of the RingIndicator line.
>
> This property is read-only.

<div id="serialconnection.stopbit">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.StopBit

**StopBit** As `StopBits<serialconnection.stopbits>`

> The number of stop bits being used.
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.
>
> </div>
>
> Set this property using the `StopBits<serialconnection.stopbits>` enumeration.
>
> This code sets the number of Stop bits in the `Opening<desktopwindow.opening>` event of the window.
>
> ``` xojo
> SerialConnection1.StopBit = SerialConnection.StopBits.One
> ```

<div id="serialconnection.xon">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.XON

**XON** As `Boolean</api/data_types/boolean>`

> Enables XON flow control.
>
> <div class="note">
>
> <div class="title">
>
> Note
>
> </div>
>
> Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.
>
> </div>
>
> This example enables XON flow control.
>
> ``` xojo
> SerialConnection1.XON = True
> ```

## Method descriptions

<div id="serialconnection.clearbreak">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.ClearBreak

**ClearBreak**

> Clears the break signal on the control immediately, without the need to call the Reset method. Available on Windows and macOS.
>
> ``` xojo
> SerialConnection1.ClearBreak
> ```

<div id="serialconnection.close">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Close

**Close**

> Closes the connection to the serial device.

<div id="serialconnection.connect">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Connect

**Connect**

> Attempts to open a connection to the serial device.
>
> If the connection could not be made, an `IOException</api/exceptions/ioexception>` is raised. If not device was assigned when the method was called, the error code returned is SerialDeviceNotAssigned.
>
> ``` xojo
> Try
>   SerialConnection1.Connect
>   MessageBox("The serial connection is open.")
> Catch error As IOException
>   MessageBox("The serial connection could not be opened.")
> End Try
> ```

<div id="serialconnection.endoffile">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.EndOfFile

**EndOfFile** As `Boolean</api/data_types/boolean>`

> Returns `True</api/language/true>` when there's no more data left to read.
>
> This code reads the rows and columns of data from a tab-delimited text file into a `ListBox</api/user_interface/desktop/desktoplistbox>`:
>
> ``` xojo
> Var f As FolderItem
> Var textInput As TextInputStream
> Var rowFromFile, oneCell As String
>
> f = FolderItem.ShowOpenFileDialog("text/plain") ' defined as a FileType
> If f <> Nil Then
>   textInput = TextInputStream.Open(f)
>   textInput.Encoding = Encodings.UTF8
>
>   Do
>     rowFromFile = textInput.ReadLine
>     Var values() As String = rowFromFile.ToArray(String.Chr(9))
>     ListBox1.ColumnCount = values.Count
>     ListBox1.AddRow("")
>     Var col As Integer
>     For Each value As String In values
>       ListBox1.CellTextAt(ListBox1.LastAddedRowIndex, col) = value
>       col = col + 1
>     End For
>   Loop Until textInput.EndOfFile
>
>   textInput.Close
> End If
> ```
>
> This example reads each pair of bytes from a file and writes them in reverse order to a new file. The user chooses the source file using the Open-file dialog box and saves the new file using the Save as dialog box. The EOF property is used to terminate the `Do...Loop</api/language/loops/do...loop>`.
>
> ``` xojo
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text")
> If readFile <> Nil Then
>   Var ReadStream As BinaryStream = BinaryStream.Open(readFile, False)
>   ReadStream.LittleEndian = True
>   Var writeFile As FolderItem = FolderItem.ShowSaveFileDialog("", "")
>   If writeFile <> Nil Then
>     Var writeStream As BinaryStream = BinaryStream.Create(writeFile, True)
>     writeStream.LittleEndian = True
>     Do Until ReadStream.EndOfFile
>       writeStream.WriteInt8(ReadStream.ReadInt8)
>     Loop
>     writeStream = Nil
>   End If
>   readStream = Nil
> End If
> ```

<div id="serialconnection.flush">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Flush

**Flush**

> Immediately sends the contents of internal write buffers to disk or to the output stream.
>
> This function can be useful in point-to-point communication over sockets and similar connections: To optimize for transmission performance, some types of output streams try to collect small pieces of written data into one larger piece for sending instead of sending each piece out individually. By calling Flush, the data collection is stopped and the data is sent without further delay, reducing latency.
>
> When using this on a stream that ends up as a file on disk, it is useful, too: Any short parts of previously written data are written to disk right away, ensuring the data is actually on disk if the application terminates abruptly, e.g. due to a crash.
>
> Avoid calling this method too often. For example, do not call it between successive Write calls because you'll slow down performance without getting much benefit.
>
> A typical use case would look like this:
>
> ``` xojo
> mySocket.Write("you typed: ")
> mySocket.Write(key)
> mySocket.Write(".")
> mySocket.Flush
> ```

<div id="serialconnection.leavedtronclose">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.LeaveDTROnClose

**LeaveDTROnClose**

> Tells the serial connection not to negate DTR on close. The serial connection must be open for this method to function.
>
> ``` xojo
> SerialConnection1.LeaveDTROnClose
> ```

<div id="serialconnection.lookahead">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.LookAhead

**LookAhead**(encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`) As `String</api/data_types/string>`

> Returns all the unread characters in the buffer without deleting them from the buffer. The optional *encoding* parameter enables you to specify the text encoding of the data to be returned. Use the `Encodings</api/text/encoding_text/encodings>` module to specify a text encoding.
>
> This example stores the lookahead text in a `TextField</api/user_interface/desktop/desktoptextfield>`.
>
> ``` xojo
> TextField1.Text = SerialConnection1.LookAhead(Encodings.UTF8)
> ```

<div id="serialconnection.poll">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Poll

**Poll**

> Causes the control's properties to update and causes the DataAvailable event to execute if any new data is available.
>
> ``` xojo
> SerialConnection1.Poll
> ```

<div id="serialconnection.read">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Read

**Read**(byteCount As `Integer</api/data_types/integer>`, encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`) As `String</api/data_types/string>`

> Reads *byteCount* bytes from the input stream and returns a `String</api/data_types/string>`.
>
> If provided, the optional parameter *encoding* specifies the text encoding to be defined for the `String</api/data_types/string>` to be read.
>
> If *byteCount* is higher than the amount of bytes currently available in the stream, all available bytes will be returned. Therefore, make sure to always consider the case that you get less than you requested. To see if you received all requested bytes, check the returned string's `String.Bytes<string.bytes>` property (avoid using `Length<binarystream.length>` as it may give a different number if the encoding is not nil).
>
> If not enough memory is available, you get back an empty string.
>
> This example reads the first 1000 bytes from a `BinaryStream</api/files/binarystream>`.
>
> ``` xojo
> Var readFile As FolderItem = FolderItem.ShowOpenFileDialog("text/plain")
> If readFile <> Nil Then
>   Var ReadStream As BinaryStream = BinaryStream.Open(readFile, False)
>   ReadStream.LittleEndian = True
>   TextArea1.Text = ReadStream.Read(1000, Encodings.UTF8)
> End If
> ```

<div id="serialconnection.readall">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.ReadAll

**ReadAll**(encoding As `TextEncoding</api/text/encoding_text/textencoding>` = `Nil</api/language/nil>`) As `String</api/data_types/string>`

> Returns all incoming data available in the buffer as a `String</api/data_types/string>`.
>
> The optional *encoding* parameter enables you to specify the text encoding of the data to be read. Use the `Encodings</api/text/encoding_text/encodings>` module to specify a text encoding.

<div id="serialconnection.readerror">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.ReadError

**ReadError** As `Boolean</api/data_types/boolean>`

> If `True</api/language/true>` then an error occurred during reading.

<div id="serialconnection.requestlinechangenotification">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.RequestLineChangeNotification

**RequestLineChangeNotification**(linesToWatch() As `SerialConnection.LineStates<serialconnection.linestates>`)

> When a line state changes, the `LineStateChanged<serialconnection.linestatechanged>` event occurs and passes an array of lines whose state has changed.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<div class="rst-class">

forsearch

</div>

SerialConnection.RequestLineChangeNotification

**RequestLineChangeNotification**(`ParamArray</api/language/paramarray>` linesToWatch As `SerialConnection.LineStates<serialconnection.linestates>`)

> When a line state changes, the `LineStateChanged<serialconnection.linestatechanged>` event occurs and passes an array of lines whose state has changed.
>
> To select which lines to watch or see which lines have changed, use `LineStates<serialconnection.linestates>`.

<div id="serialconnection.reset">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Reset

**Reset**

> Resets the connection's baud and byte format.
>
> The serial connection must be open for this method to function.
>
> Changes to the serial device are queued so that you can modify multiple properties of it without them all taking effect immediately. For instance, you can set the baud, parity and stop bits as one operation instead of three. The same thing applies to the line state properties. Once you've made all your changes, you need to call .Reset on the serial device to reset the device to the new settings.
>
> ``` xojo
> SerialConnection1.Reset
> ```

<div id="serialconnection.setbreak">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.SetBreak

**SetBreak**

> Sets the break signal on the control immediately, without the need to call the Reset method.
>
> ``` xojo
> SerialConnection1.SetBreak
> ```

<div id="serialconnection.write">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Write

**Write**(Data As `String</api/data_types/string>`)

> Writes the passed data to the output stream.
>
> Note that in order to make sure that the data actually ends up on disk or gets sent to the socket it is connected to, the stream must either get closed or the `Flush<writeable.flush>` method be called. Otherwise, the data, if small, may end up temporarily in a write buffer before either a certain time has passed or more data is written. This buffering increases performance when writing lots of small pieces of data, but may be causing unwanted delays when another process, e.g. the other end of a socket connection, is waiting for the data. Consider calling the `Flush<writeable.flush>` method to reduce latencies that this buffering may cause in such cases.
>
> If Write fails, an `IOException</api/exceptions/ioexception>` will be raised.
>
> This example displays the Save As dialog box and writes the contents of the TextArea1 to a text file.
>
> ``` xojo
> Var f As FolderItem
> Var stream As BinaryStream
> f = FolderItem.ShowSaveFileDialog(FileTypes1.Text, "Untitled.txt")
> If f<> Nil Then
>   stream = BinaryStream.Create(f, True)
>   stream.Write(TextArea1.Text)
>   stream.Close
> End If
> ```

<div id="serialconnection.writeerror">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.WriteError

**WriteError** As `Boolean</api/data_types/boolean>`

> If `True</api/language/true>` then an error occurred during writing.

<div id="serialconnection.xmitwait">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.XmitWait

**XmitWait**

> Waits until all data sent to the serial connection with the Write method has been sent.
>
> ``` xojo
> SerialConnection1.XmitWait
> ```

## Event descriptions

<div id="serialconnection.datareceived">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.DataReceived

**DataReceived**

> Occurs when additional data has been received.
>
> It is your responsibility to read the received data using Read or ReadAll methods.
>
> <div class="warning">
>
> <div class="title">
>
> Warning
>
> </div>
>
> This event is reentrant which means you should **not** process the data you have received in this event. Instead, copy it into a property, then reset a `Timer</api/language/timer>` set to Single mode. When the incoming data has a lull, the timer will fire once and your parsing code can safely process the accumulated data. Using `Timer.CallLater<timer.calllater>` is **not** recommended here, as it creates a new timer invocation for every call to `DataReceived`, which can result in a flood of timers firing in rapid succession.
>
> </div>

<div id="serialconnection.error">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.Error

**Error**(e As `RuntimeException</api/exceptions/runtimeexception>`)

> Occurs when there is an error with the <span class="title-ref">SerialConnection</span> control.

<div id="serialconnection.linestatechanged">

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

</div>

<div class="rst-class">

forsearch

</div>

SerialConnection.LineStateChanged

**LineStateChanged**(changedLines() As `SerialConnection.LineStates<serialconnection.linestates>`)

> When a line state changes, the LineStateChanged event occurs and passes an array of lines whose state has changed.

## Interfaces

This class implements the `Readable</api/files/readable>` and `Writeable</api/files/writeable>` class interfaces.

## Notes

<div class="note">

<div class="title">

Note

</div>

Changing property values does not automatically change the state of the hardware, you must call Reset or re-Open the connection in order for the change to take effect.

</div>

The <span class="title-ref">SerialConnection</span> control can be instantiated via code since it is not a subclass of `DesktopControl</api/user_interface/desktop/desktopcontrol>`. This allows you to easily write code that does communications without adding the control to a window.

The <span class="title-ref">SerialConnection</span> control can be used to communicate via multiple serial devices at once. You can use the `SerialDevice</api/hardware/serialdevice>` class to determine the number of serial devices on the computer and access them. You should create an interface to allow the end user to choose the desired device, since serial devices are different on different machines and platforms.

When data is received by a <span class="title-ref">SerialConnection</span> control, the DataReceived event handler will automatically execute. In this event handler, you would then use the Read or ReadAll functions to access the data in the serial buffer. These functions remove the data from the serial buffer as they return the data. If you need to read the data from the serial buffer without removing it from the buffer, use the LookAhead property. This buffer will use as much memory as it needs from the memory available so there is no need for it to be resized.

Because the Write method is handled asynchronously, you may need to use the XmitWait method to force your app to wait until it has finished sending the data out the serial port.

On macOS and Linux, your app gets exclusive rights to the serial device when opening the connection. This means that another application cannot also open a serial connection to that device after your app has opened it, unless the user is running as root.

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Error codes

The following class constants can be used with the Error event's exception parameter (e.ErrorNumber).

| Class Constant | Description                                                                                          |
|----------------|------------------------------------------------------------------------------------------------------|
| NoError        | No error code.                                                                                       |
| AccessDenied   | Access denied.                                                                                       |
| PortNotFound   | Port not found.                                                                                      |
| InvalidOptions | Invalid options.                                                                                     |
| BreakCondition | Hardware detected a break condition. Usually due to a signal rate mismatch.                          |
| FramingError   | Hardware detected a framing error. Occurs when the designated "start" and "stop" bits are not valid. |

Refer to this link for additional information on each error:

<http://en.wikipedia.org/wiki/Universal_asynchronous_receiver/transmitter>

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

### Communicating with usb devices

Most USB devices have a chip in them that makes them appear as a serial device. Typically this chip is a FTDI chip. If the device has this chip, you can communicate with the device with the `SerialDevice</api/hardware/serialdevice>` class. If that does not work for you, the Monkeybread plug-in has USB support for a handful of specific types of devices.

## Sample code

The following code opens a serial connection. It assumes that a <span class="title-ref">SerialConnection</span> control (named "SerialConnection1 in this example) has been added to a window. If the connection cannot be made, an `IOException</api/exceptions/ioexception>` is raised:

``` xojo
Try
  SerialConnection1.Connect
  MessageBox("The serial connection is open.")
Catch error As IOException
  MessageBox("The serial connection could not be opened.")
End Try
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This example appends any incoming data to a `TextArea</api/user_interface/desktop/desktoptextarea>`.

``` xojo
Sub DataReceived()
  TextArea1.Text = TextArea1.Text + Me.ReadAll
End Sub
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Both the Read and ReadAll methods of the <span class="title-ref">SerialConnection</span> class take an optional parameter that enables you to specify the encoding. Use the `Encodings</api/text/encoding_text/encodings>` module to get the desired encoding and pass it as a parameter. For example, the code above has been modified to specify that the incoming text uses the ANSI encoding, a standard on Windows:

``` xojo
Sub DataReceived()
  TextArea1.Text = TextArea1.Text + Me.ReadAll(Encodings.WindowsANSI)
End Sub
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You can send data to the serial device at any time as long as you have opened a connection with the <span class="title-ref">SerialConnection</span> control's `Connect<serialconnection.connect>` method. You send data using the <span class="title-ref">SerialConnection</span> control's Write method. The data you wish to send must be a string, as the Write method accepts only a string as a parameter.

``` xojo
Try
  SerialConnection1.Connect
  SerialConnection1.Write(TextArea1.Text)
Catch error As IOException
  MessageBox("The serial connection could not be opened.")
End Try
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

The Write method is performed asynchronously. This means that the next line of code following the Write method can already be executing before all the data has actually been sent to the serial device. If you need your code to wait for all data to be sent to the serial device before continuing, call the <span class="title-ref">SerialConnection</span> control's XmitWait method immediately following a call to the Write method.

The following code directs the <span class="title-ref">SerialConnection</span> control to communicate using device zero (Modem port).

``` xojo
SerialConnection1.Device = SerialDevice.At(0)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

You detect line state changes by passing an array or a `ParamArray</api/language/paramarray>` of line states that you want to watch. In the first instance, you can do it like this. The objects cCTS, cDTD, and so forth are checkboxes that the user sets to indicate which lines to watch.

``` xojo
' Set up an array of the line states that
' you want to watch.
Var watchThese(-1) As Integer
If cCTS.Value Then  ' checkbox for CTS..
  watchThese.Add(SerialConnection.LineStates.ClearToSend)
End If
If cDCD.Value Then
  watchThese.Add(SerialConnection.LineStates.DataCarrierDetect)
End If
If cDSR.Value Then
  watchThese.Add(SerialConnection.LineStates.DataSetReady)
End If
If cDTR.Value Then
  watchThese.Add(SerialConnection.LineStates.DataTerminalReady)
End If
If cRTS.Value Then
  watchThese.Add(SerialConnection.LineStates.RequestToSend)
End If
If cRI.Value Then
  watchThese.Add(SerialConnection.LineStates.RingIndicator)
End If

' Set the lines that we want to watch.  Note
' that calling this function will clear any lines
' that we were watching previously, it doesn't
' append.
SerialConnection1.LineChangeNotification(watchThese)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

To use a `ParamArray</api/language/paramarray>`, you pass the class constants for the line states you want to watch. You don't need to set up a real array.

``` xojo
SerialConnection1.RequestLineChangeNotification(Serial.LineRI, Serial.LineDTR, Serial.LineCTS)
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

To use an `enumeration</api/data_types/enumeration>`, create an array of `LineStates<serialconnection.linestates>` you wish to watch, then pass in the array:

``` xojo
Var states() As SerialConnection.LineStates
states.Add(SerialConnection.LineStates.RingIndicator)
states.Add(SerialConnection.LineStates.DataTerminalReady)
states.Add(SerialConnection.LineStates.ClearToSend)
SerialConnection1.RequestLineChangeNotification(states)
```

## Compatibility

|                       |                       |
|-----------------------|-----------------------|
| **Project Types**     | Console, Desktop, Web |
| **Operating Systems** | All                   |

<div class="seealso">

`Object</api/data_types/additional_types/object>` parent class; `SerialDevice</api/hardware/serialdevice>` class; `Readable</api/files/readable>`, `Writeable</api/files/writeable>` class interfaces; `Connecting to a Serial Device</getting_started/tutorials/connecting_to_a_serial_device>`; `Updating Older Projects</resources/updating_older_projects>` topic

</div>
