github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/common/serial/serial.go (about)

     1  package serial
     2  
     3  import (
     4  	"encoding/binary"
     5  	"io"
     6  )
     7  
     8  // ReadUint16 reads first two bytes from the reader, and then converts them to an uint16 value.
     9  func ReadUint16(reader io.Reader) (uint16, error) {
    10  	var b [2]byte
    11  	if _, err := io.ReadFull(reader, b[:]); err != nil {
    12  		return 0, err
    13  	}
    14  	return binary.BigEndian.Uint16(b[:]), nil
    15  }
    16  
    17  // WriteUint16 writes an uint16 value into writer.
    18  func WriteUint16(writer io.Writer, value uint16) (int, error) {
    19  	var b [2]byte
    20  	binary.BigEndian.PutUint16(b[:], value)
    21  	return writer.Write(b[:])
    22  }
    23  
    24  // WriteUint64 writes an uint64 value into writer.
    25  func WriteUint64(writer io.Writer, value uint64) (int, error) {
    26  	var b [8]byte
    27  	binary.BigEndian.PutUint64(b[:], value)
    28  	return writer.Write(b[:])
    29  }