c++ - Programming Linux serial timings -
im trying write small program gets values through serial port, , presents on screen. ive read several guides this, link.
now got in trouble believe timing, wrote 2 small pieces of code see if narrow problem down.
the code posted here 1 sender , 1 reciever, sender outputs 11:22 , receiever should recieve , present on screen. sort of this, lots of blank lines, , skipped letters. guess im missing fundamental, due lack of knowledge :)
please see code below: note, crude & indention got screwed men trying copy , paste gvim.. :)
receiver:
#include <iostream> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <errno.h> #include <termios.h> #include <stdio.h> using namespace std; int main() { int fd; //desc serial port. char in[20]; //char array data serial port struct termios options; tcgetattr(fd,&options); cfsetispeed(&options,b19200); cfsetospeed(&options,b19200); options.c_cflag |= (clocal | cread); options.c_cflag &= ~parenb; options.c_cflag &= ~cstopb; options.c_cflag &= ~csize; options.c_cflag |= cs8; //options.c_cc[vmin] = 5; tcsetattr(fd,tcsanow,&options); fd = open("/dev/ttys0", o_rdwr | o_noctty | o_ndelay); if (fd == -1) { cout << "could not open port." << endl; } else fcntl(fd,f_setfl,0); while(1){ read (fd, in,sizeof in); cout << in << endl; } }
sender: (same includes above)
int main() { int fd; struct termios options; tcgetattr(fd, &options); cfsetispeed(&options, b19200); cfsetospeed(&options, b19200); options.c_cflag |= (clocal | cread); options.c_cflag &= ~parenb; options.c_cflag &= ~cstopb; options.c_cflag &= ~csize; options.c_cflag |= cs8; options.c_oflag |=opost; tcsetattr(fd,tcsanow, &options); fd = open("/dev/ttys0",o_rdwr | o_noctty | o_ndelay); if (fd == -1) { cout << "cannot open port" << endl; } else fcntl(fd,f_setfl,0); while(1){ write(fd,"11:22\n",6); usleep(1000); //have little break between messages.. prob not neceserry. }
}
Comments
Post a Comment