<commit_before>package modbusone

import (
	"fmt"
	"io"
	"time"

	"github.com/goburrow/serial"
)

//SerialContext is an interface implemented by SerialPort, can also be mocked for testing.
type SerialContext interface {
	io.ReadWriteCloser
	//RTUMinDelay returns the minimum required delay between packets for framing
	MinDelay() time.Duration
	//RTUBytesDelay returns the duration is takes to send n bytes
	BytesDelay(n int) time.Duration
}

//SerialPort has configuration and I/O controller.
type SerialPort struct {
	// Serial port configuration.
	serial.Config
	// port is a platform-dependent data structure for serial port.
	port serial.Port

	isConnected bool
}

//Open opens the SerialPort with Config
func (s *SerialPort) Open(c serial.Config) (err error) {
	if s.isConnected {
		return fmt.Errorf("already opened")
	}
	s.Config = c
	if s.port == nil {
		s.port, err = serial.Open(&s.Config)
	} else {
		//reuse closed port
		err = s.port.Open(&s.Config)
	}
	s.isConnected = err == nil
	return
}

func (s *SerialPort) Read(b []byte) (int, error) {
	return s.port.Read(b)
}
func (s *SerialPort) Write(b []byte) (int, error) {
	debugf("SerialPort Write:%x\n", b)
	return s.port.Write(b)
}

//Close closes the SerialPort
func (s *SerialPort) Close() error {
	s.isConnected = false
	return s.port.Close()
}

//MinDelay returns the minum Delay of 3.5 bytes between packets or 1750 mircos
func (s *SerialPort) MinDelay() time.Duration {
	delay := 1750 * time.Microsecond
	br := time.Duration(s.BaudRate)
	if br <= 19200 {
		//time it takes to send 3.5 or 7/2 chars (a char of 8 bits takes 11 bits on wire)
		delay = (time.Second*11*7 + (br * 2) - 1) / (br * 2)
	}
	return delay
}<commit_msg>hide timeout errors from read<commit_after>package modbusone

import (
	"fmt"
	"io"
	"time"

	"github.com/goburrow/serial"
)

//SerialContext is an interface implemented by SerialPort, can also be mocked for testing.
type SerialContext interface {
	io.ReadWriteCloser
	//RTUMinDelay returns the minimum required delay between packets for framing
	MinDelay() time.Duration
	//RTUBytesDelay returns the duration is takes to send n bytes
	BytesDelay(n int) time.Duration
}

//SerialPort has configuration and I/O controller.
type SerialPort struct {
	// Serial port configuration.
	serial.Config
	// port is a platform-dependent data structure for serial port.
	port serial.Port

	isConnected bool
}

//Open opens the SerialPort with Config
func (s *SerialPort) Open(c serial.Config) (err error) {
	if s.isConnected {
		return fmt.Errorf("already opened")
	}
	s.Config = c
	if s.port == nil {
		s.port, err = serial.Open(&s.Config)
	} else {
		//reuse closed port
		err = s.port.Open(&s.Config)
	}
	s.isConnected = err == nil
	return
}

//Read reads the serial port and removes the timeout error
func (s *SerialPort) Read(b []byte) (int, error) {
	n, err := s.port.Read(b)
	if err == serial.ErrTimeout {
		debugf("serial read timeout")
		return n, nil
	}
	return n, err
}
func (s *SerialPort) Write(b []byte) (int, error) {
	debugf("SerialPort Write:%x\n", b)
	return s.port.Write(b)
}

//Close closes the SerialPort
func (s *SerialPort) Close() error {
	s.isConnected = false
	return s.port.Close()
}

//MinDelay returns the minum Delay of 3.5 bytes between packets or 1750 mircos
func (s *SerialPort) MinDelay() time.Duration {
	delay := 1750 * time.Microsecond
	br := time.Duration(s.BaudRate)
	if br <= 19200 {
		//time it takes to send 3.5 or 7/2 chars (a char of 8 bits takes 11 bits on wire)
		delay = (time.Second*11*7 + (br * 2) - 1) / (br * 2)
	}
	return delay
}
{'fraction_non_alphanumeric': 0.11619421835531855, 'fraction_numerical': 0.015717092337917484, 'mean_word_length': 3.677165354330709, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '553041', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1216, 'n_words': 425}