I encountered the same situation as Mikester. I have to open minicom first of all. Then run my own program on Beaglebone. Or the system will show ": Resource temporarily unavailable". It seems like I have to set ttyO0 free then I can use the read function.
My program can write "HelloBeaglebone!" to my laptop perfectly but it just cannot read.
I am looking to your kind reply.
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h> // using the termios.h library
int main(){
int file, count;
if ((file = open("/dev/ttyO0", O_NDELAY | O_RDWR | O_NOCTTY ))<0){ //| O_NONBLOCK
perror("UART: Failed to open the file.\n");
return -1;
}
// fcntl(file, F_SETFL, fcntl(file, F_GETFL ) & ~O_NONBLOCK);
struct termios options; //The termios structure is vital
tcgetattr(file, &options); //Sets the parameters associated with file
options.c_cflag = B9600 | CREAD | CS8 | CLOCAL;//
// Set up the communications options:
// 9600 baud, 8-bit, enable receiver, no modem control lines
options.c_iflag = IGNPAR | ICRNL; //ignore partity errors, CR -> newline
tcflush(file, TCIFLUSH); //discard file information not transmitted
tcsetattr(file, TCSANOW, &options); //changes occur immmediately
unsigned char transmit[18] = "Hello BeagleBone!"; //the string to send
if ((count = write(file, &transmit,18))<0){ //send the string
perror("Failed to write to the output\n");
return -1;
}
usleep(100000); //give the Arduino a chance to respond
char receive[4]; //declare a buffer for receiving data
for (int i=0; i<2; i++){
if ((count = read(file, (void*)receive, 4))<0){ //receive the data
perror("Failed to read from the input\n");
return -1;
}
if (count==0) printf("There was no data available to read!\n");
else {
printf("The following was read in [%d]: %s\n",count,receive);
}
}
close(file);
return 0;
}
在 2013年11月29日星期五 UTC-3:30下午7:57:44,Dave Hylands写道: