通过TCP通讯 [英] Communication over TCP

查看:79
本文介绍了通过TCP通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我有关与LCD显示器通信的问题吗?
ı发布有关通过tcp通讯的手册
谢谢

Can anybody help to me about communication with a LCD monitor
ı post the manual about communication over tcp
thank you

MIS1 communication over TCP/IP
In order to exchange information with an LCD controller all data must be wrapped into an MIS1 frame according to the Definition of Host (DoH). The following table shows all transport layers:
#        Layer         Function
  7      Data          MIS1, DoH
  6      Data          MIS1, DoH
  5      Data          MIS1, DoH
  4      Transport     TCP
  3      Network       IP
  2      Data Link     Ethernet
  1      Physical      Ethernet


Table 1: OSI model
In the example below the required MIS1 frames for displaying a given text sequence on the display (command 0x18, see Definition of Host) are illustrated.
1. Sending the command
At first, the host sends the command to the LCD controller with slave address 1 which contains all necessary information:
>>>[19] 0x04 0x81 0x02 0x18 0x01 0x01 0x00 0x00 0x10 0x05 0x00 0x0A
0x00 0x40 0x41 0x42 0x43 0x03 0xB2
Control characters: 0x04 0x02 0x03
Slave address: 0x81 Address 0x01 OR 0x80
Code and Command ID: 0x18 0x01
Data payload: 0x01 Image number, 1
0x00 Font number, 0
0x00 0x05 Horizontal position x, 5px
0x00 0x0A Vertical position y, 10px
0x00 0x40 Text range width w, 64px
0x41 0x42 0x43 Text data, "ABC"
Escape character: 0x10
Checksum: 0xB2
As the data payload byte 0x05 is a control character (ENQ) it has to be escaped with 0x10 (DLE). The checksum is an arithmetic sum counting all bytes after 0x02 (STX) including 0x03 (ETX)  any DLE characters (0x10) must be ignored! Thus the sum can be calculated as:
0x18 + 0x01 + 0x01 +  + 0x42 + 0x43 + 0x03 = 0x0132 OR 0x80 = 0xB2
2. Receiving the acknowledgement
When a command has been sent to a slave controller it immediately replies with either an ACK (0x06, positive) or a NAK (0x15, negative) byte. This returned byte only informs the host about the MIS1 frame syntax, not about the correctness of the command itself.
<<<[1] 0x06
Status byte: 0x06 Command syntax OK
3. Requesting the status
In order to check whether a previously sent command has been accepted or an error occurred, the host can request the status of the client by sending an enquiry.
>>>[3] 0x04 0x81 0x05
Control characters: 0x04 0x05
Slave address: 0x81 Address 0x01 OR 0x80
4. Receiving the error notification
In case the command from the first step did not generate an error the slave controller will simply respond with a single NAK (0x15) byte.
<<<[1] 0x15
Status byte: 0x15 No error available
Otherwise an appropriate error notification message will be returned to the host. For example if the text range width was chosen too small for the transmitted text array the following notification would be sent:
>>>[3] 0x04 0x8C 0x01 0x18 0x01 0x03 0xA9
Control characters: 0x04 0x03
Code and Command ID: 0x8C 0x01 Command ID generating the error
Data payload: 0x18 Code generating the error
0x01 Error code, "CLIPPED"
Checksum: 0xA9

推荐答案

此示例仅显示了如何显示文本,但它应该使您了解如何扩展文本以适应所有消息并读取结果:

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
{
    // I don't know if this needs to be extended
    private static readonly byte[] ControlCharacters = new byte[]
        {
            (byte)0x05
        };

    // This escapes payload data if the data is a control character
    private static void Add(byte data, IList<byte> buffer)
    {
        if (ControlCharacters.Contains(data)) // Below 1F is control characters
            buffer.Add(0x10); // Escape
        buffer.Add(data);
    }

    private static byte CalculateCheckSum(IEnumerable<byte> buffer)
    {
        // Get everything after the first 3 bytes
        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);                           // Start
        buffer.Add((byte)(0x80 | slaveAddress));    // Slave address
        buffer.Add(0x02);                           // Control
        buffer.Add(0x18);                           // Code
        buffer.Add(0x01);                           // Command
        buffer.Add(0x01);                           // DataPayload image number
        buffer.Add((byte)font);                     // Font number
        Add(0x00, buffer); Add((byte)x, buffer);    // HPosition x
        Add(0x00, buffer); Add((byte)y, buffer);    // VPosition y
        Add(0x00, buffer); Add((byte)w, buffer);    // Text range width
        foreach (byte b in Encoding.UTF8.GetBytes(text))
            Add(b, buffer);                         // Add the text
        buffer.Add(0x03);                           // Control
        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();
    }



显然,您必须调整主机和端口.

希望这会有所帮助,
弗雷德里克·博纳德(Fredrik Bornander)



Obviously you''ll have to adjust for your host and port.

Hope this helps,
Fredrik Bornander


这篇关于通过TCP通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆