|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2019 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to modify or |
| 12 | +// otherwise use the software for commercial activities involving the Arduino |
| 13 | +// software without disclosing the source code of your own applications. To purchase |
| 14 | +// a commercial license, send an email to [email protected]. |
| 15 | + |
| 16 | +package monitors |
| 17 | + |
| 18 | +import ( |
| 19 | + "github.com/pkg/errors" |
| 20 | + serial "go.bug.st/serial.v1" |
| 21 | +) |
| 22 | + |
| 23 | +// SerialMonitor is a monitor for serial ports |
| 24 | +type SerialMonitor struct { |
| 25 | + port serial.Port |
| 26 | +} |
| 27 | + |
| 28 | +// OpenSerialMonitor creates a monitor instance for a serial port |
| 29 | +func OpenSerialMonitor(portName string, baudRate int) (*SerialMonitor, error) { |
| 30 | + port, err := serial.Open(portName, &serial.Mode{BaudRate: baudRate}) |
| 31 | + if err != nil { |
| 32 | + return nil, errors.Wrap(err, "error opening serial monitor") |
| 33 | + } |
| 34 | + |
| 35 | + return &SerialMonitor{ |
| 36 | + port: port, |
| 37 | + }, nil |
| 38 | +} |
| 39 | + |
| 40 | +// Close the connection |
| 41 | +func (mon *SerialMonitor) Close() error { |
| 42 | + return mon.port.Close() |
| 43 | +} |
| 44 | + |
| 45 | +// Read bytes from the port |
| 46 | +func (mon *SerialMonitor) Read(bytes []byte) (int, error) { |
| 47 | + return mon.port.Read(bytes) |
| 48 | +} |
| 49 | + |
| 50 | +// Write bytes to the port |
| 51 | +func (mon *SerialMonitor) Write(bytes []byte) (int, error) { |
| 52 | + return mon.port.Write(bytes) |
| 53 | +} |
0 commit comments