#
whycq
2024-08-14 928380a58b93cc24d65a836071edafeed9f1a0ab
#
4个文件已修改
2个文件已添加
291 ■■■■ 已修改文件
app/src/main/java/com/example/agvcontroller/MainActivity.java 35 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/protocol/PacHeader.java 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/protocol2/AgvPackage.java 101 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/protocol2/PackagePart.java 87 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java 22 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/com/example/agvcontroller/MainActivity.java
@@ -6,6 +6,8 @@
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.KeyEvent;
@@ -13,6 +15,8 @@
import android.view.View;
import android.widget.Button;
import com.example.agvcontroller.protocol2.AgvPackage;
import com.example.agvcontroller.protocol2.PackagePart;
import com.example.agvcontroller.socket.NettyServerHandler;
import com.example.agvcontroller.socket.SocketManager;
import com.example.agvcontroller.socket.SocketTask;
@@ -35,6 +39,21 @@
    private Socket socket;
    SocketManager socketManager;
    private boolean isDowm = false;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            // 在这里进行打印输出
            System.out.println("打印输出");
            if (isDowm) {
                byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 示例消息
                nettyServerHandler.sendMessageToClient(clientId, message2); // 发送消息到客户端
                handler.sendEmptyMessageDelayed(0, 100);
            }
            return false;
        }
    });
    String clientId;
    NettyServerHandler nettyServerHandler;
    @Override
@@ -46,10 +65,17 @@
        vibrateButton = findViewById(R.id.btn_stop);
        Intent intent = getIntent();
        //String clientId = intent.getStringExtra("message");
        clientId = intent.getStringExtra("message");
        Log.i("message1",clientId);
        // 初始化单轴使能
        int single = 9;
        int len = PackagePart.UNIQUENO.getLen()
                + PackagePart.TIMESTAMP.getLen()
                + PackagePart.COMMAND_MARK.getLen()
                + 16 + 1 + 1 + 1;
        for (int i = 0; i < single; i++) {
        }
        byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 示例消息
        nettyServerHandler.sendMessageToClient(clientId, message2); // 发送消息到客户端
@@ -77,11 +103,10 @@
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if (motionEvent.getAction() == KeyEvent.ACTION_DOWN) {
                byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x06}; // 示例消息
                nettyServerHandler.sendMessageToClient(clientId, message2); // 发送消息到客户端
                isDowm = true;
                handler.sendEmptyMessage(0);
            } else if (motionEvent.getAction() == KeyEvent.ACTION_UP) {
                byte[] message2 = new byte[]{0x01, 0x02, 0x03, 0x07}; // 示例消息
                nettyServerHandler.sendMessageToClient(clientId, message2); // 发送消息到客户端
                isDowm = false;
            }
            return false;
        }
app/src/main/java/com/example/agvcontroller/protocol/PacHeader.java
@@ -7,6 +7,34 @@
 */
public class PacHeader {
    public byte getStartSymbol() {
        return startSymbol;
    }
    public int getContentLength() {
        return contentLength;
    }
    public String getUniqueNo() {
        return uniqueNo;
    }
    public ProtocolType getProtocolType() {
        return protocolType;
    }
    public int getTimestamp() {
        return timestamp;
    }
    public String getSerialNum() {
        return serialNum;
    }
    public EncryType getEncryptType() {
        return encryptType;
    }
    /**
     * 起始符
     * 固定为"0xEE || 0xAA"
app/src/main/java/com/example/agvcontroller/protocol/ProtocolEncoder.java
@@ -42,7 +42,7 @@
            //String uniqueNo = pac.getHeader().getUniqueNo();
                //byte[] uniquenoBytes = RadixTools.intToBytes(Integer.parseInt(pac.getHeader().getUniqueNo()));   // uniqueno
            //byte[] uniquenoBytes = RadixTools.intToBytes(Integer.parseInt(pac.getHeader().getUniqueNo()));   // uniqueno
            int len = PackagePart.UNIQUENO.getLen()     // len
@@ -50,14 +50,14 @@
                    + PackagePart.COMMAND_MARK.getLen()
                    + bodyBytes.length;
            //out.writeByte(pac.getHeader().getStartSymbol())         // symbol
            //        .writeShortLE(len)
            //        .writeBytes(Utils.reverse(uniquenoBytes))       // uniqueno
            //        .writeIntLE((int) (System.currentTimeMillis() / 1000))   // timestamp
            //        .writeByte(pac.getHeader().getProtocolType().getCode()) // type
            //        .writeBytes(bodyBytes)                          // body
            //        .writeShort(pac.getValidCode())                 // valid
            //;
            out.writeByte(pac.getHeader().getStartSymbol())         // symbol
                    .writeShortLE(len)
                    //.writeBytes(Utils.reverse(uniquenoBytes))       // uniqueno
                    .writeIntLE((int) (System.currentTimeMillis() / 1000))   // timestamp
                    .writeByte(pac.getHeader().getProtocolType().getCode()) // type
                    .writeBytes(bodyBytes)                          // body
                    .writeShort(pac.getValidCode())                 // valid
            ;
            pac.setValidCode(ValidUtil.calculateValidByteFromBuff(out));
            out.resetReaderIndex();
app/src/main/java/com/example/agvcontroller/protocol2/AgvPackage.java
New file
@@ -0,0 +1,101 @@
package com.example.agvcontroller.protocol2;
import com.example.agvcontroller.protocol.PacBody;
import com.example.agvcontroller.protocol.PacErrorType;
import com.example.agvcontroller.protocol.PacHeader;
import io.netty.buffer.ByteBuf;
public class AgvPackage {
    /**
     * 源数据包缓冲区(引用)
     */
    private ByteBuf sourceBuff;
    /**
     * 原始消息对应的16进制字符串
     */
    private String sourceHexStr;
    /**
     * 消息头部
     */
    private PacHeader header;
    /**
     * 消息体
     */
    private PacBody body;
    public ByteBuf getSourceBuff() {
        return sourceBuff;
    }
    public void setSourceBuff(ByteBuf sourceBuff) {
        this.sourceBuff = sourceBuff;
    }
    public String getSourceHexStr() {
        return sourceHexStr;
    }
    public void setSourceHexStr(String sourceHexStr) {
        this.sourceHexStr = sourceHexStr;
    }
    public PacHeader getHeader() {
        return header;
    }
    public void setHeader(PacHeader header) {
        this.header = header;
    }
    public int getValidCode() {
        return validCode;
    }
    public void setValidCode(int validCode) {
        this.validCode = validCode;
    }
    public PacBody getBody() {
        return body;
    }
    public void setBody(PacBody body) {
        this.body = body;
    }
    public boolean isErrorPac() {
        return errorPac;
    }
    public void setErrorPac(boolean errorPac) {
        this.errorPac = errorPac;
    }
    public PacErrorType getPacErrorType() {
        return pacErrorType;
    }
    public void setPacErrorType(PacErrorType pacErrorType) {
        this.pacErrorType = pacErrorType;
    }
    /**
     * 消息的校正码
     */
    private int validCode;
    /**
     * 是否为校验异常包
     */
    private boolean errorPac;
    /**
     * 校验异常类型
     */
    private PacErrorType pacErrorType;
}
app/src/main/java/com/example/agvcontroller/protocol2/PackagePart.java
New file
@@ -0,0 +1,87 @@
package com.example.agvcontroller.protocol2;
/**
 * 报文标识枚举
 * 下标1: 起始索引
 * 下标2: 长度
 * 下标3: 描述
 * Created by vincent on 2019-04-03
 */
public enum PackagePart {
    /**
     * 起始符
     */
    START_SYMBOL(0, 1, "起始符"),
    /**
     * 数据单元长度
     */
    CONTENT_LENGTH(1, 2, "数据单元长度"),
    /**
     * 唯一标识码
     */
    UNIQUENO(3, 4, "唯一标识码"),
    /**
     * 时间戳
     */
    TIMESTAMP(7, 4, "时间戳"),
    /**
     * 命令标识
     */
    COMMAND_MARK(11, 1, "命令标识"),
    /**
     * 数据单元
     */
    CONTENT(12, -1, "数据单元"),
    /**
     * 校验码
     */
    VALIDE_CODE( -1, 2, "校验码"),
    /**
     * 应答标志
     */
    ACK_MARK(3, 1, "应答标志"),
    /**
     * 数据单元加密方式
     */
    ENCRYPT_TYPE(21, 1, "数据单元加密方式"),
    ;
    // 字节段开始索引
    private Integer startIndex;
    // 字节段总字节数
    private Integer len;
    // 描述
    private String des;
    PackagePart(int startIndex, int len, String des) {
        this.startIndex = startIndex;
        this.len = len;
        this.des = des;
    }
    public Integer getStartIndex() {
        return startIndex;
    }
    public Integer getLen() {
        return len;
    }
    public String getDes() {
        return des;
    }
}
app/src/main/java/com/example/agvcontroller/socket/NettyServerHandler.java
@@ -3,6 +3,7 @@
import java.nio.charset.StandardCharsets;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
@@ -44,19 +45,6 @@
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 处理接收到的消息
        ByteBuf byteBuf = (ByteBuf) msg;
        //byte[] data = new byte[byteBuf.readableBytes()];
        //byteBuf.readBytes(data);
        //String received = new String(data, StandardCharsets.UTF_8);
        //System.out.println("Received from client: " + received);
        //
        //// 回复消息
        ////ByteBuf response = Unpooled.copiedBuffer("Response from server", StandardCharsets.UTF_8);
        ////ctx.writeAndFlush(response);
        //
        //byte[] responseHex = hexStringToByteArray("48656c6c6f20576f726c64"); // "Hello World" in hex
        //ByteBuf response = Unpooled.wrappedBuffer(responseHex);
        //ctx.writeAndFlush(response);
        try {
            while (byteBuf.isReadable()) {
                byte[] bytes = new byte[byteBuf.readableBytes()];
@@ -89,17 +77,17 @@
        return data;
    }
    public static void sendMessageToClient(String clientId, byte[] message) {
    public static void sendMessageToClient(String clientId, Object message) {
        Channel channel = channelMap.get(clientId);
        if (channel != null && channel.isActive()) {
            ByteBuf buf = Unpooled.wrappedBuffer(message);
            String upperCase = ByteBufUtil.hexDump(buf).toUpperCase();
            Log.d(TAG, "upperCase " + upperCase);
            channel.writeAndFlush(buf);
        } else {
            Log.d(TAG, "Client " + clientId + " is not connected");
        }
        Log.d(TAG, "Client " + clientId + " is not connected");
    }
    @Override