Malformed UTF-8 (category Ma: Malformed, bad data)

Hi, I have an issue with Malformed UTF-8 (category Ma: Malformed, bad data) when reading input data from a serial port, instead of receiving an Hex data like “0xec” I get “\xec”.
I don’t fully understand way I get the data wrong, but I need to replace the “\x” with “0x” but failed to do so.
Can someone help me?
Thanks in ahead

1 Like

My guess, without seeing your code, is you should be using read(io, UInt8) and not read(io, String)

hi, thanks for the quick reply
this is the code I use:

using LibSerialPort
sp = LibSerialPort.open(ports[1], 115200, ndatabits=8, nstopbits=1)

datain = []
append!(datain, readline(sp))

try

-append!(datain, readline(sp))
+append!(datain, read(sp, UInt8))

Thank you all for the help.
my code look like this now:

using LibSerialPort

ports = get_port_list()
sp = LibSerialPort.open(ports[1], 115200, ndatabits=8, nstopbits=1)

datain = []
while bytesavailable(sp) > 0
  push!(datain, read(sp,UInt8))
end
close(sp)

and now it’s working fine

The issue is that readline treats the read data as a UTF-8 string.
Whereas read can specify the parsed form (type) of the read data.

help?> readline
search: readline readlines readlink

  readline(io::IO=stdin; keep::Bool=false)
  readline(filename::AbstractString; keep::Bool=false)

  Read a single line of text from the given I/O stream or file (defaults to stdin). When reading from a file, the text
  is assumed to be encoded in UTF-8.
help?> read
search: read read! readdir readlink readline readeach readuntil readlines readchomp readbytes! readavailable

  read(io::IO, T)

  Read a single value of type T from io, in canonical binary representation.
1 Like