| 1 | /* |
| 2 | Package serial provides a cross-platform serial reader and writer. |
| 3 | */ |
| 4 | package serial |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "io" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | var ( |
| 13 | // ErrTimeout is occurred when timing out. |
| 14 | ErrTimeout = errors.New("serial: timeout") |
| 15 | ) |
| 16 | |
| 17 | // Config is common configuration for serial port. |
| 18 | type Config struct { |
| 19 | // Device path (/dev/ttyS0) |
| 20 | Address string |
| 21 | // Baud rate (default 19200) |
| 22 | BaudRate int |
| 23 | // Data bits: 5, 6, 7 or 8 (default 8) |
| 24 | DataBits int |
| 25 | // Stop bits: 1 or 2 (default 1) |
| 26 | StopBits int |
| 27 | // Parity: N - None, E - Even, O - Odd (default E) |
| 28 | // (The use of no parity requires 2 stop bits.) |
| 29 | Parity string |
| 30 | // Read (Write) timeout. |
| 31 | Timeout time.Duration |
| 32 | // Configuration related to RS485 |
| 33 | RS485 RS485Config |
| 34 | } |
| 35 | |
| 36 | // platform independent RS485 config. Thie structure is ignored unless Enable is true. |
| 37 | type RS485Config struct { |
| 38 | // Enable RS485 support |
| 39 | Enabled bool |
| 40 | // Delay RTS prior to send |
| 41 | DelayRtsBeforeSend time.Duration |
| 42 | // Delay RTS after send |
| 43 | DelayRtsAfterSend time.Duration |
| 44 | // Set RTS high during send |
| 45 | RtsHighDuringSend bool |
| 46 | // Set RTS high after send |
| 47 | RtsHighAfterSend bool |
| 48 | // Rx during Tx |
| 49 | RxDuringTx bool |
| 50 | } |
| 51 | |
| 52 | // Port is the interface for controlling serial port. |
| 53 | type Port interface { |
| 54 | io.ReadWriteCloser |
| 55 | // Connect connects to the serial port. |
| 56 | Open(*Config) error |
| 57 | } |
| 58 | |
| 59 | // Open opens a serial port. |
| 60 | func Open(c *Config) (p Port, err error) { |
| 61 | p = New() |
| 62 | err = p.Open(c) |
| 63 | return |
| 64 | } |