LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-22-2010, 11:29 AM   #1
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Rep: Reputation: 16
USB-RS232 serial driver


I am writing a simple driver to talk to an electronic chess board.
Actually I hacked a serial com emulator called com.c which uses
interrupt control.

I can receive data no problem.
I can write data into the tx buffer but it does not send!
I noticed if I close the program, the tx buffer is written
out and the chess board responds.

I think I need to flag an interrupt to transmit. Which I do not
know how to do in linux.
Or maybe it is because I am using a usb-rs232 cable adapter.

help..........don
 
Old 02-22-2010, 04:29 PM   #2
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Do you need to flush the buffer after writing perhaps?
 
Old 02-22-2010, 06:27 PM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
There are at least two possible things going wrong, here. Your program may be failing to send the data, the data may be incorrect and so does not elicit a response from the chess board. Have you tried a loopback cable, to see whether the data you think should be getting sent, actually is? It is possible that the chess board does not consider your message until it sees a line terminator (most commonly CF, LF or the two in combination). It is also possible that the serial port driver is not sending the data until it sees a line terminator, or has received a fflush().
--- rod.
 
Old 02-24-2010, 09:11 AM   #4
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
flushing

Quote:
Originally Posted by devnull10 View Post
Do you need to flush the buffer after writing perhaps?
Yeah, flush clears the tx buffer without transmitting it.

thanks........don
 
Old 02-24-2010, 09:36 AM   #5
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
loopback

Quote:
Originally Posted by theNbomr View Post
There are at least two possible things going wrong, here. Your program may be failing to send the data, the data may be incorrect and so does not elicit a response from the chess board. Have you tried a loopback cable, to see whether the data you think should be getting sent, actually is? It is possible that the chess board does not consider your message until it sees a line terminator (most commonly CF, LF or the two in combination). It is also possible that the serial port driver is not sending the data until it sees a line terminator, or has received a fflush().
--- rod.
Hi. Thanks for the tips. I will make a loopback cable, fflush() and I will check the CR LF options. I do believe the data is being sent (the chess board is 25 yrs old and does not echo). When I run my program with gtkterm on the same port, and I send a command with CR, nothing happens. When I close my program normally, there is the normal response from the chess board that is received and displayed by gtkterm. This happens repeatedly dozens of times. The chess board expects a CR before it responds.

I downloaded the gtkterm source, the danged serial function is 500 lines long! gtkterm works well, I think I will have to dissect it and find out what I am doing differently.

I think it might be the USB-rs232 adapter cable. Do you know where I can find the definitions for the data structure sent to open() & tcsetattr() functions?

Where do they document the programmers nuts & bolts stuff of linux?

thanks............don
 
Old 02-24-2010, 11:12 AM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
A few indispensable online references for serial comm's:
Serial HOWTO
Serial Programming HOWTO
Serial Programmming Guide for POSIX Operating Systems
Terminal Concepts in GNU/Linux

The first two references specifically will answer your questions about open() and tcsetatr().

--- rod.
 
Old 02-24-2010, 04:39 PM   #7
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
update

Quote:
Originally Posted by theNbomr View Post
A few indispensable online references for serial comm's:
Serial HOWTO
Serial Programming HOWTO
Serial Programmming Guide for POSIX Operating Systems
Terminal Concepts in GNU/Linux

The first two references specifically will answer your questions about open() and tcsetatr().

--- rod.
thanks Rod, I will check these refs.
About your previous suggestions:

* I made a loopback cable and tested it with gtkterm successfully.
When using the loopback cable the serial program is definitely NOT sending anything.

*fflush() man page says it will send whatever is in the tx buff, great.
However the fflush arg is type FILE *stream and my serial port file descriptor is type int. [btw I use open().]
Do you know of another way to open the serial port with a compatible file descriptor?
Man page says fflush(NULL) should flush all open streams, but when tested, the serial program still did not transmit the tx buffer.

*CR & LF I tested these options with gtkterm in hex mode. The chess board expects to see a CR (0x0d) at the end of the string.

*tcflush() man page says it flushes the txbuffer but does not send. I tried it anyway without success.

*tcflow() man page says this disables/enables output. It has no effect in my problem.

*tcdrain() This is interesting. Man page says this function waits until the tx buffer is empty. When I call tcdrain() after I call write(), my serial program hangs. This confirms the tx buffer is not being emptied/sent.

I need a way to tell the UART to send the contents of the tx buffer. I know this is done by flagging an interrupt. Any ideas on how to do this in Linux?

thanks...........don
 
Old 02-24-2010, 06:10 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Oops, you're right. Sorry about that. To flush, you call ioctl() on the filehandle with request = TCFLSH.

You may need to set some attributes to cause your serial port to ignore hardware handshaking/flow control.
Code:
    c_cflag &= ~CNEW_RTSCTS;
Read about the attributes (and ioctl()) you can set in the links I posted.

I don't think the use of interrupts is part of the problem you're trying to solve.

--- rod.

Last edited by theNbomr; 02-25-2010 at 12:23 PM.
 
Old 02-25-2010, 11:30 AM   #9
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
Hi Rod. Thanks for the help. I hate to bother you with details, but I can't fine the definition of TCFLSH. It is not in termios.h (as documented) or the raft of header files I have included.

later.........don
 
Old 02-25-2010, 12:00 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Simplest answer:

man ioctl

ioctl() is a generic way of talking to various drivers. It is used to modify the behavior of the driver itself, and the various requests depend on the specific driver. In this case, it is a request to flush the transmit queue.

Also, see http://www.easysw.com/~mike/serial/serial.html#5_1

--- rod.

Last edited by theNbomr; 02-25-2010 at 12:26 PM.
 
Old 02-26-2010, 12:27 PM   #11
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
OK found the byte representation for TCFLSH. I will need to study this problem more. That easysw guy is good but his article is not definitive. Actually none of what I have found is. I will crack this problem when I get home next week and try it on another linux box.

thank you kindly........don
 
  


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
Control of device names of USB to RS232 serial adapters hakelm Linux - Hardware 1 02-11-2008 07:49 PM
USB to RS232 (serial port)? - no I need to configure anything? Ultrus Linux - Hardware 12 08-16-2007 06:00 PM
USB>RS232 versus PCMCIA>RS232 jayhel Linux - Laptop and Netbook 2 08-04-2005 06:09 PM
USB - Serial RS232 Adapter blox Linux - Hardware 3 01-28-2005 10:58 AM
Serial RS232 problem Onyx^ Linux - Hardware 6 09-09-2004 11:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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