简体   繁体   English

C ++,Mac OS X 10.6.8中的串口问题

[英]Serial Port problems in C++, Mac OS X 10.6.8

I've got the following code running, and it runs all the way through. 我运行了以下代码,它一直运行。 However, it doesn't seem to wait for any input and the only output I get from the buffer is "\\377" I've been reading through Serial Programming Guide for POSIX Operating Systems and trying to use some of the suggestions provided there, but I think something in my configuration might be throwing it off. 但是,它似乎没有等待任何输入,我从缓冲区得到的唯一输出是“\\ 377”我一直在阅读POSIX操作系统的串行编程指南,并试图使用那里提供的一些建议,但我认为我的配置中的某些东西可能会把它扔掉。 Any thoughts would be appreciated. 任何想法将不胜感激。

#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;
}

You do not read from the serial port! 你没有从串口读取!

From the OSX fcntl manual page : 从OSX fcntl手册页

fcntl -- file control fcntl - 文件控制

The fcntl function is to control the file descriptor. fcntl函数用于控制文件描述符。 To read you need the read function. 要阅读,您需要read功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM