itas109 / CSerialPort

CSerialPort - lightweight cross-platform serial port library for C++/C/C#/Java/Python/Node.js/Electron
https://blog.csdn.net/itas109/article/details/84282860
Other
710 stars 337 forks source link

关于 设置读取通知触发最大字节数问题 #86

Closed yangwei1987 closed 2 months ago

yangwei1987 commented 4 months ago

描述bug 设置4096缓冲区,设置超时时间1ms,单次发送数据6280,实际只接收到4096数据,对缓冲区溢出未作保护;

重现步骤 设置4096缓冲区,设置超时时间1ms,单次发送数据6280

预期行为 设置读取通知触发最大字节数问题

截图 如果适用,请添加屏幕截图以帮助解释您的问题

运行环境信息:

其他内容 在此处添加有关该问题的任何其他内容

itas109 commented 4 months ago

目前的策略是缓冲区满了,直接覆盖。 即便是有最大字节数通知功能,也不能避免缓冲区被覆盖问题。 因此,建议根据实际业务调整缓冲区大小,最大 maxBufferSize = 1073741824;// 2^30

itas109 commented 4 months ago

最大字节数通知功能 是一个比较好的建议,虽然不能完全避免缓冲区覆盖问题。 但也能一定程度上解决推送问题。 后续会增加该功能。

yangwei1987 commented 4 months ago

非常感谢您能接受提出的建议,目前我在void *CSerialPortUnixBase::commThreadMonitor(void *pParam)中做了轻微改动,但是使用不灵活,希望后续能增加配置超出后通知接口配置。 if(p_base->p_buffer->getUnusedLen()<2048){ p_base->p_readEvent->onReadEvent(p_base->getPortName(), p_base->p_buffer->getUsedLen()); }else{ p_base->p_timer->startOnce(readIntervalTimeoutMS, p_base->p_readEvent, &itas109::CSerialPortListener::onReadEvent, p_base->getPortName(), p_base->p_buffer->getUsedLen()); }

itas109 commented 2 months ago

@yangwei1987 感谢反馈,最新代码已增加缓冲区满通知功能。 缓冲区满 或者 达到设置大小会发出读取通知。

sp.setByteReadBufferFullNotify(4096 * 0.8); // buffer full notify