1 Star 0 Fork 7

hira / SmartSerial

forked from liushuai / SmartSerial 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
smartserial_example.cpp 1.83 KB
一键复制 编辑 原始数据 按行查看 历史
liushuai 提交于 2019-12-03 15:21 . feature: support open and close
#include "SmartSerial.h"
#include "log.h"
static void listPort() {
auto ports = serial::list_ports();
if (ports.empty()) {
LOGI("No Any Port!");
}
for (const auto& info : ports) {
static int count;
LOG("list port [%d]:", count++);
// Port
// macOS: /dev/cu.usbmodem8D8842A649551
// Linux: /dev/ttyACM0
// Windows: COM6
LOG("port:%s", info.port.c_str());
// Description
// macOS: Scope STM32 Virtual ComPort
// Linux: Scope STM32 Virtual ComPort 8D8842A64955
// Windows: USB 串行设备 (COM6)
LOG("description:%s", info.description.c_str());
// ID
// mac: USB VID:PID=1234:5740 SNR=8D8842A64955
// lin: USB VID:PID=1234:5740 SNR=8D8842A64955
// win: USB\VID_1234&PID_5740&REV_0200
LOG("hardware_id:%s", info.hardware_id.c_str());
}
}
int main(int argc, char **argv) {
std::string portName = argc >= 2 ? argv[1] : "/dev/tty.usbserial-1460";
listPort();
SmartSerial smartSerial(portName, 115200, [](bool isOpen) {
// handle very likely on other thread
LOGI("on open handle: %d", isOpen);
});
// handle will on other thread
smartSerial.setOnReadHandle([&](const uint8_t* data, size_t size) {
static int count = 0;
auto str = std::string((char*)data, size);
LOGI("on read handle: size:%zu, data:%s, count=%d", size, str.c_str(), count++);
});
int count = 0;
for(;;) {
std::this_thread::sleep_for(std::chrono::seconds(1));
smartSerial.write("hello world");
LOG("write count: %d", ++count);
if (count == 3) {
smartSerial.open();
} else if (count == 6) {
smartSerial.close();
} else if (count == 8) {
break;
}
}
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/shen_hua_shan/SmartSerial.git
git@gitee.com:shen_hua_shan/SmartSerial.git
shen_hua_shan
SmartSerial
SmartSerial
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891