OpenNETCF Serial - strings struncated by NULL char

  • Thread starter Thread starter anbeyon
  • Start date Start date
A

anbeyon

Dear all,

I am having some trouble using the OpenNETCF libraries. Actually It's
VB.NET related.

My data being read from the serial port is being truncated in some way.

This is what I do.

<Snippet>

Dim sBuffer As String = ""
Dim Buffer(MasterPort.InBufferCount - 1) As Byte

Buffer = MasterPort.Input
sBuffer = sBuffer + UCase(System.Text.Encoding.ASCII.GetString(Buffer,
0, Buffer.Length))

</Snippet>

I am expecting to read a 6 byte command from the port and this is what
is reported by sBuffer.Length. However if one of the chars is the
string is = 0 the string is truncated.

Chars up to the 0 char can be read using sBuffer.Substring(x,y) those
including the 0 char and afterwards cannot.

How can I get around this problem ?

Thanks

Anbeyon
 
A 0 data byte is interpreted as a null, and string are null terminated, so
the behavior you see is expected. What are you expecting the 0 to become in
the string?

-Chris
 
Hi Chris

Thanks for the reply. I realised that what I was seeing was what I
should expect I just wasn't sure how to deal with it.

The 0 byte needed to be interpreted as a 0.

I worked around it bu using the .Chars() method and inspected each
value using that.

Thanks

Anbeyon
 
Back
Top