How to create a proper byte?

kidotnet

New member
Joined
Jul 27, 2005
Messages
3
Programming Experience
1-3
Hi all,

I'm writing an app to communicate with a serial port device and I'm having trouble creating a proper byte value... I'm using the Rs232 class from gotdotnet, and did this:

VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim connection As New Rs232
		connection.Open(7, 2400, 8, Rs232.DataParity.Parity_None, Rs232.DataStopBit.StopBit_1, 8)
		connection.Write("U!SCVER?")
[color=Black]		connection.Write(&HD)[/color]
		connection.Read(3)
		Dim temp As String
		temp = connection.InputStreamString
		TextBox1.Text = temp
		connection.Close()
	End Sub

Anyway, the problem is in "connection.write(&HD)". "write" is a method that sends a byte stream over the link. I've captured the streams with a serial port listener program, and what i got for the value "&HD" was this (in hex):

31 33

The rest of the characters were fine cuz it's how i wanted it to turn out. As you might notice, 31 and 33 are the ascii values for "1" and "3". But what I wanted to send was the HEX byte "0D" or "00001101" or "13". NOT ascii. I tried everything else like just putting "connection.write(13)" and used CByte and convert.toByte etc etc... nothing works... Please helppppppppp

Best regards,

Ron
 
I know of 2 methods for getting a carriage return or 0Dhex printed on a serial port.

1) you could try using VbCr (visual Basic Carriage return)

eg: connection.Write("hello" & VbCr)

2) you can use Chr or ChrW function as follows

connection.Write("hello" & Chr(13))

Hope this helps

Richard
 
Having problem converting numbers...

Thanks for the tip on 0D = carrier return! That works now....

But now I'm having trouble converting UInt16 into proper bytes... can anyone help?

It keeps giving me the ascii value, though i used UInt16.parse(someInt) and then using convert.tobyte on that...
 
I'm not quite sure what your are trying to achieve, but if I am thinking what you are thinking, and it might be possible to think what you are thinking, and could I possibly know what you are thinking? then...

I am assuming that you want to display a UInt16 as a string then I think u can use the CStr function as..

MyString = CStr(MyUInt16)

Richard
 
Unfortunately, that wasn't what I wanted =[

Ok, sorry for the shady explanation, here i go again:

The micro controller i'm sending data to uses this syntax:

Syntax: “!SC” C R pw.LOWBYTE, pw.HIGHBYTE, $0D

So, from my program, i got the "!SC" part and the ending 0D part correct, but the problem comes with C, R and pw. C and R are single bytes that range from 0-255. and pw is a 16 bit word which is an integer that ranges from 250-1250.

Thing is, when i tried assigning values to C and R as UInt16 and then converting with convert.toByte(myUInt16) i get the ASCII representation of the value (eg: 0 becomes HEX 30, which is ASCII for "0", instead of HEX 00).

Furthermore, for PW, which is a 16bit word, how do I convert a UInt16 into 2 seperate bytes for writing?

Is it just me or is VB kinda bad at manipulating raw bytes... Please help guys... thanks tonss
 
If I get this right,

VB.NET:
 Dim aDouble As DoubleDim 
aByte As ByteaDouble = 125.5678
' The following line of code sets aByte to 126.
aByte = CByte(aDouble)
( Taken from VB Help)
Use this CByte function to convert number types to a byte as shown above.


To convert your 16 bit value into 2 8 bit values then you could try this (the hard way I know!!)

eg: convert 356 into 2 bytes

1) Mask off upper byte by AND'ing with FF00[hex] should give 0100[Hex] as result.
2) Divide by 256 should give 0001[Hex] This can now be converted to byte as above and used for you upper byte.
3)AND original value with 00FF[Hex], should give 0064[Hex], this can be converted to byte as above and used for your lower byte.

If this makes no sense, then I'm sorry, but i'm all outta suggestions

Richard
 
Last edited:
Back
Top