Solved

CSharp problem sending ethernet data

Posted on 2010-08-31
10
367 Views
Last Modified: 2012-05-10
Hi,
My Csharp is not very good, but could someone help with the following problem.
The code fragment does work, but each time 'binwriter.write' is called, it forms a new TCP message and since this is wrapped in a loop, then it is flooding the receiver..

How can I use a single call to 'binwriter.write'  with all the data preloaded in the reg_write_message.


Message reg_write_message = new Message();
// Populate the message
reg_write_message.Message_Id = Global.UPLOAD_CODE;
reg_write_message.Length = sendlength;

sendaddress = (send_loops * sendlength);

using (BinaryWriter binWriter =
 new BinaryWriter(new NetworkStream(Global.m_socClient)))
 {

  [b]// this works but sends 5 seperate Ethernet Messages[/b]
  binWriter.Write(reg_write_message.Header);
  binWriter.Write(reg_write_message.Message_Id);
  binWriter.Write(reg_write_message.Length);
  binWriter.Write(reg_write_message.data);
  binWriter.Write(reg_write_message.Footer);

  [b]// this is what I want to do ( just sending a single ethernet message[/b]
  binWriter.Write(reg_write_message);
  // but get the following 2 error messages against the above line

Error	15	The best overloaded method match for 'System.IO.BinaryWriter.Write(bool)' has some invalid arguments	
Error	16	Argument '1': cannot convert from 'SentryClient.Message' to 'bool'

 }

	

public class Message
    {
        int header;
        int message_id;
        int length;
        byte[] data = new byte[Global.MAX_MESSAGE_SIZE]; // NOTE Just a placeholder, actual length is variable MAX_MESSAGE_SIZE]
        int footer;

        public int Header
        {
            get { return header; }
            set { header = value; }
        }

        public int Message_Id
        {
            get { return message_id; }
            set { message_id = value; }
        }

        public int Length
        {
            get { return length; }
            set { length = value; }
        }

        public byte[] Data
        {
            get { return data; }
            set { data = value; }
        }

        public int Footer
        {
            get { return footer; }
            set { footer = value; }
        }

        public Message()
        {
            // Create default message.
            header = 0x7ff7ffff;
            message_id = 0;
            length = 4;
            data[0] = 0xb;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[1] = 0xe;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[2] = 0xe;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[3] = 0xf;            // Note that this is just a placeholder, actual length is variable [0..1024]
            footer = Global.FTR_WRD;
        }
    }

Open in new window


Many Thanks
0
Comment
Question by:supertramp4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 7

Expert Comment

by:badbearontour
ID: 33565904
Hello,

the BinaryWriter object does not have Write method that accepts an object as a parameter.

the easiest way around this is to serialise your  Message object using xmlSerialiser, into a string format and use the BinaryWriter.Write(xmlSerialisedObejctString).

BB
0
 
LVL 7

Expert Comment

by:badbearontour
ID: 33565909
ah yeah, forgot to mention that you will need to deserialise it at the other end to get your object back

Bb
0
 

Author Comment

by:supertramp4
ID: 33565944
Sorry, the receiver ( which is a hardware device) can only accept raw data, so deserialising will not be suitable.
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 7

Expert Comment

by:badbearontour
ID: 33565987
hmmm... in that case then can the device handle a comma delimited string?

if you concatenate the obejct attributes into a single string?

if not then what does the unit accept and handle?

BB
0
 
LVL 3

Expert Comment

by:with
ID: 33566007
It sounds like it only handles bytes in that particular sequence.

If that's the case, use the Write(byte[]) overload of the BinaryWriter.  You'll need a new byte[] array for the purposes of transmittal.  This array will contain everything (header, data, footer etc) in whatever order the hardware is expecting to receive it.

The transmittal byte array will need to be a new array that is the length of Data[] + 16 more bytes (since each of the 4 integers, Header, Message_Id, Length and Footer will be included).  Basically you would send the whole message exactly as you're sending just the "Data" part now, but with the other integers tacked onto the byte array in whatever order is appropriate.

Check out the System.Converter class for converting int32 into byte[].
0
 

Author Comment

by:supertramp4
ID: 33566064
'badbearontour'. you may be trying to overcomplicate the problem.  'with' is on the right lines

Let say that

            reg_write_message.header = 0x7ff7ffff;
            reg_write_message.message_id = 0x01020304;
            length = 0x00000004;
            data[0] = 0xb;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[1] = 0xe;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[2] = 0xe;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[3] = 0xf;

rather than sending 5 seperate messages, i want to send a single message where the data is concatonated like :
... fffff77f04030201040000000b0e0e0f....

Could you show me a core fragment please
0
 
LVL 9

Expert Comment

by:puru1981
ID: 33566080
you can mark the class serializable and then you can use binaryformatter to serialize the data and send it across
0
 

Author Comment

by:supertramp4
ID: 33566082
Sorry, but swift with the paste. should read

            reg_write_message.header = 0x7ff7ffff;
            reg_write_message.message_id = 0x01020304;
            reg_write_message.length = 0x00000004;
            reg_write_message.data[0] = 0xb;            
            reg_write_message.data[1] = 0xe;            
            reg_write_message.data[2] = 0xe;            
            reg_write_message.data[3] = 0xf;

rather than sending 5 seperate messages, i want to send a single message where the data is concatonated like :
... fffff77f04030201040000000b0e0e0f....
0
 
LVL 3

Accepted Solution

by:
with earned 125 total points
ID: 33566253
Here's a sketch of theoretical method you could add to the Message class (code attached)

After which, you should be able to use binWriter thusly:

binWriter.Write(reg_write_message.Serialize());

Without the hardware to debug against I can't guarantee that'll work perfectly as-is, but that should get you going in the right direction I think.
public byte[] Serialize()
{
	byte[] wholemessage = new byte[this.Data.Length + 16];

	Array.Copy(System.BitConverter.GetBytes(this.header), 0, wholemessage, 0, 4); 
	Array.Copy(System.BitConverter.GetBytes(this.message_id), 0, wholemessage, 4, 4);
	Array.Copy(System.BitConverter.GetBytes(this.length), 0, wholemessage, 8, 4); 
	Array.Copy(this.data, 0, wholemessage, 12, this.data.Length);
	Array.Copy(System.BitConverter.GetBytes(this.footer), 0, wholemessage, 12 + this.data.Length, 4);

	return wholemessage;
}

Open in new window

0
 

Author Closing Comment

by:supertramp4
ID: 33566521
'With' - Thanks this gave me the framework that I needed.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question