Hi Group,
I am trying without much luck to pack messages into a byte buffer
using get and set accessors.
I have attached a simplified example of what I am trying to do using
byte variables.
(My real data is not byte variables.)
I wish to be able to populate a field in the message header and then
include this in a
init message, and finally once all the message is transfered to the
byte buffer populate
the message header length.
However I alway get zero for my message header length when I write to
the console.
I am confused I think with the get and set accessor.
Could anyone suggest where I have gone wrong.
Mark
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1
{
class Program
{
static void Main(string[] args)
{
msgHdr msgHeader = new msgHdr();
// Don't set message length here.
msgHeader.messa geId = 100;
initMessage theInitrequest = new initMessage();
theInitrequest. messageHeader = msgHeader;
theInitrequest. version = 200;
//
// Hopefully here
// Initrequest.mes sageHeader.mess ageLength = 3
// Initrequest.mes sageHeader.mess ageId = 100
// Initrequest.ver sion = 200;
Console.WriteLi ne(theInitreque st.messageHeade r.messageLength .ToString());
Console.WriteLi ne(theInitreque st.messageHeade r.messageId.ToS tring());
Console.WriteLi ne(theInitreque st.version.ToSt ring());
// and
// Initrequest.Dat a [0] = 3
// Initrequest.Dat a [1] = 100;
// Initrequest.Dat a [2] = 200;
Console.WriteLi ne(theInitreque st.Data[0].ToString());
Console.WriteLi ne(theInitreque st.Data[1].ToString());
Console.WriteLi ne(theInitreque st.Data[2].ToString());
}
}
public class Utility
{
public static void Pack(byte src, ref byte[] Data, ref uint
Index)
{
Data[Index++] = src;
}
public static void Unpack(ref byte dst, byte[] Data, ref uint
Index)
{
dst = Data[Index++];
}
}
public class initMessage
{
public msgHdr messageHeader;
public byte version;
private byte[] _internalBuffer ;
public byte[] Data
{
set
{
uint index = 0;
Array.Copy(_int ernalBuffer, index, messageHeader.D ata,
0, messageHeader.D ata.Length);
index = index +
System.Convert. ToByte(messageH eader.Data.Leng th);
Utility.Unpack( ref version, value, ref index);
}
get
{
uint index = 0;
_internalBuffer = new byte[20];
Array.Copy(mess ageHeader.Data, 0, _internalBuffer ,
index, messageHeader.D ata.Length);
index = index +
System.Convert. ToByte(messageH eader.Data.Leng th);
Utility.Pack(ve rsion, ref _internalBuffer , ref index);
messageHeader.m essageLength =
System.Convert. ToByte(index);
return _internalBuffer ;
}
}
}
public class msgHdr
{
public byte messageLength;
public byte messageId;
private byte[] _internalBuffer = new byte[2];
public byte[] Data
{
set
{
uint index = 0;
Utility.Unpack( ref messageLength, value, ref index);
Utility.Unpack( ref messageId, value, ref index);
}
get
{
uint index = 0;
Utility.Pack(me ssageLength, ref _internalBuffer , ref
index);
Utility.Pack(me ssageId, ref _internalBuffer , ref
index);
return _internalBuffer ;
}
}
}
} 0 1451 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Leandro Pardini |
last post by:
Hello there,
I'm trying to process a binary file and I really don't know how. The
story: gPhoto2 downloads the images from my camera just fine, but
small areas of 10x3 pixels are screwed up. I found exactly where the
problem happens and wrote a simple interpolation filter that works
like a charm... under DOS, using the old QuickBasic. I've used Perl
for a while, but never to handle binary data, so I'm at a loss; and I
really want to get...
|
by: Steve |
last post by:
Please take a look at the simple code segment below and advise me what is
wrong.
According to the help and examples I've seen it should work unless I
misunderstand
something.
The problem is that UdpClient.Receive method always throws following
exception, even though
I verified that message was successfuly received by devices and responses
were sent back:
|
by: Gajendra |
last post by:
How does the byte packing and the byte alignment work in VC++ compiler? What is the effect of #pragma pack(n) on the alignment and byte packing
for example while using the structur
struc
double a
char b
char c
}
in a 8 byte packing the sizeof structure is 16. Could anyone explain.
|
by: Dennis |
last post by:
I was trying to determine the fastest way to build a byte array from
components where the size of the individual components varied depending on
the user's input. I tried three classes I built: (1) using redim arrays to
add to a normal byte array (2) using an ArrayList and finally (3) using a
memorystream. These three classes are listed below the test sub called
"TestBuildByteArray". It was interesting that using the memorystream was...
|
by: D. Yates |
last post by:
All,
Is there a faster way to do this in C#:
byte buffer;
buffer = new byte;
for(int i = 0; i < buffer.Length; i++)
buffer = 0;
| |
by: Cichy |
last post by:
Hello,
I'm writing a Client-Server application using sockets (asynchronous).
There is a Server (Master) which accepts incoming connections, and
Client (Slave).
Afetr establishing connections with all Slaves I wanna hit a button
"Automatic", then everything must be reorganised, there is an
ellection for a new Master.
Everything is all right when I'm connecting manually (when I hit a
button connect), but after hitting this button...
|
by: JonJacobs |
last post by:
When I add a series of byte arrays to an array list, then I read them back, all the arraylist byte array elements are identical to the last byte array entry. What is wrong? The following code will reproduce the problem.
Thanks,
Jon Jacobs
Imports System.Text
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
|
by: cmrhema |
last post by:
Hi,
We have developed a socket program as below, The program works fine, except that it sometimes gives me jumbled messages or same message gets repeated twice.
The whole code is produced below.
I will let you know what exactly are we doing.
This a socket program for tracking vehicle.
The vehicle sends the signal through gprs+gsm mode, to our port.
|
by: Scott Townsend |
last post by:
So I need to talk to a devices that expects all of the bits and bytes I sent
it to be in specific places (not yet 100% defined).
I wanted to create a structure/class with all of the data in it and then
convert that to a byte array, pass it to the device, then get a reply and
then convert that to a structure.
I'm having issues with making sure what I've coded is correct. Cant figure
out how to define an array in structure that is a...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |