Byte Array to send over socket

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cainnech
    New Member
    • Nov 2007
    • 132

    Byte Array to send over socket

    Hello everybody,

    I'm trying to communicate with a device using a winsocket.
    I want to try and change it's IPaddress so I need to follow a specific sequence.

    So I need to start with a DLE + STX, then the IP-number, the subnet, the gateway, the port and if DHCP should be enabled or not and finally another DLE + ETX.

    I'm creating a byte array to send all of the info but there are two questions that I have doing this.

    #1 How do I send the control characters (DLE / STX / ETX)?
    #2 How do I split up the port in multiple bytes?

    Below is my code:
    Code:
    ' First extract all data
    Dim Bites(16) As Byte '9-byte array
    Dim IP() As String
    Dim Subnet() As String
    Dim Gateway() As String
    Dim Port As String
    
    ' IPAddress
        IP = Split(txtModuleIp.Text, ".")
        
    ' Subnet
        Subnet = Split(txtModuleSubnet.Text, ".")
        
    ' Gateway
        Gateway = Split(txtModuleGateway.Text, ".")
        
    ' Port
        Port = txtModulePort.Text
        
        
        
    ' Create Byte array
        Bites(0) = DLE
        Bites(1) = STX
        Bites(2) = IP(0) '192
        Bites(3) = IP(1) '168
        Bites(4) = IP(2) '1
        Bites(5) = IP(3) '11
        Bites(6) = Subnet(0) '255
        Bites(7) = Subnet(1) '255
        Bites(8) = Subnet(2) '255
        Bites(9) = Subnet(3) '0
        Bites(10) = Gateway(0) '192
        Bites(11) = Gateway(1) '168
        Bites(12) = Gateway(2) '1
        Bites(13) = Gateway(3) '1
        Bites(14) = Port '50505
        Bites(15) = Dhcp '0 for disabled / 1 for enabled
        Bites(16) = DLE
        Bites(17) = ETX
    
        If Sock.State = sckConnected Then
            Sock.SendData Bites
            lblStatus.Caption = "  Sending message"
        End If
    When I execute this code I'm receiving the all of the numeric info without a problem, with the exception of the port because it's too long.
    The control characters are being displayed as 0.

    So I'm guessing that there is a conflict in the way I'm trying to send these values.
    Ideally I would like to send it as hex but my bytearray doesn't seem to allow me.

    If anybody has some ideas, it would be very helpful.

    Thanks,
    Kenneth
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    DLE etc are just names for ASCII values. Look up the codes you need in any ASCII reference table (here, for example). Since you're talking about a byte array, you can just plug in the values directly. For example ETX would be (if I remember my syntax correctly) &H03.

    Personally, I'd create a bunch of named constants such as Public Const ETX As Byte = &H03 - then you can go ahead and use the names in your code.

    Sorry, not sure about what's required for part 2 of your question.

    Comment

    Working...