LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-19-2011, 12:25 AM   #1
shankar.489
Member
 
Registered: Jan 2011
Posts: 53

Rep: Reputation: 0
Unhappy Reg: LOOP BACK SETTINGS OF SERIAL PORT


hi Folks,

iam a newbie in linux system programming, current ly iam trying to write loop back(internal) test for serial for i have copied below code from internet. i really dont know where its going wrong(not able to read written data), its not working can anybody help in getting out of this..? please let me know if there is any issues with my code.

static int fd = 0;
int OpenAdrPort (char* sPortNumber);
int WriteAdrPort(char* psOutput);
int ReadAdrPort(char* psResponse, int iMax);
void CloseAdrPort();
// opens the serial port
// return code:
// > 0 = fd for the port
// -1 = open failed
int OpenAdrPort(char* sPortNumber)
{
char sPortName[64];
printf("in OpenAdrPort port#=%s\n", sPortNumber);
sprintf(sPortName, "/dev/ttyS%s", sPortNumber);
printf("sPortName=%s\n", sPortName);

// make sure port is closed
CloseAdrPort(fd);

fd = open(sPortName, O_RDWR | O_NOCTTY | O_NDELAY );
perror("open");
if (fd < 0)
{
printf("open error %d %s\n", errno, strerror(errno));
}
else
{
struct termios my_termios;
printf("fd is %d\n", fd);
tcgetattr(fd, &my_termios);
// NOTE: you may want to save the port attributes
// here so that you can restore them later
printf("old cflag=%08x\n", my_termios.c_cflag);
printf("old oflag=%08x\n", my_termios.c_oflag);
printf("old iflag=%08x\n", my_termios.c_iflag);
printf("old lflag=%08x\n", my_termios.c_lflag);
printf("old line=%02x\n", my_termios.c_line);
fcntl(fd,F_SETFL,FASYNC);
tcflush(fd, TCIFLUSH);

my_termios.c_cflag = B9600 | CS8 |CREAD | CLOCAL | HUPCL;
printf("%d %d %d %d\n",ICANON,ECHO,ECHOE,ISIG);
my_termios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
cfsetospeed(&my_termios, B9600);
tcsetattr(fd, TCSANOW, &my_termios);

printf("new cflag=%08x\n", my_termios.c_cflag);
printf("new oflag=%08x\n", my_termios.c_oflag);
printf("new iflag=%08x\n", my_termios.c_iflag);
printf("new lflag=%08x\n", my_termios.c_lflag);
printf("new line=%02x\n", my_termios.c_line);
} // end if
return fd;
}//end of open port
// writes zero terminated string to the serial port
// return code:
// >= 0 = number of characters written
// -1 = write failed
int WriteAdrPort(char* psOutput)
{
int iOut;
if (fd < 1)
{
printf(" port is not open\n");
return -1;
} // end if
iOut = write(fd, psOutput, strlen(psOutput));
perror("write:");
printf("errno write:%d\n",errno);
if (iOut < 0)
{
printf("write error %d %s\n", errno, strerror(errno));
}
else
{
printf("wrote %d chars: %s\n", iOut, psOutput);
} // end if
return iOut;
} // end WriteAdrPort

int ReadAdrPort(char* psResponse, int iMax)
{
int iIn;
printf("in ReadAdrPort iMax=%d\n", iMax);
if (fd < 1)
{
printf(" port is not open\n");
return -1;
} // end if
strncpy (psResponse, "N/A", iMax<4?iMax:4);
iIn = read(fd, psResponse, iMax-1);
perror("read");
if (iIn < 0)
{
if (errno == EAGAIN)
{
printf("no response\n");
return 0; // assume that command generated no response
}
else
{
printf("read error %d %s\n", errno, strerror(errno));
} // end if
}
else
{
psResponse[iIn<iMax?iIn:iMax] = '\0';
printf("read %d chars: %s\n", iIn, psResponse);
} // end if

return iIn;
} // end ReadAdrPort

// closes the serial port
void CloseAdrPort()
{
// you may want to restore the saved port attributes
if (fd > 0)
{
close(fd);
} // end if
} // end CloseAdrPort

int main(int argc, char *argv[])
{
char sCmd[254];
char sResult[254];
if (argc < 2 || argc > 2)
{
printf("adrserial needs 1 parameter for the serial port\n");
printf(" ie. use 'adrserial 0' to connect to /dev/ttyS0\n");
return 0;
} // end if
printf("Type q to quit.\n\n");
printf("arg:%s\n",argv[1]);
if (OpenAdrPort(argv[1]) < 0) return 0;
while (1)
{
int iSpot;

printf("?:");
gets(sCmd);
if (sCmd[0] == 'q' || sCmd[0] == 'Q') return 0;
iSpot = strlen(sCmd);
sCmd[iSpot] = 0x0d; // stick a <CR> after the command
sCmd[iSpot+1] = 0x00; // terminate the string properly

if (WriteAdrPort(sCmd) < 0) {
printf("write error\n");
return 0;
}
sleep(2); // give the ADR card some time to respond
if (ReadAdrPort(sResult,254) > 0)
{
printf("****Response is %s\n", sResult);
} // end if
} // end while

CloseAdrPort();

}//end of main




output:
[root@localhost hw]# ./a.out 0
Type q to quit.

arg:0
in OpenAdrPort port#=0
sPortName=/dev/ttyS0
open: Success
fd is 3
old cflag=00000cbd
old oflag=00000005
old iflag=00000500
old lflag=00008a20
old line=00
2 8 16 1
new cflag=00000cbd
new oflag=00000005
new iflag=00000500
new lflag=00008a20
new line=00
?:1
write:: Illegal seek
errno write:29 //errcode not matching any of error code in man page

wrote 2 chars: 1
in ReadAdrPort iMax=254
//here its blocking on read

any help will be greatly appriciated
thanks in adv
i have shorted pin2 and pin 3 with some core wire while testing code
regards
shankar
 
Old 08-19-2011, 10:19 AM   #2
Soadyheid
Senior Member
 
Registered: Aug 2010
Location: Near Edinburgh, Scotland
Distribution: Cinnamon Mint 20.1 (Laptop) and 20.2 (Desktop)
Posts: 1,671

Rep: Reputation: 486Reputation: 486Reputation: 486Reputation: 486Reputation: 486
I'm not sure where you're going with this, you say you've copied the code from the internet and then called it your code?

I'm afraid the programming is beyond me but if it's an internal loopback, you don't need an external pin2-to-pin3 loopback. You may need to know the type and registers within the UART(Do they still use UARTS for the serial chip? )so you know how to talk to it.
What do you get when you run your code anyway, I assume you expect to get some sort of keyboard to screen echo?

On the other hand... I could be talking a load of tosh!

Play Bonny!

Last edited by Soadyheid; 08-19-2011 at 10:20 AM. Reason: spellnig!
 
  


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
Serial loop back test doesnt work raypen Linux - Hardware 5 01-07-2012 06:06 AM
serial line loop back error in dial-up mahdif Linux - Networking 0 07-05-2007 01:19 PM
Real-time close loop control needs serial port communication areftaidi Linux - Software 6 01-25-2006 06:30 PM
What are my serial port settings? glenn69 Linux - Newbie 3 08-11-2004 10:37 PM
reg loop back adapater anilchow Linux - Security 1 10-02-2003 06:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration