> For the complete documentation index, see [llms.txt](https://developer.magtek.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.magtek.com/sdks-and-tools/sdks-and-software-libraries/odynamo-sdks/common-message-structure-mtcms-microsoft-.net-java-applet/appendix-code-examples-.net.md).

# Appendix - 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://developer.magtek.com/sdks-and-tools/sdks-and-software-libraries/odynamo-sdks/common-message-structure-mtcms-microsoft-.net-java-applet/appendix-code-examples-.net.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
