Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CSharp problem sending ethernet data

Posted on 2010-08-31
10
Medium Priority
?
370 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 500 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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