Skip to content

Commit

Permalink
Fixed UART data reader functionalities.
Browse files Browse the repository at this point in the history
  • Loading branch information
nthnn committed Apr 18, 2024
1 parent 030f1c7 commit 03dc846
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions uart/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ import (
* @return The read float32 value and nil if successful, or 0 and an error if unsuccessful.
*/
func ReadFloat32() (float32, error) {
var buf [4]byte
buf := []byte{0, 0, 0, 0}
num, _ := machine.Serial.Read(buf)

for num == 0 {
num, _ = machine.Serial.Read(buf)
}

for i := uint8(0); i < uint8(4); i++ {
value, err := machine.Serial.ReadByte()

Expand All @@ -44,15 +50,21 @@ func ReadFloat32() (float32, error) {
buf[i] = value
}

return util.BytesToFloat32(buf), nil
return util.BytesToFloat32([4]byte(buf)), nil
}

/**
* @brief Reads a uint16 value from UART.
* @return The read uint16 value and nil if successful, or 0 and an error if unsuccessful.
*/
func ReadUint16() (uint16, error) {
var buf [2]byte
buf := []byte{0, 0}
num, _ := machine.Serial.Read(buf)

for num == 0 {
num, _ = machine.Serial.Read(buf)
}

for i := uint8(0); i < uint8(2); i++ {
value, err := machine.Serial.ReadByte()

Expand All @@ -63,18 +75,20 @@ func ReadUint16() (uint16, error) {
buf[i] = value
}

return util.BytesToUint16(buf), nil
return util.BytesToUint16([2]byte(buf)), nil
}

/**
* @brief Reads a uint8 value from UART.
* @return The read uint8 value and nil if successful, or 0 and an error if unsuccessful.
*/
func ReadUint8() (uint8, error) {
value, err := machine.Serial.ReadByte()
if err != nil {
return uint8(0), err
buf := []byte{0}
num, err := machine.Serial.Read(buf)

for num == 0 {
num, err = machine.Serial.Read(buf)
}

return value, nil
return buf[0], err
}

0 comments on commit 03dc846

Please sign in to comment.