LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
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
 
LinkBack Search this Thread
Old 06-05-2006, 11:52 PM   #1
mta
LQ Newbie
 
Registered: Jun 2006
Posts: 2

Rep: Reputation: 0
problem with serial port in loopback mode .. (termios)


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)
 
Old 06-06-2006, 11:56 AM   #2
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
Where is your code to set the serial port options?
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
single-user mode through serial port? Buurzgoth Linux - Software 5 01-22-2006 04:17 AM
Serial loopback max76 Programming 2 10-14-2004 10:24 AM
serial sync loopback error? mjwnijkamp Linux - Laptop and Netbook 0 05-10-2004 10:09 AM
Serial port problem ruchika Linux - Networking 0 08-15-2003 04:18 PM
Problem with Serial port mritunjay Linux - Networking 1 04-22-2002 01:59 PM


All times are GMT -5. The time now is 02:50 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
Open Source Consulting | Domain Registration