# Serial Output

The Serial Output sends actuator positions over a serial (COM) port. It's designed for custom or DIY motion controllers that accept serial data — for example, Arduino-based builds, Sabertooth motor drivers, or any hardware that reads position commands from a serial port. With configurable framing, output format, and per-axis prefixes/suffixes, it can adapt to almost any serial protocol.

---

## When to Use This

Use the Serial Output when:

- You have a DIY motion controller that accepts serial data.
- Your controller is not directly supported by one of the dedicated outputs (SMC3, Thanos, DMover, M4S, VNM).
- You need to interface with motor drivers like Sabertooth that use a simple serial protocol.
- You want full control over the byte format, framing, and encoding of position data.

---

## Setup Steps

1. Connect your serial device to your PC via USB.
2. In DR Sim Manager, go to the **Outputs** tab and click the **+** tab.
3. Select **Serial Output** from the list.
4. Select your **COM port** and set the **baud rate** to match your device.
5. Configure **parity**, **data bits**, and **stop bits** if your device requires non-default values.
6. Set the **axis count** and **position bits** to match your controller's protocol.
7. Choose the **output type** (Binary, Decimal, or Hexadecimal).
8. For each axis, set the **prefix**, **suffix**, and **invert** options as needed.
9. Optionally set **startup** and **shutdown strings** for initialization and parking commands.

---

## Settings

### Connection Settings

- **COM Port** — The serial port your controller is connected to. Click **Refresh** to rescan.
- **Baud Rate** — Communication speed, selectable from 300 to 921,600. Must match your device's configuration.
- **Parity** — Error-checking mode: None, Odd, or Even.
- **Data Bits** — Number of data bits per frame: 5, 6, 7, or 8.
- **Stop Bits** — Number of stop bits: 1, 1.5, or 2.

### Output Settings

- **Axis Count** — Number of axes to transmit (1 or more). Each axis appears as a mappable channel.
- **Position Bits** — Resolution of each position value in bits (default: 8). Higher values give finer positioning. For example, 8-bit gives 256 positions (0–255), 16-bit gives 65,536 positions (0–65535).
- **Output Type** — Data format for position values:
  - **Binary** — Raw binary encoding (most compact; typical for embedded controllers).
  - **Decimal** — ASCII decimal numbers (human-readable; e.g., "1023").
  - **Hexadecimal** — ASCII hex numbers (e.g., "3FF").
- **Output Rate** — How frequently position updates are sent, in milliseconds (default: 2 ms).

### Startup & Shutdown Strings

- **Startup String** — A byte string sent once when the output connects. Useful for initializing your controller or enabling motors.
- **Shutdown String** — A byte string sent once when the output disconnects. Useful for parking actuators or disabling motors.

Both strings support special notation:

- **Hex bytes** — Use `0xFF` notation to send raw byte values (e.g., `0xFFFF` sends two bytes: 0xFF 0xFF).
- **ASCII codes** — Use `<code>` notation to send specific ASCII characters (e.g., `<13>` sends a carriage return, `<10>` sends a line feed).
- **Plain text** — Any other characters are sent as-is.

**Examples:**

- `INIT<13><10>` — Sends the text "INIT" followed by a carriage return and line feed.
- `0xFF0x00` — Sends two raw bytes: 0xFF and 0x00.
- `MOTORS_ON<13>` — Sends "MOTORS_ON" followed by a carriage return.

---

## Axis Mapping

For each axis, you can configure:

- **Device** — The sim component to read from.
- **Actuator** — Which actuator output to send.
- **Prefix** — Bytes to prepend before the position value (e.g., `S` for Sabertooth-style protocols). Supports the same special notation as startup/shutdown strings.
- **Suffix** — Bytes to append after the position value (e.g., `<13>` for a carriage return line ending). Supports the same special notation as startup/shutdown strings.
- **Invert** — Reverse the direction of this axis (the position value is flipped so that 0 becomes the maximum and vice versa).

### Building a Custom Protocol

The output constructs each packet by concatenating, for each axis: **Prefix + Position Value + Suffix**. All axes are combined into a single packet sent per output cycle.

**Example — Sabertooth-style protocol:**
If your controller expects `S<value><CR>` for each axis, set the prefix to `S` and suffix to `<13>`.

**Example — Simple binary protocol:**
For a controller expecting raw 16-bit binary values with no framing, set Position Bits to 16, Output Type to Binary, and leave prefix and suffix empty.

---

## Troubleshooting

### Device not responding

1. **Check baud rate** — The baud rate must match exactly between DR Sim Manager and your device. A mismatch produces garbled data or silence.
2. **Check COM port** — Verify in **Device Manager > Ports (COM & LPT)**.
3. **Check data bits, parity, stop bits** — These must also match your device's configuration. Most devices use 8 data bits, no parity, 1 stop bit (8N1).
4. **Check output type** — If your device expects ASCII text, use Decimal or Hexadecimal. If it expects raw bytes, use Binary.

### Position values seem wrong or garbled

- Verify the **Position Bits** setting matches what your controller firmware expects.
- Check whether your controller expects **big-endian** or **little-endian** byte order. DR Sim Manager sends binary values in big-endian (most significant byte first).
- If using Decimal output type, check whether your controller expects leading zeros or fixed-width strings.

### Startup string not working

- Use a serial terminal (like PuTTY or the Arduino Serial Monitor) to manually test your startup command and verify the device responds correctly.
- Check that you're using the correct line ending notation (e.g., `<13><10>` for CR+LF).

### COM port not listed or "Access Denied"

See [Serial Output Not Working](../frequently-asked-questions/serial-output-not-working.md) for general COM port troubleshooting steps.
