安卓开发硬件开发之-大华条码秤开发1

最近公司做零售项目,接了电子秤,条码秤,打印机等一堆的硬件设备,其它设备网上都能找到相应资料这里就不在重述,这篇文章主要讲的是条码秤连接以及向秤发送命令与接收回应。
1.条码秤的连接:
条码秤通讯采用的是ping ip的方式,我们就用socket通讯来建立连接,我socket通讯偷懒采用的是别人的库OkSocket 引用地址是:implementation’com.tonystark.android:socket:latest.release’ 因为条码秤没得啥子固定包头这些,所以是直接将lib下载下来导入进了项目中修改了ReaderImpl文件

import com.xuhao.didi.core.exceptions.ReadException;
import com.xuhao.didi.core.iocore.interfaces.IOAction;
import com.xuhao.didi.core.pojo.OriginalData;
import com.xuhao.didi.core.protocol.IReaderProtocol;
import com.xuhao.didi.core.utils.BytesUtils;
import com.xuhao.didi.core.utils.SLog;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * Created by xuhao on 2017/5/31.
 */

public class ReaderImpl extends AbsReader {
   

//    private ByteBuffer mRemainingBuf;
//
//    @Override
//    public void read() throws RuntimeException {
   
//        OriginalData originalData = new OriginalData();
//        IReaderProtocol headerProtocol = mOkOptions.getReaderProtocol();
//        int headerLength = headerProtocol.getHeaderLength();
//        ByteBuffer headBuf = ByteBuffer.allocate(headerLength);
//        headBuf.order(mOkOptions.getReadByteOrder());
//        try {
   
//            if (mRemainingBuf != null) {
   
//                mRemainingBuf.flip();
//                int length = Math.min(mRemainingBuf.remaining(), headerLength);
//                headBuf.put(mRemainingBuf.array(), 0, length);
//                if (length < headerLength) {
   
//                    //there are no data left
//                    mRemainingBuf = null;
//                    readHeaderFromChannel(headBuf, headerLength - length);
//                } else {
   
//                    mRemainingBuf.position(headerLength);
//                }
//            } else {
   
//                readHeaderFromChannel(headBuf, headBuf.capacity());
//            }
//            originalData.setHeadBytes(headBuf.array());
//            if (SLog.isDebug()) {
   
//                SLog.i("read head: " + BytesUtils.toHexStringForLog(headBuf.array()));
//            }
//            int bodyLength = headerProtocol.getBodyLength(originalData.getHeadBytes(), mOkOptions.getReadByteOrder());
//            if (SLog.isDebug()) {
   
//                SLog.i("need read body length: " + bodyLength);
//            }
//            if (bodyLength > 0) {
   
//                if (bodyLength > mOkOptions.getMaxReadDataMB() * 1024 * 1024) {
   
//                    throw new ReadException("Need to follow the transmission protocol.\r\n" +
//                            "Please check the client/server code.\r\n" +
//                            "According to the packet header data in the transport protocol, the package length is " + bodyLength + " Bytes.\r\n" +
//                            "You need check your <ReaderProtocol> definition");
//                }
//                ByteBuffer byteBuffer = ByteBuffer.allocate(bodyLength);
//                byteBuffer.order(mOkOptions.getReadByteOrder());
//                if (mRemainingBuf != null) {
   
//                    int bodyStartPosition = mRemainingBuf.position();
//                    int length = Math.min(mRemainingBuf.remaining(), bodyLength);
//                    byteBuffer.put(mRemainingBuf.array(), bodyStartPosition, length);
//                    mRemainingBuf.position(bodyStartPosition + length);
//                    if (length == bodyLength) {
   
//                        if (mRemainingBuf.remaining() > 0) {//there are data left
//                            ByteBuffer temp = ByteBuffer.allocate(mRemainingBuf.remaining());
//                            temp.order(mOkOptions.getReadByteOrder());
//                            temp.put(mRemainingBuf.array(), mRemainingBuf.position(), mRemainingBuf.remaining());
//                            mRemainingBuf = temp;
//                        } else {//there are no data left
//                            mRemainingBuf = null;
//                        }
//                        //cause this time data from remaining buffer not from channel.
//                        originalData.setBodyBytes(byteBuffer.array());
//                        mStateSender.sendBroadcast(IOAction.ACTION_READ_COMPLETE, originalData);
//                        return;
//                    } else {//there are no data left in buffer and some data pieces in channel
//                        mRemainingBuf = null;
//                    }
//                }
//                readBodyFromChannel(byteBuffer);
//                originalData.setBodyBytes(byteBuffer.array());
//            } else if (bodyLength == 0) {
   
//                originalData.setBodyBytes(new byte[0]);
//                if (mRemainingBuf != null) {
   
//                    //the body is empty so header remaining buf need set null
//                    if (mRemainingBuf.hasRemaining()) {
   
//                        ByteBuffer temp = ByteBuffer.allocate(mRemainingBuf.remaining());
//                        temp.order(mOkOptions.getReadByteOrder());
//                        temp.put(mRemainingBuf.array(), mRemainingBuf.position(), mRemainingBuf.remaining());
//                        mRemainingBuf = temp;
//                    } else {
   
//                        mRemainingBuf = null;
//                    }
//                }
//            } else if (bodyLength < 0) {
   
//                throw new ReadException(
//                        "read body is wrong,this socket input stream is end of file read " + bodyLength + " ,that mean this socket is disconnected by server");
//            }
//            mStateSender.sendBroadcast(IOAction.ACTION_READ_COMPLETE, originalData);
//        } catch (Exception e) {
   
//            ReadException readException = new ReadException(e);
//            throw readException;
//        }
//    }
//
//    private void readHeaderFromChannel(ByteBuffer headBuf, int readLength) throws IOException {
   
//        for (int i = 0; i < readLength; i++) {
   
//            byte[] bytes = new byte[1];
//            int value = mInputStream.read(bytes);
//            if (value == -1) {
   
//                throw new ReadException(
//                        "read head is wrong, this socket input stream is end of file read " + value + " ,that mean this socket is disconnected by server");
//            }
//            headBuf.put(bytes);
//        }
//    }
//
//    private void readBodyFromChannel(ByteBuffer byteBuffer) throws IOException {
   
//        while (byteBuffer.hasRemaining()) {
   
//            try {
   
//                byte[] bufArray = new byte[mOkOptions.getReadPackageBytes()];
//                int len = mInputStream.read(bufArray);
//                if (len == -1) {
   
//                    break;
//                }
//                int remaining = byteBuffer.remaining();
//                if (len > remaining) {
   
//                    byteBuffer.put(bufArray, 0, remaining);
//                    mRemainingBuf = ByteBuffer.allocate(len - remaining);
//                    mRemainingBuf.order(mOkOptions.getReadByteOrder());
//                    mRemainingBuf.put(bufArray, remaining, len - remaining);
//                } else {
   
//                    byteBuffer.put(bufArray, 0, len);
//                }
//            } catch (Exception e) {
   
//                throw e;
//            }
//        }
//        if (SLog.isDebug()) {
   
//            SLog.i("read total bytes: " + BytesUtils.toHexStringForLog(byteBuffer.array()));
//            SLog.i("read total length:" + (byteBuffer.capacity() - byteBuffer.remaining()));
//        }
//    }

    //接收byte数组
    @Overri
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值