ManRas
ManRas

Reputation: 57

How to send STX and ETX over sockets

With VB.NET, i need to send to a tcp listener the following:

<STX>CODER<ETX>

I understand i need to send stx and etx but i don't know how to do this.

i have declared:

Public Const STX = &H2
Public Const ETX = &H3

and i try to send a string like this:

var = STX & "CODEX" & ETX
bytes = Encoding.ASCII.GetBytes(var)

but it doesn't work

Can anyone give some help? Thanks in advance

Upvotes: 0

Views: 2764

Answers (1)

Visual Vincent
Visual Vincent

Reputation: 18310

&H2 and &H3 (0x2 and 0x3) are the character codes for STX and ETX. They're just numbers, meaning your two constants currently contain the integers 2 and 3 respectively. For this reason your string literally results in "2CODEX3".

You need to convert the character codes into actual Chars to be able to put them in a string. Since we can't do this with constants in VB.NET, we'll have to use read-only variables instead:

Public ReadOnly STX As Char = ChrW(&H2)
Public ReadOnly ETX As Char = ChrW(&H3)

Upvotes: 2

Related Questions