惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - ifdef

wxRichTextCtrl移动到最后一行显示 MFC的DLL工程加载异常的问题 VC FormView 上的CEdit不能响应复制粘贴按键(CTRL+C和CTRL+V)的问题 win10环境安装vs2015的问题:缺少JavaScript_ProjectSystem.msi和JavaScript_LanguageService.msi等等 记录:LINUX下,编译一个调用了OPENCV库的程序出错的解决方法 解决:小米11导入其他手机的VCF文件后,电话簿不完整的问题 调用libhv的HTTP客户端给服务器发送图片失败或图片不完整的问题 linux下查看usb设备的端点、VID/PID等信息 VS2010编译静态链接MFC的OCX遇到的问题:nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12 已经在 LIBCMTD.lib(dllmain.obj) 中定义 linux C++中宏定义的问题:error: unable to find string literal operator ‘operator""fmt’ with ‘const char [4]’, ‘long unsigned int’ arguments 新装vs2010的问题:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏 windows下删除虚拟串口的方法,以及解决串口使用中,无法变更设备串口号的问题 error C4996: 'stricmp': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _stricmp. 给linux shell 添加ll命令支持 编译WDF驱动项目时,缺少WDKConversion\PreConfiguration.props文件的问题 - ifdef - 博客园 error C3867: “ClassA::OnFuncA”: 函数调用缺少参数列表;请使用“&ClassA::OnFuncA”创建指向成员的指针 VS2010的_vscprintf函数在BCB6下的替代方法vsnprintf 逐个删除网页输入框的下拉提示 升级win10 1903版后,vmware打开虚拟机黑屏的解决办法
LINUX下USB转串口编程中的一点心得
ifdef · 2020-06-09 · via 博客园 - ifdef

串口设备的名字

普通物理串口是ttySx,比如ttyS0, ttyS1等,USB转串口的设备名为ttyUSBx,比如ttyUSB0, ttyUSB1等

设置读写RAW模式

这一步非常重要,不设置就收不到数据,一个字节都没有,下面是设置RAW模式的代码

int SetRawMode(int fd)
{
  struct termios opt;
  opt.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
  opt.c_oflag &= ~OPOST;
  opt.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
  opt.c_cflag &= ~(CSIZE|PARENB);
  opt.c_cflag |= CS8;
  
  if (tcsetattr(fd, TCSANOW, &opt)!=0) {
    fprintf(stderr, "set rawmode fail\n");
    return -1;
  } else {
    fprintf(stderr, "set rawmode ok\n");
    return 0;
  }
}

读写超时控制

尤其是读操作,否则经常收不全数据,下面是读的示例代码

int ReadFix(int fd, char* buffer, int FixLen, int Timeout)
{
  struct timeval timeout;
  fd_set readfds, writefds;
  int index = 0, rt = 0, s = 0, ms = 0;

  s = Timeout/1000;
  ms = Timeout%1000;
  timeout.tv_sec = s;
  timeout.tv_usec = ms*1000;
  FD_ZERO(&readfds);
  FD_SET(fd, &readfds);
  switch(select(fd+1, &readfds, NULL, NULL, &timeout)) {
  case 0:
    fprintf(stderr, "read timeout\n");
    break;
  case -1:
    fprintf(stderr, "read fail\n");
    break;
  default:
    if (FD_ISSET(fd, &readfds)) {
      while (1) {
        rt = read(fd, buffer+index, FixLen-index);
        if (rt<=0) {
          break;
        }
        index += rt;
        if (index >= FixLen) break;
      }
    }
    break;
  }
  
  return index;
}

调用流程示例

int main(int argc, char* argv[])
{
  char szSendBuf[128], szRecvBuf[128];
  int rt = 0;
  
  OpenSerial();
  
  SetRawMode();
  rt = WriteFix(szSendBuf, nSendLen, 5000);
  rt = ReadFix(szRecvBuf, 20, 5000);
  if (rt<=0) {
    // 读失败
    return -1;
  }
  if (rt<20) {
    // 未读完,继续读,此处最好循环处理
    rt = ReadFix(szRecvBuf+rt, 20-rt, 5000);
  }
  CloseSerial();
  return 0;
}