簡體   English   中英

如何在tcp端口上使用二進制通信協議發送數據包

[英]How to send packet using binary communication protocol on tcp port

我正在研究一種可以通過傳感器測量一些讀數的設備。 設備由Android應用程序運營。 我必須從TCP層獲取讀數。 這是在TCP上發送數據的代碼

TcpClient.java

import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

/**
* Created by shahbaz on 25/4/17.
*/

 public class TcpClient {

 public static final String SERVER_IP = "192.168.1.76"; //server IP address
 public static final int SERVER_PORT = 1800;
 // message to send to the server
 private String mServerMessage;
 // sends message received notifications
 private OnMessageReceived mMessageListener = null;
 // while this is true, the server will continue running
 private boolean mRun = false;
 // used to send messages
 private PrintWriter mBufferOut;
 // used to read messages from the server
 private BufferedReader mBufferIn;

 /**
  * Constructor of the class. OnMessagedReceived listens for the messages received from server
  */
 public TcpClient(OnMessageReceived listener) {
    mMessageListener = listener;
 }

/**
 * Sends the message entered by client to the server
 *
 * @param message text entered by client
 */
public void sendMessage(String message) {
    if (mBufferOut != null && !mBufferOut.checkError()) {
        mBufferOut.println(message);
        mBufferOut.flush();
    }
}

/**
 * Close the connection and release the members
 */
public void stopClient() {

    mRun = false;

    if (mBufferOut != null) {
        mBufferOut.flush();
        mBufferOut.close();
    }

    mMessageListener = null;
    mBufferIn = null;
    mBufferOut = null;
    mServerMessage = null;
}

public void run() {

    mRun = true;

    try {
        //here you must put your computer's IP address.
        InetAddress serverAddr = InetAddress.getByName(SERVER_IP);

        Log.e("TCP Client", "C: Connecting...");

        //create a socket to make the connection with the server
        Socket socket = new Socket(serverAddr, SERVER_PORT);

        try {

            //sends the message to the server
            mBufferOut = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);

            //receives the message which the server sends back
            mBufferIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));


            //in this while the client listens for the messages sent by the server
            while (mRun) {

                mServerMessage = mBufferIn.readLine();

                if (mServerMessage != null && mMessageListener != null) {
                    //call the method messageReceived from MyActivity class
                    mMessageListener.messageReceived(mServerMessage);
                }

            }

            Log.e("RESPONSE FROM SERVER", "S: Received Message: '" + mServerMessage + "'");

        } catch (Exception e) {

            Log.e("TCP", "S: Error", e);

        } finally {
            //the socket must be closed. It is not possible to reconnect to this socket
            // after it is closed, which means a new socket instance has to be created.
            socket.close();
        }

    } catch (Exception e) {

        Log.e("TCP", "C: Error", e);

    }

}

   //Declare the interface. The method messageReceived(String message) will must be implemented in the MyActivity
   //class at on asynckTask doInBackground
   public interface OnMessageReceived {
    public void messageReceived(String message);
}

}

包格式 數據包結構

數據包格式包含,當在TCP上與設備通信時,不識別數據包之間的邊界,在這種情況下,如果數據包無序或者如果丟失了任何數據包,則可以使用“報頭啟動”識別新數據包。 因此,數據包中的前2個字節表示數據包的開始。

標頭開始:雙字節字段,指示每個數據包的開始。 0x55AA是用作頭開始的2字節數。

協議版本:單字節字段,用於指定正在使用的協議版本。 有效載荷中指定的版本將決定有效載荷結構。 在任何給定時刻,設備將支持單協議版本。 目前的協議版本為'1'。

DSN:序列號是1字節字段,它將唯一地標識數據包。 數據包的請求者必須在請求有效載荷中填寫此字段; 響應者必須在響應有效負載中填充相同的唯一標識符。

請求ID:單字節字段指定命令ID。 有效載荷的解析將基於命令id完成。 在請求有效載荷的情況下,該字段將不為零,並且在響應的情況下它將為零。

有效載荷長度:雙字節字段以字節為單位指定有效載荷的長度。 它指定了有效負載長度字段的字節數。 在有效載荷長度中,不包括報頭長度和CRC。 目前,網關設備支持的最大有效載荷長度為512(字節)。 CRC:1字節字段,通過對所有字節進行異或運算並將XOR計數加0來計算。

它正在發揮作用。 但根據文檔我必須使用二進制通信協議發送數據包。 包括頭開始有效載荷數據等。如何在數據包結構中發送這些參數? 我該如何創建數據包?

任何幫助表示贊賞。

主要的錯誤是我沒有考慮原始數據類型的大小。

byte = 1個字節

short = 2個字節

int = 4個字節

long = 8個字節

float = 4個字節

double = 8個字節

char = 2個字節

在引用原始數據類型的大小后,我意識到我們應該跟蹤數據包的大小和索引,因為我們正在處理字節數組。

TcpPacket.java

public class TcpPacket {

private static int header_start =  0x55AA;

private static int protocol_version = 1;

private PacketUtils packetUtils = new PacketUtils();






 public byte[] getHandshakePacket()
 {
    int request_id = 1;

    byte[] header_data = packetUtils.ItoBA2(header_start);
    byte[] payload_data = packetUtils.ItoBA4(packetUtils.getDateTime());
    byte[] payload_length = packetUtils.ItoBA2(4);


    byte[] a_data = new byte[]{header_data[0], header_data[1], (byte) protocol_version, packetUtils.getDSN(), (byte) request_id, payload_length[0], payload_length[1],
            payload_data[0], payload_data[1], payload_data[2], payload_data[3]};

    byte[] b_data = new byte[]{ packetUtils.getCRC(a_data)};

    byte[] packet_data = packetUtils.concatBytes(a_data,b_data);


    return packet_data;
 }

}

PacketUtils.java

public class PacketUtils {



public byte[] ItoBA4(int value) {       // integer to bytes function (return byte array of 4 bytes)
    return new byte[] {
            (byte)(value >>> 24),
            (byte)(value >>> 16),
            (byte)(value >>> 8),
            (byte)value};
}


public byte[] ItoBA2(int value) {   // integer to bytes function (return byte array of 2 bytes)
    return new byte[] {
            (byte)(value >>> 8),
            (byte)value};
}


public byte getDSN()    // return one byte random number
{
    char[] chars = "1234567890".toCharArray();
    StringBuilder sb = new StringBuilder();
    Random random = new Random();
    for (int i = 0; i < 1; i++) {
        char c = chars[random.nextInt(chars.length)];
        sb.append(c);
    }
    byte output = Byte.valueOf(sb.toString());
    return output;
}



public byte getCRC(byte[] packet)   //  required CRC function (return byte)
{
    try
    {
        if (packet == null)
        {
            //Logger.Error("empty packet received");
            return (byte)0;
        }

        byte XORCheckSum = 0;
        byte zeroCount = 0;
        byte FFCount = 0;

        for (int i = 0; i < packet.length; i++)
        {
            XORCheckSum ^= packet[i];
            if (packet[i] == (byte) 0)
            {
                zeroCount++;
                continue;
            }
            if (packet[i] == (byte)255)
            {
                FFCount++;
                continue;
            }
        }

        XORCheckSum ^= zeroCount;
        XORCheckSum ^= FFCount;
        return XORCheckSum;
    }
    catch (Exception ex)
    {
        //Logger.Error(ex);
        return (byte)0;
    }
}



byte[] concatBytes(byte[]...arrays)     //  concatenate byte arrays
{
    // Determine the length of the result array
    int totalLength = 0;
    for (int i = 0; i < arrays.length; i++)
    {
        totalLength += arrays[i].length;
    }

    // create the result array
    byte[] result = new byte[totalLength];

    // copy the source arrays into the result array
    int currentIndex = 0;
    for (int i = 0; i < arrays.length; i++)
    {
        System.arraycopy(arrays[i], 0, result, currentIndex, arrays[i].length);
        currentIndex += arrays[i].length;
    }

    return result;
}

public int getDateTime()
{
    int dateInSec = (int) (System.currentTimeMillis() / 1000);
    return dateInSec;

}



}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM