I am trying to use a Honeywell 1911i Barcode Scanner through a serial port on Ubuntu 22.04 machine.
I am having an issue where attempting to set the baud rate of the serial port via stty command does not work.
I perform:
sudo stty -F /dev/ttyS6 115200
Then the output of sudo stty /dev/ttyS6 gives:
speed 9600 baud; line = 0;
min = 1; time = 0;
-brkint -icrnl -imaxbel
-opost -onlcr
-isig -icanon -iexten -echo -echoe -echok -echoctl -echoke
I cannot get the baud rate to change and stay changed.
The scanner is set to 115200, as well as in minicom. The code that processes the results of the scanner also opens the port with 115200.
EDIT: Upon further testing, it appears to be my software launching that is resetting the baud rate of the serial port to 9600, though I am unsure how that is possible so any insight is still appreciated.
CODE:
type SerialScanner struct {
//Scanner Options
PortName string
PortList []string
PortOpen bool
baud int
bufferLen int
portActive bool
//Port for Server
port string
//Channels for GoRoutine Interaction
readCh chan []byte
quitCh chan bool
ExitCh chan bool
}
func NewSerialScanner(port string) *SerialScanner {
scanner := SerialScanner{
PortName: "",
PortList: []string{},
baud: 115200,
bufferLen: 128,
port: port,
portActive: true,
readCh: make(chan []byte),
quitCh: make(chan bool),
ExitCh: make(chan bool),
}
scanner.LoadState() // This just reads some information I have stored in a file.
if scanner.PortName != "" {
scanner.OpenPort()
}
return &scanner
}
func (scanner *SerialScanner) OpenPort() error {
scanner.SavePort(scanner.PortName) // This saves the port being used in a file. User can switch between serial ports.
go scanner.StartReading()
return nil
}
func (scanner *SerialScanner) StartReading() error {
var err error
mode := &serial.Mode{BaudRate: scanner.baud, Parity: serial.NoParity, DataBits: 8, StopBits: serial.OneStopBit}
port, err := scanner.openPort(mode)
if logger.HandleError(err, fmt.Sprintf("Failed to open serial port %s", scanner.PortName)) != nil {
return err
}
scanner.PortOpen = true
logger.PrintLogInfo(fmt.Sprintf("Opened serial port %s", scanner.PortName))
go scanner.MoniterHoneywellScannerConnection(&port)
reader := bufio.NewReader(port)
go scanner.read(reader)
for {
select {
case buf := <-scanner.readCh:
scanner.portActive = true
_ = scanner.publish(buf)
go scanner.read(reader)
case <-scanner.quitCh:
err = port.Close()
if logger.HandleError(err, fmt.Sprintf("Failed to close serial port %s", scanner.PortName)) != nil {
return err
}
scanner.PortOpen = false
logger.PrintLogInfo(fmt.Sprintf("Closed serial port %s", scanner.PortName))
return nil
case <-time.After(60 * time.Second):
scanner.portActive = false
}
}
}
func (scanner SerialScanner) MoniterHoneywellScannerConnection(port serial.Port) {
for {
select {
case <-scanner.ExitCh:
return
case <-time.After(60 * time.Second):
fmt.Println("Testing Serial Port Connection")
_, err := (*port).Write([]byte("232bad?.\r\n"))
if err != nil {
logger.PrintLogError("Failed to write to serial port")
}
}
}
}
func (scanner *SerialScanner) StopReading() {
if scanner.PortOpen {
scanner.quitCh <- true
}
}
func (scanner SerialScanner) read(reader bufio.Reader) {
buf := make([]byte, scanner.bufferLen)
//Read blocks until at least one byte is sent or the port is closed
n, err := reader.Read(buf)
_ = logger.HandleError(err, fmt.Sprintf("Failed to read from serial port %s", scanner.PortName))
if scanner.PortOpen {
scanner.readCh <- buf[:n]
}
}