Click here to Skip to main content
15,887,989 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am now working on the project that need to send data to show on LED display through TCP/IP. The supplier have no SDK for this LED so I am now trying to send data through TcpClient .net library.

Command format to display data on LED provided by my supplier is as follow:
format = 0x01 + "Z" + "00" + 0x02 + "AA" + 0x06 + "10" + "4500" + 0x01 + "MyData"+ 0x04

This is my example code to send data to LED.
C#
private void SendData()
{
    TcpClient l_Client = new TcpClient();
    l_Client.Connect(txtIPAddress.Text, Convert.ToInt32(txtPortNumber.Text));
    NetworkStream l_stream = l_Client.GetStream();
    byte[] data = new byte[]{
        0x01, 
        Encoding.ASCII.GetBytes("Z")[0],
        Encoding.ASCII.GetBytes("0")[0],
        Encoding.ASCII.GetBytes("0")[0],
        0x02,
        Encoding.ASCII.GetBytes("AA")[0],
        Encoding.ASCII.GetBytes("AA")[1],
        0x06,
        Encoding.ASCII.GetBytes("1")[0],
        Encoding.ASCII.GetBytes("0")[0],
        0x0A,
        Encoding.ASCII.GetBytes("4")[0],
        Encoding.ASCII.GetBytes("5")[0],
        Encoding.ASCII.GetBytes("0")[0],
        Encoding.ASCII.GetBytes("0")[0],
        0x01,
        Encoding.ASCII.GetBytes("M")[0],
        Encoding.ASCII.GetBytes("y")[0],
        Encoding.ASCII.GetBytes("D")[0],
        Encoding.ASCII.GetBytes("a")[0],
        Encoding.ASCII.GetBytes("t")[0],
        Encoding.ASCII.GetBytes("a")[0],
        0x04
    };
    l_stream.Write(data, 0, data.Length);
}


I have no experience sending such data to hardware through sockets before. And I think my code is definitely not right. Especially for "AA", "4500", "MyData". Is there any easy and clear way to send data with the provided format?

Thanks in advanced,
tslin
Posted
Comments
Richard MacCutchan 6-Apr-15 4:09am    
Use your debugger to see exactly what gets stored in the resulting array. You could create the buffer dynamically just before you send it, although the coding would be almost the same.
tslin89 6-Apr-15 4:17am    
Thanks for the reply. So, I cannot optimize the code?

Could you please explain what do you mean by "Create buffer dynamically"?
Richard MacCutchan 6-Apr-15 4:38am    
You create memory buffers dynamically in the code as it is executed, with the new statement. You then fill the buffer with the control characters and strings as required, before sending to the device. If you do not understand that then I suggest you reread your C# reference guides.

One way to do it is to create it as a single string, with embedded hex values:
C#
string format = "\x0001Z00\x0002AA\x0006104500\x0001MyData\x0004";
byte[] data = Encoding.ASCII.GetBytes(format);

That will generate the 22 byte array you want:
01
5A Z
30 0
30 0
02
41 A
41 A
06
31 1
30 0
34 4
35 5
30 0
30 0
01
4D M
79 y
44 D
61 a
74 t
61 a
04
Will it work? Dunno - that's down to the sign! I'd see if the manufacturer has any examples in the manual, or on his website - because guesswork here can take a seriously long time! :laugh:
 
Share this answer
 
Comments
tslin89 6-Apr-15 5:05am    
Thanks for the reply. I will try with single string. In my situation, "MyData" will be 26 characters max that user will key in. And there are two places for "MyData". The supplier have no manual or SDK. I already asked them and they said they only provide format. I guess I need to change supplier if this not work out.
OriginalGriff 6-Apr-15 5:25am    
To be honest, if they don't provide even basic samples for customers, then their service is lousy to start with, and I'd be looking for a different supplier from day one.
If they can't be bothered to even put basic details to help customers on a web site these days, they don't really care about the product!
tslin89 6-Apr-15 5:49am    
Yes, I agree with you. Problem is company want to save money. The other supplier have the SDK even in C# but they are expensive. That's why my company choose current supplier. We are still evaluating and product, so it can be changed if this not work out. Thanks.
OriginalGriff 6-Apr-15 5:55am    
Saving money is difficult:
Are you buying this as a one off, or planning onshipping lots?

If a one off, ask yourself two questions:
1) What's the dollar value saving between the two?
2) What's the cost to the company of your time? (Figure your hourly wage times 2 to cover premises, electricity, taxes, etc. - it normally works out about right).
If (1) is not a lot greater than (2), stuff the sign! :laugh:

If this is a product, then what's going to be the cost if the supplier goes out of business, or drops the product? They already show no confidence and pride in it...
tslin89 6-Apr-15 6:01am    
Yes, you are right. I already told about this to management. They told me to try out first :P
Do you understand the string 0x01 + "Z" + "00" + 0x02 + "AA" + 0x06 + "10" + "4500" + 0x01 + "MyData"+ 0x04?
I mean, does "00" really mean two 0 characters, instead of 0x00? Same for "AA": are they really two A characters, or are they rather 0xAA?

You may have to study and understand the difference between a char value (which is in fact a byte value, at least in ASCII) and its string representation. Example: 0xAA is the number 170, i.e. a byte value expressed in its hexadecimal representation form; "AA" are two 'A' characters, and are stored in memory by the double-byte 0x4141 (because 0x41 is the ASCII code of the 'A' character). Both have not the same in-memory representation.

Same for 0x00, which is, well, a single byte zero value; "00" is a string stored with the double byte 0x3030 (0x30 is the ASCII code of the '0' character).

Maybe your method could be rewritten to:
C#
private void SendData()
{
    TcpClient l_Client = new TcpClient();
    int port;
    if (!int.TryParse(txtPortNumber.Text, out port) || (port < 0) || (port > 65535))
    {
        // There is a problem with the port number
        throw new ArgumentOutOfRangeException("txtPortNumber.Text");
    }
    l_Client.Connect(txtIPAddress.Text, port);
    NetworkStream l_stream = l_Client.GetStream();
    byte[] data = new byte[]{
        0x01,
        Encoding.ASCII.GetBytes("Z")[0],
        0x00,
        0x02,
        0xAA,
        0x06,
        0x10,
        0x45,
        0x00,
        0x01,
        Encoding.ASCII.GetBytes("M")[0],
        Encoding.ASCII.GetBytes("y")[0],
        Encoding.ASCII.GetBytes("D")[0],
        Encoding.ASCII.GetBytes("a")[0],
        Encoding.ASCII.GetBytes("t")[0],
        Encoding.ASCII.GetBytes("a")[0],
        0x04
    };
    l_stream.Write(data, 0, data.Length);
}

(I included a basic port checking - never trust user inputs without checking)

BUT: it's just a guess; I do not have enough reliable information, as what is expected from the LED supplier needs to be clarified.
That's why you need to sort this out by understanding what the supplier really expects you to provide. Once you have understood it, your issue will be solved, because you will be able to provide it the right data structure.

Hope this helps. Good luck.
 
Share this answer
 
Comments
tslin89 6-Apr-15 5:12am    
Thanks for the reply. "00" represents double "0". Not 0x00. Also for "4500", "AA". Thanks for the port checking :) I already have validation for both Port and IP Address. The function here is just for the clarification.

So, there is no other way to send "MyData" as a single string but to split them into multiple bytes? Then I guess I need to create a helper method that will convert string into byte array for easy understanding and clean coding.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900