Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

接收一次收不全,会自动分段 #50

Open
yecaler opened this issue Jun 15, 2023 · 3 comments
Open

接收一次收不全,会自动分段 #50

yecaler opened this issue Jun 15, 2023 · 3 comments

Comments

@yecaler
Copy link

yecaler commented Jun 15, 2023

我使用串口调试助手是能收全的,返回的指令在本demo中接收会自动分段,最大接收64个字符,如何可以接收完全?

我修改size = mInputStream.read(received);
成size = mInputStream.read();以后收到的全是000000000000000000000

@licheedev

@licheedev
Copy link
Owner

licheedev commented Jun 16, 2023

这demo是google原代码自带的,我没改过里面的代码。


如果是这个demo的话,改大这个数组的容量看看。

我修改size = mInputStream.read(received);
成size = mInputStream.read();以后收到的全是000000000000000000000

你下面改的是读取一个字节,到size变量里,size变成读取到的那个字节的内容了,而不是到读取到的数据长度。
received数组压根没参与,内容没改变过,当然是默认初始化的全0。

@yecaler yecaler closed this as completed Nov 28, 2023
@yecaler yecaler reopened this Jan 4, 2024
@yecaler
Copy link
Author

yecaler commented Jan 4, 2024

麻烦问一下,现在一次接收最多64个字符,如何修改成接收1024个字符?
@OverRide
public void run() {
byte[] received = new byte[50 * 1024];
int size;

    LogUtils.e("开始读线程");

    while (true) {
        if (Thread.currentThread().isInterrupted()) {
            break;
        }
        try {
            int available = mInputStream.available();
            if (available > 0) {
                size = mInputStream.read(received);
                if (size > 0) {
                    onDataReceive(received, size);
                }
            } else {
                // 暂停一点时间,免得一直循环造成CPU占用率过高
                SystemClock.sleep(1);
            }
        } catch (IOException e) {
            LogUtils.e("读取数据失败", e);
        }
        //Thread.yield();
    }

    LogUtils.e("结束读进程");
}

@yecaler
Copy link
Author

yecaler commented Jan 5, 2024

@licheedev
大佬,帮忙看一下如何修改成接收多一些指令?感谢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants