LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-15-2009, 01:20 AM   #1
nathan
LQ Newbie
 
Registered: Aug 2009
Location: california
Distribution: fedora
Posts: 21

Rep: Reputation: 15
expecting Serial port output to a buffer


Dear All,

with the following code i am expecting modem out put to buffer.
here its not responding.
Is it delay problem ??

kindly suggest the same.

char buffer[255]; /* Input buffer */

/* send an AT command followed by a CR */
write(fd, "AT\r", 3);
sleep(2);
printf("res1= %d\n",res);

/* read characters into our string buffer until we get a CR or NL */
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
printf("nbytes = %d\n",nbytes);
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
/* nul terminate the string and see if we got an OK response */
*bufptr = '\0';
if (strncmp(buffer, "OK", 2) == 0)
return (0);


Thanks in Advance
Nathan
 
Old 09-16-2009, 09:20 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
There is absolutely nothing in your (unformatted) code that shows anything to do with serial communications other than what may be a Hayes-compatible "AT" command being sent to an arbitrary file or device. Your input buffer handling seems very cumbersome. Show us the actual serial comm's related code (formatted in [CODE] tags, please).
--- rod.
 
Old 09-16-2009, 11:32 PM   #3
nathan
LQ Newbie
 
Registered: Aug 2009
Location: california
Distribution: fedora
Posts: 21

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by theNbomr View Post
There is absolutely nothing in your (unformatted) code that shows anything to do with serial communications other than what may be a Hayes-compatible "AT" command being sent to an arbitrary file or device. Your input buffer handling seems very cumbersome. Show us the actual serial comm's related code (formatted in [CODE] tags, please).
--- rod.
Hi,

Thanks for your reply,

and here is the code..


#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/time.h>
#include <signal.h>

#define BAUD_RATE 115200

int main()
{

int fd;
long baud;
struct termios options;

/* open the port */
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fcntl(fd, F_SETFL, 0);

if(fd < 0)
{
printf("\n fail to open serial port");
return 0;
}
else
{ printf("\n success in opening serial port\n"); }

/* get the current options */
tcgetattr(fd, &options);

switch(BAUD_RATE)
{
case 2400:
baud = B2400;
break;
case 9600:
baud = B9600;
break;
case 19200:
baud = B19200;
break;
case 38400:
baud = B38400;
break;
case 115200:
baud = B115200;
break;
default:
break;
} // end of switch

/* set raw input, 1 second timeout */
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_oflag &= ~OPOST;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 0;

tcflush(fd, TCIFLUSH); /* flush the buffer */

/* set the options */
tcsetattr(fd, TCSANOW, &options);

/* initializing modem */

init_modem(fd);

return 0;

} // end of main

/////////////////////////////////////////////////////////



int /* O - 0 = MODEM ok, -1 = MODEM bad */
init_modem(int fd) /* I - Serial port file */
{
char buffer[255]; /* Input buffer */
char *bufptr; /* Current char in buffer */
int nbytes; /* Number of bytes read */
int tries; /* Number of tries so far */

for (tries = 0; tries < 1; tries ++)
{
/* send an AT command followed by a CR */
if (write(fd, "AT\r", 3) < 3) // writinig to serial port
continue;

usleep(10000); // delay to get response to serial port it is in micro sec
/* read characters into our string buffer until we get a CR or NL */
bufptr = buffer;
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0) // reading serial port data
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
*bufptr = '\0';
if (strncmp(buffer, "OK", 2) == 0)
return (0);

}

Here i am expecting the serial port response (string) to a buffer, and string compare with reference data.

If it is OK send next command to serial port

else ERROR send same command to serial port.

The response from modem is always OK or ERROR.

Kindly suggest me

thanks in advance
Nathan
 
Old 09-22-2009, 11:08 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Your code is very hard to read when posted as commentary text.
You have not tested the file descriptor for a successful open() of the serial port. Typically, the serial port has non-user access permissions. Check this first.
Since you have apparently copied the bulk of the code from the Serial Programming Guide for POSIX Operating Systems, it is interesting that you chose to use
Code:
    options.c_cc[VMIN] = 1;
    options.c_cc[VTIME] = 0;
as opposed to the usual
Code:
    options.c_cc[VMIN]  = 0;
    options.c_cc[VTIME] = 10;
Have you used the serial port to communicate with the modem on this platform via some known-good software, such as minicom, C-Kermit, or other serial comm's software?
Have you tried a loopback connector to jumper the Tx pin back into the Rx pin of the serial port? This will eliminate the modem as a possible problem, and verify that the serial port is actually being driven.

--- rod.
 
Old 09-22-2009, 02:37 PM   #5
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,230

Rep: Reputation: 173Reputation: 173
WTF is this?:
Code:
switch(BAUD_RATE)
{
case 2400:
baud = B2400;
break;
case 9600:
baud = B9600;
break;
case 19200:
baud = B19200;
break;
case 38400:
baud = B38400;
break;
case 115200:
baud = B115200;
break;
default:
break;
} // end of switch
why are you putting a B before the number?
that simply wont work
if you need the B try 'B'<<32|n where n is the number
 
Old 09-22-2009, 03:29 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by smeezekitty View Post
WTF is this?:
Code:
switch(BAUD_RATE)
{
case 2400:
baud = B2400;
break;
case 9600:
baud = B9600;
break;
case 19200:
baud = B19200;
break;
case 38400:
baud = B38400;
break;
case 115200:
baud = B115200;
break;
default:
break;
} // end of switch
why are you putting a B before the number?
that simply wont work
if you need the B try 'B'<<32|n where n is the number
A bit unconventional, to be sure (BAUD_RATE is a constant, as opposed to the usual variable used in a switch() statement), The values prefixed with B are standard constants used for the purpose of setting bit rates, and are defined in one of the standard header files for the purpose. The actual values of these constants may not be the bit rates, but instead may be some values written to registers used in the hardware implementation, such as an 8250 family UART Baud Rate Divisor register.
--- rod.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Serial port input/output error john_erlandsson Linux - Newbie 9 07-27-2009 08:47 AM
Serial PIC programmer, but no serial output on my laptop simopal6 Linux - Hardware 1 08-21-2008 03:04 PM
serial port not filling buffer entirely windell Programming 1 08-05-2008 10:12 AM
Query buffer with serial port /dev/ttyS0 mmcshmi11 Linux - Hardware 2 07-08-2008 04:24 PM
Serial Port Buffer Overrun damiendusha Linux - Hardware 1 06-22-2006 02:56 AM


All times are GMT -5. The time now is 07:07 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration