# Appendix A	Code Examples

#### Connect to Device

```
openDevice();
```

#### Send Command String to Device

```
const char* dataString = “C00101C10100C20114”; 
sendDataString(dataString);
```

#### Send Command Bytes to Device

```
unsigned char* dataBytes =
{0xC0,0x01,0x01,0xC1,0x01,0x00,0xC2,0x01,0x14};

sendDataBytes(dataBytes,9);
```

#### Send MTCMSMessage to Device

```
MTCMSMessage* message = CreateMTCMSMessage(0x01,0x00,0x14, NULL,0, 
false);
sendMTCMSMessage(message); 
ReleaseMTCMSMessage(message);
```

#### Receiving Connection State Updates from Device

```
void _stdcall OnDeviceConnectionStateChanged(void* sender, 
MTConnectionState state)
{
        switch (state)
        {
        case MTConnectionState::Connected: 
                Log(_T("[Connected]")); 
                break;
        case MTConnectionState::Connecting: 
                Log(_T("[Connecting...]")); 
                break;
        case MTConnectionState::Disconnecting: 
                Log(_T("[Disconnecting...]")); 
                break;
        case MTConnectionState::Disconnected: 
                Log(_T("[Disconnected]")); 
                break;
        }
}
```

#### Receiving Response Message from Device

```
void  stdcall OnDeviceResponseMessage(void* sender, const 
MTCMSMessage* response)
{
    processResponseMessage(response);
}
```

#### Receiving Notification Message from Device

```
void  stdcall OnDeviceNotificationMessage(void* sender, const 
MTCMSMessage* notification)
{
    processNotificationMessage(notification);
}
```

#### Close Device <a href="#a.9_close_device" id="a.9_close_device"></a>

```
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-programmers-reference-manual-c++/appendix-a-code-examples.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.
