Using Net.Sockets and encoding.ascii

J

Jason L James

Hi All,

I am trying to send information from my vb.Net
app to a piece of hardware with an embedded
TCP/IP controller using sockets.

I can send information so the sockets are working
but the data is garbage!!!

It works if the ascii of the character I am sending
is less than 127, however, if I send ascii 128 or
above (up to 255, obviously) then things start to
go wrong.

I am sure it is to do with the encoding of the
byte and thee fact that it seems to be signed
as oppossed to unsigned.

Decimal 1 in binary is
00000001

Decimal 127 in binary is
011111111

In an unisgned int, decimal 128 is
100000000

and decimal 255 is
11111111

How can I get this to be outputted to my
embedded controller using sockets???

Please help.

This is what I have used for my output stream to the
socket.

Dim writer As New IO.StreamWriter(m_Client.GetStream(),
System.Text.Encoding.ASCII)

Regards,

Jason.
 
D

Dick Grier

Hi,

Encoding.Ascii supports only ASCII (characters with values between 0 and
127, as you have observed). You should use a binary Writer, not Text.

Dim writer As New BinaryWriter(Stream)

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 4th
Edition ISBN 1-890422-28-2 (391 pages) published July 2004.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top