Solved

CSharp problem sending ethernet data

Posted on 2010-08-31
10
358 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now