C 中的串行端口问题,Mac OS X 10.6.8
Serial Port problems in C++, Mac OS X 10.6.8
我已经运行以下代码,并且它一直运行。但是,它似乎没有等待任何输入,而我从缓冲区中获得的唯一输出是" 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
手册页:
fcntl
-文件控制
fcntl
功能是控制文件描述符。要阅读您需要read
函数。
相关文章:
- libprotobuf 检查在 Mac OS 上执行程序时失败
- C++读取用户输入而不按回车键(Mac OS X),与Turbo Pascal中的读取键相同
- 如何在 MAC OS c/c++ 中阻止 USB 存储设备
- Mac os x app bundle 在下载并运行时崩溃,但在终端或更改 Info.plist 时运行良好
- const 如何防止写入特定内存 [Clang, Mac OS]
- 如何在 Mac OS 上安装 boost-mpi 及其对 clang 的依赖关系?
- 在 Mac OS 中将 QT 与 CMAKE 集成
- Homebrew OS X OpenCV - 在另一台Mac上运行时寻找dylib
- 从我的2d数组中的一列返回的字符串值被压缩为一个字符串(在Mac os上打开Windows txt文件)
- VK_ERROR_INCOMPATIBLE_DRIVER with Mac OS and Vulkan MoltenVK
- 我无法在C++(Clion,Mac OS)中运行Gurobi
- 如何在Mac OS的发布版本中包含Qt库?
- 如何将C++项目从 32 位迁移到 64 位以支持 MAC OS 10.14(莫哈韦)?
- 在 Mac OS x 中制造期间出现错误未知类型名称"constexpr"
- 无法在 Mac OS X 上链接静态资源库
- 在Mac OS X Mountain Lion(或更高版本)和Linux上使用X11/Xlib.h
- Mac OS c++ Link OpenCV and libtiff
- 使用 c++ 在 Mac OS X 上启动"new email"窗口
- 在 Mac OS 上运行 x11
- 为什么 Mac OS 指定的 TCP 端口不侦听?