安卓串口调试记录(包括串口通信底层知识点)

14 篇文章 0 订阅

一、有关串口通信基本知识

第一个字节的10位(1位起始位-0,8位数据位和1位停止位-1)共占约1.05ms,这样可计算出其波特率约为:10bit / 1.05ms X 1000 ≈ 9600 bit/s;

  • 有关数据的传输顺序 是LSB先出的;(参考链接

第一个数据 01010101 反过来就是10101010 =0xAA

第二个10101010 反过来01010101 0x55;

案例:

11 23 ---2020-6-25 10:55 byte发送;

int command[]=new int[]{0x01,0x03,0x00,0x01,0x00,0x04,0x15,0xC9}; 

0 1000 0000 1 * 0 1100 0000 1 * 0 0000 0000 1 *   

01               03              00

0 100 0000 1 * 0 0000 0000  1* 0 0010 0000  1*

01                00         04

0 1010 1000 1* 0 1001 0011 1*

    15         C9

示波器波形
标题

二、代码讲解

    对于倾角模块Bwh527,每发送一次查询数据,串口才能接收到一次数据;

 

 

//在js中代码
        this.printLog('开发串口发送数据');
        Utils.sendAngleCommandProductBwh527Data();
        Utils.sendAngleCommandProductListener();

        //倾角
        setTimeout(()=>{
        Utils.sendAngleCommandProductBwh527Data();

        },100);

//在RCTUtilsModule文件中代码;
    @ReactMethod
    public void sendAngleCommandProductBwh527Data(){  //发送数据

        byte command[]=new byte[]{0x77,0x04,0x00,0x04,0x08}; 

        serialPortUtils.sendSerialPortByte(command);

    }
    final int  adressAngleRs485=0x00;  //本地485地址;
    @ReactMethod
    public void sendAngleCommandProductListener(){  //开监听器;

        serialPortUtils.setOnDataReceiveListener(new             
   SerialPortUtils.OnDataReceiveListener() {
            @Override
            public void onDataReceive(byte[] buffer, int size) {
                Log.d(TAG, "进入数据监听事件中" + changeTool.ByteArrToHex(buffer));
                StringBuffer sBuffer = new StringBuffer();

                //适用于BWH526--圆形串口TTL输出
                if((buffer[2]==adressAngleRs485)){
                    if((buffer[0]==0x77)&&(buffer[1]==0x10)){
                        switch (buffer[3]){
                            case (byte)0x84:  {
                                //x轴角度
                                sBuffer.append(changeTool.Byte2Hex(buffer[4]));
                                sBuffer.append(changeTool.Byte2Hex(buffer[5]));
                                sBuffer.append(changeTool.Byte2Hex(buffer[6]));
                                sBuffer.append(changeTool.Byte2Hex(buffer[7]));
                                if((buffer[4]&0x10)==0x10){
                                    sBuffer.setCharAt(0,'-');
                                }else{
                                    sBuffer.setCharAt(0,' ');
                                }
                                sBuffer.insert(4,'.');
                                Log.d(TAG, "倾角:x轴角度:" + sBuffer);
                                //y轴角度
                                sBuffer.delete(0,sBuffer.length());
                                sBuffer.append(changeTool.Byte2Hex(buffer[8]));
                                sBuffer.append(changeTool.Byte2Hex(buffer[9]));
                                sBuffer.append(changeTool.Byte2Hex(buffer[10]));
                                sBuffer.append(changeTool.Byte2Hex(buffer[11]));
                                if((buffer[8]&0x10)==0x10){
                                    sBuffer.setCharAt(0,'-');
                                }else{
                                    sBuffer.setCharAt(0,' ');
                                }
                                sBuffer.insert(4,'.');
                                Log.d(TAG, "倾角:y轴角度:" + sBuffer);
                                sBuffer.delete(0,sBuffer.length());

                                break;
                            }
                            default: ;
                        }

                    }
                }
                //适用于BWM427--RS485输出
                if((buffer[0]==adressAngleRs485)&&(buffer[1]==0x03)){

                    if((buffer[2]==0x04)){
                        int temp =(buffer[3]<<8)|buffer[4];
                        float tempx=(temp-10000)/100;
                        sBuffer.append(tempx);
                        Log.d(TAG, "倾角:x轴角度:" +sBuffer);
                        sBuffer.delete(0,sBuffer.length());

                        temp =(buffer[5]<<8)|buffer[6];
                        float tempy=(temp-10000)/100;
                        sBuffer.append(tempy);
                        Log.d(TAG, "倾角:x轴角度:" + sBuffer);
                        sBuffer.delete(0,sBuffer.length());
                        
                    }

                }




            }
        });
    }

    //字节数组转转hex字符串
    //-------------------------------------------------------
    //1字节转2个Hex字符
    public static String Byte2Hex(Byte inByte) {
        return String.format("%02x", new Object[]{inByte}).toUpperCase();
    }

    //-------------------------------------------------------
    //字节数组转转hex字符串
    public static String ByteArrToHex(byte[] inBytArr) {
        StringBuilder strBuilder = new StringBuilder();
        for (byte valueOf : inBytArr) {
            strBuilder.append(Byte2Hex(Byte.valueOf(valueOf)));
            strBuilder.append(" ");
        }
        return strBuilder.toString();
    }

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值