# Appendix A Code Examples (.NET)

#### Initialize Device

<pre><code>MTDevice device = new MTDevice(); if (device != null)
{
        device.setConnectionType(MTConnectionType.USB);
<strong>
</strong><strong>        device += OnDeviceConnectionStateChanged; 
</strong><strong>        device += onDeviceResponseMessage; 
</strong><strong>        device += onDeviceNotificationMessage;
</strong>}
</code></pre>

#### Connect to Device

```
if (device != null)
{
device.openDevice();
}
```

#### Send Data String to Device

```
String dataString = “C00101C10100C20114”; 
device.sendDataString(dataString);
```

#### Send Data Bytes to Device

```
Byte[] dataBytes = new Byte[9] {0xC0,0x01,0x01,
                                0xC1,0x01,0x00,
                                0xC2,0x01,0x14};
device.sendDataBytes(dataBytes);
```

#### Send MTCMSMessage to Device

```
MTCMSMessage message = new MTCMSMessage(0x01,0x00,0x14, 0xC4, null); 
device.sendMTCMSMessage(message);
```

#### Send MTCMSRequestMessage to Device

```
MTCMSRequestMessage request = new MTCMSRequestMessage
                                (0x00,0x14, 0xC4, false);
device.sendMTCMSMessage(request);
```

#### Receiving Connection State Updates from Device

```
protected void OnDeviceConnectionStateChanged(object sender, 
MTConnectionState state)
{
    if (state == MTConnectionState.Connected)
    {
        sendToDisplay(“Device is Connected”);
    }
    else if (state == MTConnectionState.Disconnected)
    {
        sendToDisplay(“Device is Disconnected”);
    }
}
```

#### Receiving Response Message from Device

```
protected void OnDeviceResponseMessage(object sender, 
MTCMSResponseMessage response)
{
        sendToDisplay(“[Response Message]” 
        sendToDisplay(“AppID: ” + response.getApplicationID()); 
        sendToDisplay(“CommandID: ” + response.getCommandID());
        sendToDisplay(“ResultCode: ” + response.getResultCode());
}
```

#### Receiving Notification Message from Device

```
protected void OnDeviceNotificationMessage(object sender, 
MTCMSNotificationMessage notification)
{
        sendToDisplay(“[Notification Message]” 
        sendToDisplay(“AppID: ” notification. getApplicationID() ); 
        sendToDisplay(“CommandID: ” + notification.getCommandID());
        sendToDisplay(“ResultCode: ” + notification.getResultCode());
}
```

#### Close Device

```
if (device != null)
{
        device.closeDevice();
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developer.magtek.com/hardware/oem-readers-and-components/oem-readers/odynamo/documentation/programmers-manuals/common-message-structure-mtcms-microsoft-.net-java-applet/appendix-a-code-examples-.net.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
