> Hello everyone:
> I am working on a school project where I need to read some data from a
> Basic Stamp micrprocessor via the serial port. I have the microprocessor
> connected to my notebook from the serial (microprocessor) to the USB in
> (notebook side which does not have a 9 pin serial port) via a serial to
> usb adaptor (is this an issue?). I wrote a piece of software that sends
> some data to the serial port from the Basic Stamp microprocessor, this
> part I have tested and I know it works with test data. Now I need to bring
> it in to the notebook via the serial (usb) port.
> I need some pointers on where to get started. I have already found some
> info using c and c++ but it is a bit over my head. Does anyone has or
> knows of a simple tutorial that can take me through the basics of serial
> programming so I can begin to understand the more advanced stuff? Do any
> of you have any other ideas on how to bring the data into the notebook?
> ANY help in this matter will be greatly appreciated.
> Thanks in advance,
> MR
> I am using Fedora Core 3 Test 2
Here are a few functions I use for serial setup. This is used for talking
to a PIC, so should be applicable. I'm no expert in this, so if people see
errors or bad code, please let me know.
/* open and configure the port */
int openport(char *portname){
struct termios oldtio, newtio;
char *error = 0;
int fd;
/* open read/write, non-controlling, non-blocking */
fd = open(portname, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1){
fprintf(stderr, "Unable to open %s: ", portname);
perror(error);
exit(-1);
}
tcgetattr(fd, &oldtio); //save current port settings
bzero(&newtio, sizeof(newtio)); //clear struct for new settings
newtio.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VINTR] = 0; /* Ctrl-c */
newtio.c_cc[VQUIT] = 0; /* Ctrl-\ */
newtio.c_cc[VERASE] = 0; /* del */
newtio.c_cc[VEOF] = 4; /* Ctrl-d */
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
newtio.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
newtio.c_cc[VSWTC] = 0; /* '\0' */
newtio.c_cc[VSTART] = 0; /* Ctrl-q */
newtio.c_cc[VSTOP] = 0; /* Ctrl-s */
newtio.c_cc[VSUSP] = 0; /* Ctrl-z */
newtio.c_cc[VEOL] = 0; /* '\0' */
newtio.c_cc[VREPRINT] = 0; /* Ctrl-r */
newtio.c_cc[VDISCARD] = 0; /* Ctrl-u */
newtio.c_cc[VWERASE] = 0; /* Ctrl-w */
newtio.c_cc[VLNEXT] = 0; /* Ctrl-v */
newtio.c_cc[VEOL2] = 0; /* '\0' */
/* flush the port, and apply the new settings now */
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &newtio);
return (fd);
Quote:}
Then you can write to it with write(), but my wrapper function for that is
too specific to be interesting to you.
Now here is my reading function. It takes a timeval for the timeout which
is previously declared and initialized like this:
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 600000;
Also note that my code was for reading multiple ports. If fd is -1, it
reads all the ports I have setup. I'm leaving this in because you asked
for more advanced stuff, but be aware of the multiple port thing. Also
note that my protocol for communicating with the pic is comprised of 1
character commands. That is, I'm reading 1 character at a time because
that's all I care about, not because I have to.
/* read a character off the port */
int readcommand(int fd, struct timeval* tv){
char inbuf[2];
int ret, max_fd;
fd_set set;
inbuf[1] = 0;
FD_ZERO(&set);
// read from both descriptors
if(fd == -1){
FD_SET(pic_fd, &set);
FD_SET(card_fd, &set);
if(card_fd > pic_fd)
max_fd = card_fd;
else
max_fd = pic_fd;
}
// read from a specific descriptor
else{
FD_SET(fd, &set);
max_fd = fd;
}
// blocking select waits for input
if(!select(max_fd+1, &set, 0, 0, tv)){
return 0; //timed out
}
if(errno == EINTR) //select returned due to signal
return -1;
// read from the PIC
if(FD_ISSET(pic_fd, &set)){
if(read(pic_fd, inbuf, 1) < 0){
perror("read() failed in readcommand() for the pic");
}
return inbuf[0];
}
// read from the card reader
if(FD_ISSET(card_fd, &set)){
if(read(card_fd, inbuf, 1) < 0){
perror("read() failed in readcommand() for the card reader");
}
return inbuf[0];
}
return -1;
Quote:}