Hi all,
a simple problem but has been troubling me since long..
trying to test serial port in Loopback mode..

help would be appreciated..
( i wonder if loopback is supported atall in linux!)
-------------------
c0de
------------------------------
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include<termios.h>
#define Xuint8 unsigned char
Xuint8 WriteString[255];
Xuint8 ReadString[255];
main()
{
int fd_tty2;
Xuint8 BytesWritten = 0;
Xuint8 WriteChars = 0;
Xuint8 BytesRead = 0;
Xuint8 Index;
Xuint8 Loop;
Xuint8 TcDrn;
fd_tty2 = open("/dev/ttyS0", O_RDWR);
if(fd_tty2 < 0)
{
printf("Cannot open the COM2 Port\n");
return -1;
}
Loop = 0;
do
{
printf("Loop %d\n", Loop);
for(Index = 0; Index < 1; Index++)
WriteString[Index] = '1' + Loop;
WriteString[Index] = 0x0D;
Loop++;
WriteChars = Index + 1;
BytesWritten = write(fd_tty2, WriteString, WriteChars);
TcDrn = tcdrain(fd_tty2);
sleep(1);
BytesRead = read(fd_tty2, ReadString, 255);
printf("\n No of chars read from port= %d\n", BytesRead);
for(Index = 0; Index < BytesRead; Index++)
{
printf("%c", ReadString[Index]);
}
}while ( Loop < 2);
return 0;
}
---------------
Output:
--------------
# ./uartl
Loop 0
No of chars read from port= 2
1
Loop 1
<Ctrl-C> -->stops here.. after 'write' STRACE shows it stops after an ioctl after write
ioctl(3,TCSBRK,<unfinished>
--------------------
If 'tcdrain' is removed then
# ./uartl
Loop 0
No of chars read from port= 2
1 -->prints 1 as expected (ie, wrote 1 readback 1)
Loop 1
No of chars read from port= 2
1 -->prints 1 where 2 is expected(ie, wrote 2 but readback 1)