c++ - C++、Mac OS X 10.6.8 中的串口问题

标签 c++ macos serial-port

我已经运行了以下代码,并且它一直运行。但是,它似乎不等待任何输入,我从缓冲区获得的唯一输出是“\377”我一直在阅读 POSIX 操作系统的串行编程指南并尝试使用那里提供的一些建议,但我认为我的配置中的某些东西可能会把它扔掉。如有任何想法,我们将不胜感激。

#include <iostream>
using namespace std;
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */


int open_port() {
    int fd; //Descriptor for the port
    fd = open("/dev/tty.usbmodemfa141", O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
        cout << "Unable to open port. \n";
}
else {
        cout << "Port opened.\n";
}
cout << "Descriptor in open:";
cout << fd;
cout << "\n";
return fd;
}

int configure_port (int fd) {
struct termios options;

tcgetattr(fd, &options);
cfsetispeed(&options, B9600);
cfsetospeed(&options, B9600);

options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag |= (CLOCAL | CREAD);
tcsetattr(fd, TCSANOW, &options);

cout << "Port configured.\n";

return (fd);
}

void read_data(int fd) {
cout << "Reading data from: ";
cout << fd;
cout << "\n";

char buffer;
buffer = fcntl(fd, buffer, 0);
cout << "Buffer: ";
cout << buffer;
cout << "\n";
}

int main (int argc, char * const argv[]) {
int fd; //Descriptor for the port

fd = open_port();
configure_port(fd);
cout << "Descriptor: ";
cout << fd;
cout<< "\n";
read_data(fd);
cout << "Data read.";

return 0;
}

最佳答案

你没有从串口读取!

来自 OSX fcntl manual page :

fcntl -- file control

fcntl函数是控制文件描述符的。要阅读,您需要 read功能。

关于c++ - C++、Mac OS X 10.6.8 中的串口问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13023250/

相关文章:

c++ - 在安装了 VS08 的旧 XP 中使用最新的 dll 函数

c++ - 函数对象方法对函数指针安全吗?

c - mac 'purge' 命令的真正作用是什么?

perl - 在 perl 中将原始十六进制转换为可读的十六进制?

c - Linux termios在串行端口read()之后修改第一个字符

c++ - 字符串相等时比较不相等

c++ - R 崩溃/中止使用带有 NA 输入的 Rcpp

java - Mac OS X 上的 Java 安装在哪里?

python - CSV 模块 - 'for row in reader' - 在 Mac 和 Windows 之间

java - 当端口存在时获取 javax.comm.NoSuchPortException