Solved

CSharp problem sending ethernet data

Posted on 2010-08-31
10
366 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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