This example only shows how to display a text but it should give you an idea of how to extend it to cater for all messages and reading results as well:
class Program
{
private static readonly byte[] ControlCharacters = new byte[]
{
(byte)0x05
};
private static void Add(byte data, IList<byte> buffer)
{
if (ControlCharacters.Contains(data))
buffer.Add(0x10);
buffer.Add(data);
}
private static byte CalculateCheckSum(IEnumerable<byte> buffer)
{
byte[] payload = buffer.Reverse().Take(buffer.Count() - 3).Reverse().ToArray();
return (byte)((from b in payload where b != 0x10 select (int)b).Sum() | 0x80);
}
public static byte[] GetDisplayTextMessage(int slaveAddress, string text, int font, int x, int y, int w)
{
IList<byte> buffer = new List<byte>();
buffer.Add(0x04);
buffer.Add((byte)(0x80 | slaveAddress));
buffer.Add(0x02);
buffer.Add(0x18);
buffer.Add(0x01);
buffer.Add(0x01);
buffer.Add((byte)font);
Add(0x00, buffer); Add((byte)x, buffer);
Add(0x00, buffer); Add((byte)y, buffer);
Add(0x00, buffer); Add((byte)w, buffer);
foreach (byte b in Encoding.UTF8.GetBytes(text))
Add(b, buffer);
buffer.Add(0x03);
buffer.Add(CalculateCheckSum(buffer));
return buffer.ToArray();
}
static void Main(string[] args)
{
byte[] data = GetDisplayTextMessage(1, "ABC", 0, 5, 10, 64);
TcpClient client = new TcpClient("localhost", 4242);
Stream stream = client.GetStream();
stream.Write(data, 0, data.Length);
stream.Close();
}
Obviously you'll have to adjust for your host and port.
Hope this helps,
Fredrik Bornander