send hex data to Ethernet or LAN using Visual basic 6.0-VBForums
Results 1 to 18 of 18

Thread: send hex data to Ethernet or LAN using Visual basic 6.0

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    send hex data to Ethernet or LAN using Visual basic 6.0

    I wanted to send hex data to Ethernet or LAN using Visual basic 6.0.the hex data will be like AB7745683ACB76B34E5FF5E99EBC5F878A6BC8E9DF9876BCEA89

    On the form i have a text box in which user will input this data .

    How can i send this hex data to LAN or Ethernet using vb6.0

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    6,880

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    It will not be HEX data if enter into a textbox as shown. It would be a string containing those characters which when read as hex would look quite a lot different.

    As for sending it to the LAN, a Lan does not really process data it just allows it to travel from one point to another. The most common means of sending data would be TCP which you can do using the Winsock control but there must be a target that will receive the data.

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Posts
    10,383

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Yeah, not enough useful information to begin answering this.

    While API calls may permit sending IP layer traffic there are generally security issues getting access to "raw sockets" and Microsoft blocked their use in XP with a security patch circa 2004. I don't think there is any API support for mucking below that (Ethernet MAC layer) and only a device driver would have such abilities. There are even fewer non-malware reasons to do this than raw IP.

    From the question I'm not even sure how we should interpret "LAN" which might be talking about LAN Manager (MS Networking) services such as file and printer sharing and IPC.


    But mostly likely this is a very naively expressed question asking about sending binary data entered as hexadecimal text over a TCP connection. This would really be two separate questions: hex text to binary data, and sending binary data over TCP (or possibly UDP).

  4. #4
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    See my signature, specifically Binary Server(or buddy paint, perhaps) and HexToAsc.

  5. #5

    Thread Starter
    Registered User
    Join Date
    Jan 2013
    Posts
    2

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Quote Originally Posted by DataMiser View Post
    It will not be HEX data if enter into a textbox as shown. It would be a string containing those characters which when read as hex would look quite a lot different.

    As for sending it to the LAN, a Lan does not really process data it just allows it to travel from one point to another. The most common means of sending data would be TCP which you can do using the Winsock control but there must be a target that will receive the data.

    yes we have a target that will recieve the data.

    and yes the data in the text box will be a string containg the hex data.

    and yes winsock can be used.

    but i m new to vb and tcp programming so i dont know how to use winsock and send that hex data in the text box. plz help.

  6. #6
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,257

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Since the data is going to be entered by a User into a TextBox you'll need to perform some validation on it before convering to binary values which can then be sent.
    Code:
    Private Sub Command1_Click()
    Dim boInvalid As Boolean
    Dim intI As Integer
    Dim intJ As Integer
    Dim strHex As String
    Dim bytToSend() As Byte
    '
    ' Must be an even number of characters entered
    '
    If Len(txtdata.Text) Mod 2 = 0 Then
        ReDim bytToSend((Len(txtdata.Text) \ 2) - 1)
        intI = 1
        Do
            strHex = UCase(Mid$(txtdata.Text, intI, 2))
            '
            ' Each character must be A thru F or 0 thru 9
            '
            For intJ = 1 To 2
                Select Case Mid$(strHex, intJ, 1)
                    Case "A" To "F", "0" To "9"
                    Case Else
                        boInvalid = True
                End Select
            Next intJ
            '
            ' If it's a valid pair of characters
            ' convert the hexadecimal representation to a binary value
            ' and store in the next element of the array
            ' If it's invalid, tell the user and stop processing
            '
            If Not boInvalid Then
                bytToSend(intI \ 2) = Val("&H" & Mid$(txtdata.Text, intI, 2))
                intI = intI + 2
            Else
                MsgBox "Invalid Hexadecimal Value: " & strHex
            End If
        Loop Until intI > Len(txtdata.Text) Or boInvalid
        '
        ' If it was valid hexadeciomal data then send it
        '
        If Not boInvalid Then
            Winsock1.SendData bytToSend
        End If
    Else
        MsgBox "Invalid Data - Hexadecimal data must be an even number of characters"
    End If
    End Sub
    Of course, this assumes that you've already made the connection to the other end.

  7. #7
    PowerPoster
    Join Date
    Jan 2008
    Posts
    8,492

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Can you not just allow 0 - 9, A - F (upper and/or lower) in the text box and calculate on every two-characters entered?
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved

    DISCLAIMER

    Anything I post is an example for demonstration purposes only and is not intended to be the solution to your request nor do I claim that it is. If you find that it is useful then it is entirely up to you to make whatever necessary changes you feel are adequate for your purposes.

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,257

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Yes, you can, but you'd need to look at the TextBox Change event to trap Paste operations (my guess is that the user will probably be pasting the data from somewhere rather than typing it all in - not many users speak fluent Hexadecimal). You might have to use ReDim Preserve for the Byte Array (or use a Collection) if you do it that way, as you don't know the total size of the Input in the TextBox events. I'd go with the simpler 'batch' orientated approach and do the validation when the user clicks the 'Go do it' button.

    At the end of the day it's a matter of style and how much code you want to write.

    As a side issue, I'm not fond of the type of 'If some_condition Then do nothing, Else do something, EndIf' construct, as in the Select Case statements I used. With an If / Then statement it's easy to invert the logic to: 'If Not (some_condition) Then do something, EndIf'. I'm struggling to find a simple way of inverting the logic of the Case Statements
    i.e.
    instead of
    Code:
    Case "A" To "F", "0" To "9"
    Case Else
        boInvalid = True
    something like:
    Code:
    Case Not ("A" To "F", "0" To "9")
        boInvalid = True
    which obviously doesn't work, and I can't find a combination of logical operators or Select Case that will work. 'Not', in this context, seems to require some brackets and Not("A") will not cast to a boolean so there's a Type Mismatch. There must be a way.........perhaps I can't see the Woods for the Trees.

    I suppose
    Code:
    Case Chr(0) to Chr(Asc("0") - 1), Chr(Asc("9") + 1) To Chr(Asc("A") - 1), Chr(Asc("F") + 1) To Chr(255)
        boInvalid = True
    might work but even if it does, it doesn't look very clear.

  9. #9
    PowerPoster
    Join Date
    Jan 2008
    Posts
    8,492

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Well the 'not very clear' is only for the programmer(s) and not the users. If the programmer(s) is/are programmers then I wouldn't worry about not very clear in this case as it looks clear to me and I'm not an expert programmer
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved

    DISCLAIMER

    Anything I post is an example for demonstration purposes only and is not intended to be the solution to your request nor do I claim that it is. If you find that it is useful then it is entirely up to you to make whatever necessary changes you feel are adequate for your purposes.

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,257

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    That might be the case, but in loads of years programming I've learnt that there's always an 'elegant' way of doing things

  11. #11
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,130

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Code:
    Select Case True
        Case Mid$(strHex, intJ, 1&) Like "[!0-9A-F]"
            boInvalid = True
    End Select
    Last edited by Bonnie West; Jan 9th, 2013 at 02:29 PM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0

    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  12. #12
    PowerPoster
    Join Date
    Jan 2008
    Posts
    8,492

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    OK, that seems to work but I don't understand the reason you hav "[" "!" and "]" in the test string.

    Also, how do you prevent any character from being entered into a textbox?
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved

    DISCLAIMER

    Anything I post is an example for demonstration purposes only and is not intended to be the solution to your request nor do I claim that it is. If you find that it is useful then it is entirely up to you to make whatever necessary changes you feel are adequate for your purposes.

  13. #13
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,130

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Quoted from the Like Operator:

    A group of one or more characters (charlist) enclosed in brackets ([ ]) can be used to match any single character in string and can include almost any character code, including digits.
    An exclamation point (!) at the beginning of charlist means that a match is made if any character except the characters in charlist is found in string.
    Code:
    Private Sub Text1_GotFocus()
        Text1.Locked = True         'Prevents pasting too
    End Sub
    
    Private Sub Text1_LostFocus()
        Text1.Locked = False
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0

    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  14. #14
    PowerPoster
    Join Date
    Jan 2008
    Posts
    8,492

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    That wasn't what I meant, Bonnie. I meant how to you prevent any character from being entered into a textbox as the user types in characters. Example: how to prevent user from typing in anything except numbers. I know your code example detects the characters but it doesn't stop the character from being entered. I need a way to allow only certain characters to be entered and if user types in an invalid character I want the code to simply prevent that character from being entered even if the user goes back and tries to insert it in the middle of the text.
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved

    DISCLAIMER

    Anything I post is an example for demonstration purposes only and is not intended to be the solution to your request nor do I claim that it is. If you find that it is useful then it is entirely up to you to make whatever necessary changes you feel are adequate for your purposes.

  15. #15
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    3,130

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Code:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case vbKey0 To vbKey9, vbKeyBack
            Case Else:  KeyAscii = 0
        End Select
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Select Case True
            Case Chr$(KeyAscii) Like "[!0-9]"
                KeyAscii = 0
        End Select
    End Sub
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0

    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  16. #16
    PowerPoster
    Join Date
    Jan 2008
    Posts
    8,492

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    OK, that's it.

    So, in response to Doogle's reply then why wouldn't this work instead of all that code?

    Code:
    Private Sub cmdSendHexString_Click()
     If Len(Text1.Text) Mod 2 <> 0 Then
       MsgBox "Must be even number of hex codes"
       Text1.SetFocus
     Else
       '
       ' Code to send hex string or convert to actual hex
       '  
     End If
    End Sub
    
    Private Sub Text1_KeyPress(KeyAscii As Integer)
     Select Case True
       Case Chr$(KeyAscii) Like "[!0123456789AaBbCcDdEeFf]"
         KeyAscii = 0
     End Select
    Exit Sub
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved

    DISCLAIMER

    Anything I post is an example for demonstration purposes only and is not intended to be the solution to your request nor do I claim that it is. If you find that it is useful then it is entirely up to you to make whatever necessary changes you feel are adequate for your purposes.

  17. #17
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,257

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    As I mentioned earlier, what if I copy and paste into the TextBox ? The KeyPress event doesn't get triggered.
    EDIT: and in the real world you'd have to cater for Backspace and Delete
    Last edited by Doogle; Jan 9th, 2013 at 05:26 PM.

  18. #18
    PowerPoster
    Join Date
    Jan 2008
    Posts
    8,492

    Re: send hex data to Ethernet or LAN using Visual basic 6.0

    Oh, yeah, I forgot about you mentioning Copy/Paste. Oh, well, another plan foiled
    The better the information you give to begin with and the sooner you reply the sooner you will get help and get your problem resolved

    DISCLAIMER

    Anything I post is an example for demonstration purposes only and is not intended to be the solution to your request nor do I claim that it is. If you find that it is useful then it is entirely up to you to make whatever necessary changes you feel are adequate for your purposes.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.