setting / clearing MSB

dbrb2

New member
Joined
Oct 16, 2008
Messages
3
Programming Experience
3-5
Hi,

I wonder if anyone can help. I am using VB.NET to read (and send) data to/from a TCP server. This server allows me to sniff/insert data onto physical RS232 data lines. However, what it does not do is strip / insert the parity bit for me.

This means that initially, the string I read from incoming data to my socket is decoded incorectly if the parity bit is set. For example, when I am using even parity, the character '1' is represented on the data lines by:

MSB...10110001...LSB

Where the MSB is my even parity bit. However, initially this is decoded as a member of the extended ascii set, character "±" (any characters with zero parity bit decoded OK of course, since the MSB was not set)

By initial solution was to convert the received string into a byte array, then loop through every element of that array and AND the byte with 127, which in binary is:

01111111

This should change any set 8th bits to 0, and so when I then convert the byte array back to a string I should have the correct characters...but it seems not to be working.

Any thoughts?
 
I don't get it..

Youre reading a STRING of length 8 from your device, instead of just a byte?

e.g. youre reading this:

"10111001"

which is a string rep of a binary number which is: 185 in decimal, or B9 in hex, or a Cedilla in ASCII

or are you reading LITERALLY one byte, B9 with:

Dim b as Byte()
b = mySocket.ReadByte()

or similar?

See, if youre reading it as a string, not only is that a pain in the ass in converting it back to a byte (performance hit rather than lines of code) so you can bitwise it, but it's a huge waste of bandwidth too

-

So first, let's assume that youre NOT doing this..

and you have read a single byte b, and its value is 0xB9, but you want to mask off the MSB so it actually becomes 0x39:

Dim b as Byte = &HB9
Dim newB as Byte = b And &H7F

newB is now 39.. Why?

VB.NET:
b =10111001
7f=01111111
Result of AND op:
b =00111001

MSB of <any number> ANDed with 7F is always 0


-

Now lets assume that you are reading a string rep of a binary number:

Dim b as Byte = Convert.ToByte("10111001", 2)

-

If, somewhere youre reading a byte and then ToString(2) it in order to turn it into a string rep of a binary number.. please dont convert it back using ToByte(string, radix).. keep it as a byte all the way through
 
Hi,


This should change any set 8th bits to 0, and so when I then convert the byte array back to a string I should have the correct characters...but it seems not to be working.

Any thoughts?

ps the reason this soluton does not work is because your string of:

"10111001"

becomes an array of (c# syntax; vb is too ugly):

byte[]{ 0x31, 0x30, 0x31, 0x31, 0x31, 0x30, 0x30, 0x31 }


You wouldnt then AND every element with 127 because all those elements are already below 127, being the numerical values of the ASCII characters for ZERO and ONE

What you would do is set the leading character to 0 (0x30) with:

byteArray(0) = &H30


-

but then, you wouldnt even do that!

If you had a string of:

"10111001"

and you wanted it to be:

"00111001"

You would do:

newStr = "0" & oldStr.Substring(1)


???


as I say.. do your byte ops in bytes, dont convert everything to string because it makes it easier to think about; it also makes it much slower and consume 16x more memory (unicode..)
 
The reason I start with a string is that I am using this component:

CodeProject: Winsock Revamped. Free source code and programming help

Which reads incoming data into a buffer string. There may be a way of capturing directly to bytes - I will have to check. But since the parity bit is set on certain characters, the ascii character in the string is incorrect. This is the problem I need to address...

I do not have a string of "10111001" - I have a string of "some stuff" which I then convert to a byte array, whic I assume consists of an element for every character in the string. Each element I then try to perform a bitwise AND on to remove parity, before converting the array of bytes back into a single string...
 
OK well the logic is the same

If your serial device is wanting to send you a string of:

"01234"

which is bytes:
0x30, 0x31, 0x32, 0x33, 0x34

which is hence binary (with parity bit set):
00110000, 10110001, 10110010, 00110011, 10110100

(note that bold sequences have a parity bit set)

and is hence looking like:
0±²3´

Then you do:
VB.NET:
    Dim srcStr As String = "01234"
    Dim bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(srcStr)

    'set parities like what youre getting
    bytes(1) += &H80
    bytes(2) += &H80
    bytes(4) += &H80

    MessageBox.Show(System.Text.Encoding.ASCII.GetString(bytes))

    'mask off the leading bit of every element
    For i As Integer = 0 To bytes.Length - 1
      bytes(i) = bytes(i) And &H7F
    Next i

    Dim destStr As String = System.Text.Encoding.ASCII.GetString(bytes)

    MessageBox.Show(destStr)
 
Back
Top