# Serial interface

{% hint style="info" %}
**Supported event types:** `logs`
{% endhint %}

The *Serial* input plugin lets you retrieve messages and data from a serial interface.

## Configuration parameters

This plugin has the following configuration parameters:

| Key         | Description                                                                                                                                 | Default |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------- | ------- |
| `bitrate`   | The bit rate for the communication. For example: `9600`, `38400`, `115200`.                                                                 | *none*  |
| `file`      | Absolute path to the device entry. For example, `/dev/ttyS0`.                                                                               | *none*  |
| `format`    | Specify the format of the incoming data stream. The options are `json` and `none`. `format` and `separator` can't be used at the same time. | `none`  |
| `min_bytes` | The serial interface expects at least `min_bytes` to be available before processing the message.                                            | `1`     |
| `separator` | Specify a separator string that's used to determine when a message ends.                                                                    | *none*  |
| `threaded`  | Indicates whether to run this input in its own [thread](https://docs.fluentbit.io/manual/administration/multithreading#inputs).             | `false` |

## Get started

To retrieve messages by using the Serial interface, you can run the plugin from the command line or through the configuration file:

### Command line

The following example loads the input serial plugin where it set a `bitrate` of `9600`, listens from the `/dev/tnt0` interface, and uses the custom tag `data` to route the message.

```shell
fluent-bit -i serial -t data -p file=/dev/tnt0 -p bitrate=9600 -o stdout -m '*'
```

The interface (`/dev/tnt0`) is an emulation of the serial interface. Further examples will write some message to the other end of the interface. For example, `/dev/tnt1`.

```shell
echo 'this is some message' > /dev/tnt1
```

In Fluent Bit you can run the command:

```shell
fluent-bit -i serial -t data -p file=/dev/tnt0 -p bitrate=9600 -o stdout -m '*'
```

Which should produce output like:

```
...
[0] data: [1463780680, {"msg"=>"this is some message"}]
...
```

Using the `separator` configuration, you can send multiple messages at once.

Run this command after starting Fluent Bit:

```shell
echo 'aaXbbXccXddXee' > /dev/tnt1
```

Then, run Fluent Bit:

```shell
fluent-bit -i serial -t data -p file=/dev/tnt0 -p bitrate=9600 -p separator=X -o stdout -m '*'
```

This should produce results similar to the following:

```
...
[0] data: [1463781902, {"msg"=>"aa"}]
[1] data: [1463781902, {"msg"=>"bb"}]
[2] data: [1463781902, {"msg"=>"cc"}]
[3] data: [1463781902, {"msg"=>"dd"}]
...
```

### Configuration file

In your main configuration file append the following sections:

{% tabs %}
{% tab title="fluent-bit.yaml" %}

```yaml
pipeline:
  inputs:
    - name: serial
      tag: data
      file: /dev/tnt0
      bitrate: 9600
      separator: X

  outputs:
    - name: stdout
      match: '*'        
```

{% endtab %}

{% tab title="fluent-bit.conf" %}

```
[INPUT]
  Name      serial
  Tag       data
  File      /dev/tnt0
  Bitrate   9600
  Separator X

[OUTPUT]
  Name   stdout
  Match  *
```

{% endtab %}
{% endtabs %}

## Emulating a serial interface on Linux

You can emulate a serial interface on your Linux system and test the serial input plugin locally when you don't have an interface in your computer. The following procedure has been tested on Ubuntu 15.04 running Linux Kernel 4.0.

### Build and install the `tty0tty` module

1. Download the sources:

   ```shell
   git clone https://github.com/freemed/tty0tty
   ```
2. Unpack and compile:

   ```shell
   cd tty0tty/module

   make
   ```
3. Copy the new kernel module into the kernel modules directory:

   ```shell
   sudo cp tty0tty.ko /lib/modules/$(uname -r)/kernel/drivers/misc/
   ```
4. Load the module:

   ```shell
   sudo depmod

   sudo modprobe tty0tty
   ```

   You should see new serial ports in `dev` (`ls /dev/tnt\*\`).
5. Give appropriate permissions to the new serial ports:

   ```shell
   sudo chmod 666 /dev/tnt*
   ```

When the module is loaded, it will interconnect the following virtual interfaces:

```
/dev/tnt0 <=> /dev/tnt1
/dev/tnt2 <=> /dev/tnt3
/dev/tnt4 <=> /dev/tnt5
/dev/tnt6 <=> /dev/tnt7
```
