473,386 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Byte Array to send over socket

132 100+
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:
Expand|Select|Wrap|Line Numbers
  1. ' First extract all data
  2. Dim Bites(16) As Byte '9-byte array
  3. Dim IP() As String
  4. Dim Subnet() As String
  5. Dim Gateway() As String
  6. Dim Port As String
  7.  
  8. ' IPAddress
  9.     IP = Split(txtModuleIp.Text, ".")
  10.  
  11. ' Subnet
  12.     Subnet = Split(txtModuleSubnet.Text, ".")
  13.  
  14. ' Gateway
  15.     Gateway = Split(txtModuleGateway.Text, ".")
  16.  
  17. ' Port
  18.     Port = txtModulePort.Text
  19.  
  20.  
  21.  
  22. ' Create Byte array
  23.     Bites(0) = DLE
  24.     Bites(1) = STX
  25.     Bites(2) = IP(0) '192
  26.     Bites(3) = IP(1) '168
  27.     Bites(4) = IP(2) '1
  28.     Bites(5) = IP(3) '11
  29.     Bites(6) = Subnet(0) '255
  30.     Bites(7) = Subnet(1) '255
  31.     Bites(8) = Subnet(2) '255
  32.     Bites(9) = Subnet(3) '0
  33.     Bites(10) = Gateway(0) '192
  34.     Bites(11) = Gateway(1) '168
  35.     Bites(12) = Gateway(2) '1
  36.     Bites(13) = Gateway(3) '1
  37.     Bites(14) = Port '50505
  38.     Bites(15) = Dhcp '0 for disabled / 1 for enabled
  39.     Bites(16) = DLE
  40.     Bites(17) = ETX
  41.  
  42.     If Sock.State = sckConnected Then
  43.         Sock.SendData Bites
  44.         lblStatus.Caption = "  Sending message"
  45.     End If
  46.  
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
Feb 9 '13 #1
1 3819
Killer42
8,435 Expert 8TB
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.
Feb 20 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sathyaish | last post by:
I am using MCI (winmm.dll) to read, record and playback sound. For now, I am doing this with disk files instead of realtime doing it straight from the memory. If I want to stream/relay/transmit...
6
by: Gator | last post by:
Hi All, Basically my situation is this, I have a server implemented in C++, unmanaged code, using proprietery protocol over TCP/IP to communicate with the cilent(c++ also). Now, I am implementing...
7
by: Prabhu | last post by:
Hi, I have to send a structure through TCPClient socket. we can send only byte array through the socket, So please any one can help me by telling How to convert a struct object into an byte...
6
by: Ricardo Quintanilla | last post by:
i have a code that sends data to a socket listening over as400 platform, the socket responds to me as a "byte array". then i need to convert the "byte array" into a string. the problem is that...
2
by: Shreddy | last post by:
Hi, I'm trying (or struggling) to convert some C code to C#. The existing C client is sending a structure via a TCP socket to a network server. The structure contains a mix of int and char...
4
by: TomHL | last post by:
Hello, I want to send an additional info with byte array wiith sockets as "one packet". I know how to send the byte array by himself, but how can I send the additional info with it at the same...
3
by: Fireangel | last post by:
I want to cast a class into a byte array. I've seen some examples of this floating around, but they all have simple data members. What happens if I cast something that has a ArrayList or an...
1
by: sridev | last post by:
hi all, i have a byte array which has length of 10,000.I have to break this byte array into smaller parts of length 512 and save this values into another byte array. I am doing this because...
3
by: Peted | last post by:
Is it possible to concatanate a string and a byte array, into a "string" variable, send it as a string to an ip socket device and have the bytes, seen as a sequence of bytes, not char or string ? ...
3
by: tash.robinson | last post by:
I have been pulling my hair out with this and have searched and found some solutions but I can't seem to get them to work as expected. I have a structure, which I have declared explicitly using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.