1 Replies - 9615 Views - Last Post: 20 January 2010 - 04:26 AM Rate Topic: -----

#1 p55xp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-December 09

Parsing Intel hex file through Serial Port

Posted 20 January 2010 - 12:35 AM

I am new to VB .NET, now studing VB2008.
Being an 8051 microcontroller enthusiast my dream is to program an at89c51 with my own device. This requires one GUI that can communicate with the device programmer via serial port. I have found the post "Serial Port Communication in VB.Net" by PsychoCoder at - www.dreamincode.net/forums/showtopic37361.htm - Which is very helpful.
I have already built an interface in VB6 that can open and save .hex type text files which is in Intel Hex Format. I trying to do the same with VB .NET. Upto this everything seems alright.
But the strings in intel hex file is like -:100D3400E532B40D0C7BFF7A05798E120BCD120DC2.
I do not under stand how to send this hex bytes to serial port as this is in reality is a text file and their ascii values represents a different value other than they looks.
Please Guide
Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: Parsing Intel hex file through Serial Port

#2 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Parsing Intel hex file through Serial Port

Posted 20 January 2010 - 04:26 AM

You have a string, say fileText, such as: "100D3400E532B40D0C7BFF7A05798E120BCD120DC2"

And you want the corresponding Byte array, probably
{ &H10, &H0D, &H34, &H00, &HE5, &H32, &HB4, &H0D, &H0C, &H7B, &HFF,
&H7A, &H05, &H79, &H8E, &H12, &H0B, &HCD, &H12, &H0D, &HC2 }

You can then send the bytes using the overload of SerialPort.Write that takes a byte array.

To convert, you have to run through the String and convert each pair of chars into their byte value.

1) create an array of the correct size:
- We know that the number of bytes will be half the length of the String.
- You Dimension arrays by specifying the upper bound, not the size (if you got this wrong you would get an extra byte 0 at the end!):
Dim bytes((fileText.Length \ 2) - 1) As Byte

2) Run through the string 2 chars at a time:

For index As Integer = 0 to fileText.Length - 2 Step 2

2.1) Read two chars from the string at position index

Dim hexText As String = fileText.SubString(index, 2)

2.2) Convert the hexText into a byte value:

Dim value As String = Convert.ToByte(hexText, 16)
- Here 16 specifies the base, you can also use 2 and 8.

2.3) Write the value into the array at the correct index

bytes(index \ 2) = value

3) Next
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1