2012-10-23 63 views
0

我已经运行了以下代码,并且它一直运行。但是,它似乎并没有等待任何输入,而且我从缓冲区获得的唯一输出是“\ 377”,我一直在阅读通过POSIX操作系统的串行编程指南并尝试使用其中提供的一些建议,但我认为在我的配置中可能会抛弃它。任何想法将不胜感激。C++,Mac OS X中的串行端口问题10.6.8

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

回答

2

你不从串口读取!

从OSX fcntl manual page

fcntl - 文件控制

fcntl功能是控制文件描述符。阅读你需要的功能read