|
-
February 29th, 2008, 11:44 AM
#1
Serial port c++
Hi!
I'm trying to import readings from my GPS using c++ code, but have som difficulty. I have used the tutorial given at http://www.easysw.com/~mike/serial/serial.html, but it doesn't work.
I sometimes am able to receive some measurements, but then it can freeze the next time i run it. The program then freezes at read().
Further, the HW is all ok, as i get readings using cutecom. with the same settings.
int open_port(){
fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY);
if (fd == -1){
//Couldn't open port
perror("Open_port: Unable to open device");
cout << "Try running the config file again\n";
return -1;
}
fcntl(fd, F_SETFL, 0);
struct termios options;
// Loading current options
if (tcgetattr(fd, &options)){
perror("Could not load options ");
return -2;
}
// Setting 4800 baud rate
cfsetispeed(&options, B4800);
cfsetospeed(&options, B4800);
options.c_cflag |= (CLOCAL | CREAD); // Enabling reciever, setting local mode
options.c_cflag &= ~CSIZE; // Mask the character size bits
options.c_cflag |= CS8; // Select 8 data bits
options.c_cflag &= ~(PARENB | PARODD);
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CRTSCTS;
options.c_lflag |= (ICANON | ECHO | ECHOE);
options.c_iflag |= (INPCK | ISTRIP);
options.c_iflag &= ~(IXON | IXOFF | IXANY);
tcsetattr(fd, TCSANOW, &options);
char m_buf[4096];
int bytesRead=read(fd, m_buf, 4096);
return 1;
}
Any help would be appreciated.
-
February 29th, 2008, 12:07 PM
#2
Re: Serial port c++
Which OS and compiler are you using? There´s a fantastic free MFC library available at CSerialPort by PJ Naughter
- Guido
-
February 29th, 2008, 12:57 PM
#3
Re: Serial port c++
You have set the serial port into blocking mode. From the site you gave us:
Reading Data from the Port
Reading data from a port is a little trickier. When you operate the port in raw data mode, each read(2) system call will return the number of characters that are actually available in the serial input buffers. If no characters are available, the call will block (wait) until characters come in, an interval timer expires, or an error occurs. The read function can be made to return immediately by doing the following:
fcntl(fd, F_SETFL, FNDELAY);
The FNDELAY option causes the read function to return 0 if no characters are available on the port. To restore normal (blocking) behavior, call fcntl() without the FNDELAY option:
fcntl(fd, F_SETFL, 0);
This is also used after opening a serial port with the O_NDELAY option.
Since the site refers to POSIX and Unix, I think you are using a Unix-like OS. My experience is in Windows, so I probably can't help any further, and I am not completely certain that my explanation here is even correct.
Mike
-
March 3rd, 2008, 05:45 AM
#4
Re: Serial port c++
I am using linux and gcc-compiler.
Further, as far as i can tell, it is supposed to be in blocking mode, if I do not use it, it will not give any output...
-
March 3rd, 2008, 11:14 AM
#5
Re: Serial port c++
You told us that the problem is that "[t]he program then freezes at read()". We are saying that if the serial port is in blocking mode, then the code is designed to freeze at read(). It will wait at read() forever, until new data arrives.
If you don't want the code to freeze at read(), then you cannot use blocking mode.
Mike
-
March 3rd, 2008, 05:19 PM
#6
Re: Serial port c++
 Originally Posted by MikeAThon
You told us that the problem is that "[t]he program then freezes at read()". We are saying that if the serial port is in blocking mode, then the code is designed to freeze at read(). It will wait at read() forever, until new data arrives.
If you don't want the code to freeze at read(), then you cannot use blocking mode.
Mike
Or, you can use multiple threads, where the read thread is "frozen" waiting for data.
Viggy
-
March 3rd, 2008, 06:42 PM
#7
Re: Serial port c++
Ok, sorry about the last post, was in a bit of a rush. However, the thing is this:
I agree that the program should freeze whenever I do not get any information. However, the gps is sending information. How do I know this? Because when attempting to read the port from cutecom, I get readings as I should.
However when trying to execute the code, I usually get readings a few times, but when trying to run again, or for several loops, the program freezes. Thus, if I put it in non-blocking, the program will simply quit, and not read any information.
Further, as I have checked the source code for cutecom, it is also put in blocking mode, so it shouldn't be a problem there.
-
April 1st, 2009, 11:40 AM
#8
Re: Serial port c++
Hi !
I am having exactly the same problem with a laser telemeter.
I can receive some data for a few seconds and then the program freezes on read. Did you finally manage to understand what was happening ?? If you did please send me some help !
Have a good day
-
July 2nd, 2009, 06:29 AM
#9
Re: Serial port c++
 Originally Posted by MikeAThon
You have set the serial port into blocking mode. From the site you gave us:
Since the site refers to POSIX and Unix, I think you are using a Unix-like OS. My experience is in Windows, so I probably can't help any further, and I am not completely certain that my explanation here is even correct.
Mike
i also need similar solution....
Here is my question
Hi Friend...
Can anyone tell me how capture the data transferred over any port.(com, serial).
Is it possible remove/delete/drop the data/packets which is being passed over the ports before it reaches the destination.
In simple words how to block the ports which is used by a particular device(Bluetooh, USB,InfraRed) and created dynamically(We do not know the port no during coding).
if you have answer mail me to [email protected]
Any help will be appreciated.....
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|