Results 1 to 9 of 9

Thread: [RESOLVED] VB 2010 - It's possible translate HEX text into a binary file?

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    [RESOLVED] VB 2010 - It's possible translate HEX text into a binary file?

    Hi friends! I hope you're all well.

    I discovered a wonderful project that dump the Atari 2600 ROMs from a cartridge, using the Arduino Uno.

    Summarizing the process, Arduino extracts several lines of hexadecimal data text, between the "Start" and "End" tags in Serial Monitor. After the extraction, you can copy and paste these lines.

    I'm creating a program in VB 2010, which simulates an Arduino serial monitor in a textbox. The Arduino dump process, appears in this textbox.


    The author of the project, made available a script in Ruby that does this process. And the script, works as follow: It looks for the START/END tags, verifies that each line contains 16 bytes, each address increments by 16, and writes those bytes into a binary file.

    And I want to create an converter process in VB.Net like the Ruby script. The user clicks a button and this process happens.

    Is it possible to create this process in VB.NET?
    if so, how could I do it?


    This is the code of my project, but, without the function of converting the text of the textbox into a binary file, because I do not know how to do it.

    Code:
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
            Try
    
                For Each port As String In SerialPort.GetPortNames()
                    comboxCOMport.Items.Add(port)
                Next
    
                comboxCOMport.SelectedIndex = 0
    
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    
    
    
    
        End Sub
    
    
    
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            SerialPort1.BaudRate = "9600"
            SerialPort1.PortName = comboxCOMport.SelectedItem
            Try
                SerialPort1.Open()
                MsgBox("Connected! now, go to the Dump button!", MsgBoxStyle.Information)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub
    
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            txtboxserialmonitor.Text = SerialPort1.ReadExisting()
    
        End Sub

    Thank you in advance.
    Last edited by vbnewbieuser; Jul 3rd, 2019 at 10:25 AM. Reason: errors

  2. #2
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: VB 2010 - It's possible translate HEX text into a binary file?

    Yes this is possible but for you to get help we need a little more information. You say the data is framed with "start" and "end" "tags", can you show an example of how the arduino transmits this 16 bytes of data with the start and end tags, that way we can design a receiving routine. Once the receiving is satisfactory you can start to think about writing the data to a file.


    P.S. this is not a good line to include in your code

    Code:
    CheckForIllegalCrossThreadCalls = False

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB 2010 - It's possible translate HEX text into a binary file?

    [QUOTE=Mc_VB;5400149]Yes this is possible but for you to get help we need a little more information. You say the data is framed with "start" and "end" "tags", can you show an example of how the arduino transmits this 16 bytes of data with the start and end tags, that way we can design a receiving routine. Once the receiving is satisfactory you can start to think about writing the data to a file.


    P.S. this is not a good line to include in your code

    Code:
    CheckForIllegalCrossThreadCalls = False
    [/QUOTE


    Hi, Sir Mc_Vb! yes, I can show. The data shown on the Arduino serial monitor is as follows:

    Code:
    START
    40   A9034CD6FAA5822920F00CA5EF2907F0
    10  0320C1FD20F0FEA9288588A5EBC933D0
    20  05A9014CD6FAA000C8D9C1FEB0FAB9C0
    30 DDB0F4E69CA59C20D5FD859C2085F985
    40 E2A59C18690C858DA58B186934858FA5
    END

    And I will remove the "crossthreadcalls", thank you!

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: VB 2010 - It's possible translate HEX text into a binary file?

    As a minimum, once you remove the crossthreadcalls override, you will have to update the textbox correctly, not directly from a separate thread (which the serial receiver event is on a secondary thread).

    Perhaps (the minimum) change would be
    Code:
        Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim inputBuf as String = SerialPort1.ReadExisting()
    
            Me.Invoke( Sub() txtboxserialmonitor.Text = inputBuf)
    
        End Sub
    I would guess though, if you're going to a ROM there will be a lot more data to be read, and your code (and the modification), would get the serial data in multiple transfers, so the above code would keep clobbering what is in the textbox with each transfer.
    You should have a more robust receiver which would accumlate each inputBuf read.

    Again, as a minimum, change the invoke line to
    Code:
        Me.Invoke(Sub() txtboxserialmonitor.AppendText(inputBuf))
    [QUOTE=vbnewbieuser;5400153]
    Quote Originally Posted by Mc_VB View Post
    ...
    Code:
    START
    40   A9034CD6FAA5822920F00CA5EF2907F0
    10  0320C1FD20F0FEA9288588A5EBC933D0
    20  05A9014CD6FAA000C8D9C1FEB0FAB9C0
    30 DDB0F4E69CA59C20D5FD859C2085F985
    40 E2A59C18690C858DA58B186934858FA5
    END
    ...
    So, the first line to the second line (40 to 10), violates the incrementing by 16 rule, so do you ignore the 40 line, or report an error and do nothing?
    Last edited by passel; Jul 2nd, 2019 at 09:55 PM.

  5. #5
    Addicted Member
    Join Date
    Nov 2011
    Posts
    229

    Re: VB 2010 - It's possible translate HEX text into a binary file?

    That really does not show how the arduino is sending the data. What you can try is the following code snippet to see if it acts like your "serial monitor".

    Before you try this change your arduino code so that every Serial.print statement is changed to Serial.println and then copy and paste a screenshot if you get results.

    The following is simply a windows form application with a rich text box control. Don't forget to set the portname to your port.

    Code:
    Imports System.IO.Ports
    
    Public Class Form1
    
        Public Delegate Sub myDelegate(ByVal sData As String)
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            With SerialPort1
                .BaudRate = "9600"
                .PortName = "COM7" 'Change this line to your own com port
                .NewLine = vbCrLf
                .DtrEnable = True
                .Open()
            End With
        End Sub
    
    
        Private Sub Text_Out(ByVal sData As String)
            RichTextBox1.AppendText(sData & vbCr)
        End Sub
    
        Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim sData As String
            SerialPort1.ReadTimeout = 20
    
            Do While SerialPort1.BytesToRead > 0
                Try
                    sData = SerialPort1.ReadLine
                    Me.BeginInvoke((New myDelegate(AddressOf Text_Out)), sData)
    
                Catch ex As Exception
    
                End Try
            Loop
        End Sub
    End Class

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB 2010 - It's possible translate HEX text into a binary file?

    Quote Originally Posted by passel View Post
    As a minimum, once you remove the crossthreadcalls override, you will have to update the textbox correctly, not directly from a separate thread [...]
    Hi, Sir Passel. I'll follow your suggestion about the Serial Receive Data. I appreciate your help. Thank you!

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB 2010 - It's possible translate HEX text into a binary file?

    Quote Originally Posted by Mc_VB View Post
    That really does not show how the arduino is sending the data. What you can try is the following code snippet to see if it acts like your "serial monitor".

    Before you try this change your arduino code so that every Serial.print statement is changed to Serial.println and then copy and paste a screenshot [...]
    I'll redo my form. I must keep the Combobox for convenience if the COM ports change. But in everything else, I'm going to look at your example code and also follow Sir Passel's suggestion . I thank you very much for your suggestion.

    At this time, as we are from different countries, in my country is more than 12:30 a.m. I need to go to sleep, because I wake up very early. I will not be able to operate the program now. I'll continue tomorrow.

    I have only one question. Let's say I have a set of hexadecimal lines in my Arduino serial monitor:

    Code:
    100000009CC00000B5C00000B3C00000B1C000003B
    10001000AFC00000ADC00000ABC00000A9C0000030
    10002000A7C00000A5C00000A3C0000054C4000089
    1000300018C400009DC000009BC0000099C00000D3
    1000400097C0000095C0000093C0000091C0000060
    100050008FC000008DC000008BC000000EC10000EA
    1000600087C0000085C0000083C0000081C0000080
    100070007FC000007DC000007BC0000079C0000090
    1000800077C0000075C0000073C0000071C00000A0
    Let's suppose that this hex, represents a "Hello World" when translated into binary. When I modify my form according to your suggestion, how could I turn this hex into a binary file like the Ruby Script that I mentioned in the first topic?

    Thank you!

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: VB 2010 - It's possible translate HEX text into a binary file?

    That sounds like a different question, since the script earlier sounded like it did more than just convert hex to bytes and write to a file.

    There are many possible ways to do this, so here is just one, simply based on the apparent last question where you have a textbox with lines of hex that you want converted to bytes and written to a file. This assumes a multiline enabled textbox named "txtboxserialmonitor" contains the lines as you've shown.
    Code:
      Private Function HexStringToByteArray(ByVal shex As String) As Byte()
        Return Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
      End Function
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim fileOutput As New List(Of Byte)
    
        For Each line As String In txtboxserialmonitor.Lines
          fileOutput.AddRange(HexStringToByteArray(line))
        Next
        IO.File.WriteAllBytes("c:\c\tmp.bin", fileOutput.ToArray)
      End Sub

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2017
    Posts
    38

    Re: VB 2010 - It's possible translate HEX text into a binary file?

    Quote Originally Posted by passel View Post
    That sounds like a different question, since the script earlier sounded like it did more than just convert hex to bytes and write to a file.

    There are many possible ways to do this, so here is just one, simply based on the apparent last question where you have a textbox with lines of hex that you want converted to bytes and written to a file. This assumes a multiline enabled textbox named "txtboxserialmonitor" contains the lines as you've shown[...]
    I got permission to use the computer at lunch time.

    Sir Passel, this is awesome! that's exactly what I needed! all hexadecimal text, becomes a binary file in machine language. With this, I will now make this binary file, be inserted into a Listbox. When clicking on this file in the Listbox, this file will execute a Process Start, opening an Atari emulator and opening the ROM. If all goes well, I'll be able to emulate this ROM in the Atari emulator. Because in Ruby Script, after converting the hex data into a binary file, you can emulate this file in the emulator.

    You are awesome! with this Arduino Dump process I found on the internet for Atari cartridges, plus your code, I believe I can do this with other types of EPROMs as long as they have up to 11 address bars and 8 date bits to date 7) and that can be energized with 5V. Using an Arduino Mega, I can have other EPROMs with more address bars. Anyway, I can emulate several ROMS and after that, try to develop more complex processes.

    Case Resolved!

    I wanted to rate your post millions of times! many thanks to you and to the whole forum one more time!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width