Sending data to serial port in binary format

Hello,

I have a vb.net form where the user enters the following data:
1. Input1: 02
2. Input2: 05
3. Input3: 12

Now how do I convert the above data into hex and send in binary format to the serial port?

Regards,
MI
vj_miAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChloesDadCommented:
converting to hex is done with the tostring function

hexstring = mynumber.tostring("X")

to then convert each hex digit to a character

dim charnum as integer

for charnum = 0 to hexstring.length -1

dim hexcharacter = hexstring.Substring(charnum,1)

' get the numeric value of each character

dim numvalue as integer = convert.toint32(hexcharacter,16)

' write the character to the serial port

serialport.write chr(numvalue)

next charnum
0
vj_miAuthor Commented:
Hello,

I want to write binary data to the serial port. When why are we converting the hex value back to integer as in:

dim numvalue as integer = convert.toint32(hexcharacter,16)
' write the character to the serial port
serialport.write chr(numvalue)

Regards,
MI
0
vj_miAuthor Commented:
Another question:
hexstring = mynumber.tostring("X")
What is mynumber? Considering that the above values I have given is in text boxes as:
txtInput1.Text = "02"   'This is entered by user
txtInput2.Text = "05"   'This is entered by user
txtInput3.Text = "12"   'This is entered by user

Now can you please let me know how to re-write you above code?

Regards,
MI
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

CodeCruiserCommented:
0
ChloesDadCommented:
When you said binary I assumed you meant a sequence of hex characters eg F7h becomes char(15) char(7)

If that is not the case then can you giove examples of how each hex character should be transmitted?

I wrote code to show you how to do it, mynumber represents the number that you want to decode, as I didnt have the rest of your program with your exact variable names. You need to insert my code into your program and change the variable names accordingly.

You could convert my code into a function and then call the function once for each text box as shown below
....
serialport.write dataconvert(txtinput1.text)
serialport.write dataconvert(txtinput2.text)
serialport.write dataconvert(txtinput3.text)
....
 
private function dataconvert(numberToConvert as string) as string
 
dim OutputString as new system.text.stringbuilder
 
' convert the numeric string to a hex string via an integer
dim HexString = convert.tointeger(mynumber.tostring("X"))
 
' Convert each hex digit to a character 
 
dim charnum as integer
 
for charnum = 0 to hexstring.length -1
 
  dim hexcharacter = hexstring.Substring(charnum,1)
  ' get the numeric value of each character
  dim numvalue as integer = convert.toint32(hexcharacter,16)
  ' write the character to the string
 
  outputstring.append chr(numvalue)
 
next charnum
 
return Outputstring.tostring
 
end function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vj_miAuthor Commented:
Hello ChloesDad,

I think I am now trying to get near to your above logic. Since I am not too good at serial port programs, you may find me asking sometimes very basic questions.

Just once again to summarize what I need: My client has asked me to take input in 3 text boxes and then convert to hex and send in binary format through the serial port. The user enters values in pure integer values as I gave an example above.

Now after going through the above code, I still have few queries:
1. In DataConvert function, you are taking parameter numberToConvert as string. But this is not used. Please let me know where to use.

2. I am still not clear with mynumber.tostring("X"). Here where do I define myNumber and what does it represent?

3. In mynumber.tostring("X"), what is X for?

4. To convert into hex, do we not need ConversionToHex method of .Net? I am not sure whether we really need it... but I just read and therefore I am mentioning it.

Your help is highly appreciated.

Regards,
MI
0
ChloesDadCommented:
well spotted...oops, 1 and 2 are both caused by my mistake in the code

' convert the numeric string to a hex string via an integer
dim HexString = convert.tointeger(mynumber.tostring("X"))


should have been

' convert the numeric string to a hex string via an integer
dim HexString = convert.tointeger(numbertoconvert.tostring("X"))

3  the X tells the tostring function to output result in hex format.

4 There are probably other ways to turn an integer into a hex, this is one way. I gererally use the .tostring function. I have not used the conversion.Hex function but it will do the same job, however nether can accept a string as a parameter so we have to convert it to an integer first.
0
vj_miAuthor Commented:
Hello ChloesDad,

Since convert.tointeger is not found in vb.net, it gave error. So I used convert.Int16. But with this I get error on line "for charnum = 0 to hexstring.length -1" saying that .length cannot be used with integer type. So I use CStr as : dim HexString = CStr(mynumber.tostring("X")).
But i get eror now on "X" as some system.IFormatProvider as unable to cast.

I am attaching the code below in code window. I tried to upload the source as zip file but it seems bcause of the .designer.vb file, it does not allow to upload the zip file. Anyway, the code is below.

Regards,
MI


Imports System
Imports System.Text
Imports System.Drawing
Imports System.Windows.Forms
 
 
Public Class Form1
 
    Private Sub btnConvert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnConvert.Click
 
        txtOutput1.Text = dataconvert(txtInput1.Text)
        txtOutput2.Text = dataconvert(txtInput2.Text)
        txtOutput3.Text = dataconvert(txtInput3.Text)
 
    End Sub
 
    Private Function dataconvert(ByVal numberToConvert As String) As String
 
        Dim OutputString As New system.text.stringbuilder
       
        ' convert the numeric string to a hex string via an integer
        Dim HexString = Convert.ToInt16(numberToConvert.ToString("X"))
 
        ' Convert each hex digit to a character 
 
        Dim charnum As Integer
 
        For charnum = 0 To hexstring.length - 1
 
            Dim hexcharacter = hexstring.Substring(charnum, 1)
            ' get the numeric value of each character
            Dim numvalue As Integer = convert.toint32(hexcharacter, 16)
            ' write the character to the string
 
            outputstring.append(chr(numvalue))
 
        Next charnum
 
        Return Outputstring.tostring
 
    End Function
End Class

Open in new window

0
ChloesDadCommented:
sorry, in line 22 is very wrong. Toint16 is Ok, but toInt32 would be better depending on the maximum size of number you expect to be entered.

dim HexString as string = convert.toint16(mynumber).tostring("X")

note the definition as a string and the change in bracket position, I should remember to copy and paster rather than type lines out again .....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.