LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Reg: LOOP BACK SETTINGS OF SERIAL PORT (https://www.linuxquestions.org/questions/linux-newbie-8/reg-loop-back-settings-of-serial-port-898210/)

shankar.489 08-19-2011 12:25 AM

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

Soadyheid 08-19-2011 10:19 AM

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? :scratch: )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! :hattip:


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