B4J Question GRBL 1.1 and G-Code to Hex to USB/COM

skyracer90

Member
Licensed User
Longtime User
Job: Communicate with my Lasercutter....

Hallo, here i Translate "Textstring" to "HexString" and it's work perfect!
Any where any Idea to an shorter Version of my Sub(TranslateTextToHex)?

e.g....
Call's...
0.TranslateTextToHex("G0 X100 Y100 F10","")
1.TranslateTextToHex("G0 X10 Y210 F1000","")
2.TranslateTextToHex("G0 X200 Y210 F10","")
3.TranslateTextToHex("G0 X200 Y200 F1000","")
4.TranslateTextToHex("G0 X200 Y50 F10","")
5.TranslateTextToHex("G0 X10 Y10 F1000","")
6.TranslateTextToHex("G0 X0 Y0 F10","")
7.TranslateTextToHex("G1 X0 Y240 F1000","")

Sub TranslateTextToHex(Text As String,Tail As String)
Log("Show Text : " & Text)

Dim Dummy1 As Int
Dim Dummy2 As Int
Dim Dummy3 As Int
Dim Result As String
Text = Text.Replace(" ","")
Log("Show Text Clear: " & Text)

For I = 0 To Text.Length-1
Dummy1 = (Asc(Text.SubString2(I,I+1))/16)
Dummy1 = Dummy1 * 10
Log("Dummy1 :" & Dummy1)

Dummy2 = Asc(Text.SubString2(I,I+1)) Mod 16
Log("Dummy2 :" & Dummy2)

Dummy3 = Dummy1+Dummy2
Log("Result = " & Dummy3)

Result = Result & Dummy3
Next

'CRLF anfügen
'Add Tail here... Moment Constant
Result = Result & "0D0A"
Log(Result)
astream.Write(bc.HexToBytes(Result))
End Sub

Results:
0. Show Text : G0X100Y100F10
0. RESULT : 473058313030593130304631300D0A
1. Show Text : G0X10Y210F1000
1. RESULT : 47305831305932313046313030300D0A
2. Show Text : G0X200Y210F10
2. RESULT : 473058323030593231304631300D0A
3. Show Text : G0X200Y200F1000
3. RESULT : 4730583230305932303046313030300D0A
4. Show Text : G0X200Y50F10
4. RESULT : 4730583230305935304631300D0A
5. Show Text : G0X10Y10F1000
5. RESULT : 473058313059313046313030300D0A
6. Show Text : G0X0Y0F10
6. RESULT : 4730583059304631300D0A
7. Show Text : G1X0Y240F1000
7. RESULT : 473158305932343046313030300D0A

Thx, Skyracer...
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
B4X:
    'uses byteconverter
    'your data in - without last  ,""
    Dim stringIn() As String = Array As String("G0 X100 Y100 F10", _
    "G0 X10 Y210 F1000", _
    "G0 X200 Y210 F10", _
    "G0 X200 Y200 F1000", _
    "G0 X200 Y50 F10", _
    "G0 X10 Y10 F1000", _
    "G0 X0 Y0 F10", _
    "G1 X0 Y240 F1000")

    ' your results for checking against
    Dim stringOut() As String = Array As String("473058313030593130304631300D0A", _
    "47305831305932313046313030300D0A",    _
    "473058323030593231304631300D0A", _
    "4730583230305932303046313030300D0A", _
    "4730583230305935304631300D0A",    _
    "473058313059313046313030300D0A", _
    "4730583059304631300D0A", _
    "473158305932343046313030300D0A")

    For p = 0 To stringIn.Length-1
        Log(stringIn(p))
        ' the next line does the conversion 
        Log(bc.HexFromBytes(stringIn(p).Replace(" ","").GetBytes("utf8"))&"0D0A")
 
        Log(stringOut(p)) ' your result
        Log(bc.HexFromBytes(stringIn(p).Replace(" ","").GetBytes("utf8"))&"0D0A" = stringOut(p)) ' check against your results
        Log("--------------")
    Next

Or if you want as a Sub
B4X:
...
    For p = 0 To stringIn.Length-1
        Log(stringIn(p))

        Log(convert(stringIn(p)))

        Log(stringOut(p)) ' your result

        Log(convert(stringIn(p)) = stringOut(p)) ' check against your results

        Log("--------------")
    Next
End Sub
Sub convert(a As String) As String
    Return    bc.HexFromBytes(a.Replace(" ","").GetBytes("utf8"))&"0D0A"
End Sub
 
Last edited:
Upvote 0
Top