LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 06-01-2010, 12:47 AM   #1
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Rep: Reputation: 16
rs232 serial port programming


Hi. I have been working on writing a small rs232 driver like minicom for months. I am almost there, I have the interrupt service routine running, I can read() ok. However when I write(), it returns the number of characters written, 1, but nothing is actually written out the port.

I researched termios, and they say that serial port programming is really messy in linux/unix.

I am probably not setting up the port parameters correctly, or my write() function is not doing what it is supposed to. As I said, write() is returning successful.

Other comm programs run ok (picocom & gtkterm) on my hardware. I am running knoppix/debian on an ancient computer. I saw other guys using slackware.

any suggestions?

thanks...........don
 
Old 06-01-2010, 03:43 AM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,286

Rep: Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322Reputation: 2322
Nobody ought to program serial ports anymore, imho. Most boxes only have one, there's only 115k baud on offer, and they are the easiest thing to blow. Laptops don't have earth, they have 0V, and someone brings one along and plugs it in sooner or later. 0V is brought to earth by your serial port circuitry and things are never the same after that :-(.

Check that that hasn't happened to you already - make sure other programs can write to your serial port. Are you writing under GPL? There's plenty out there already - copy it or call the appropriate library
 
Old 06-01-2010, 04:06 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,698

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Not sure what you mean by "Nobody ought to program serial ports anymore", There are still plenty of specialized devices plus most embedded single board computers use RS-232 for their console.

By read ok do you mean you can receive characters from another device? Without seeing your code it is difficult to suggest anything.

I am going to suggest this thread be moved to programming.

Edit:
Actually, I assume this thread is related to your chess board thread and consider it a duplicate post.

Last edited by michaelk; 06-01-2010 at 04:10 AM.
 
Old 06-01-2010, 04:20 AM   #4
XavierP
Moderator
 
Registered: Nov 2002
Location: Kent, England
Distribution: Debian Testing
Posts: 19,192
Blog Entries: 4

Rep: Reputation: 475Reputation: 475Reputation: 475Reputation: 475Reputation: 475
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 06-01-2010, 12:43 PM   #5
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 116Reputation: 116
I would imagine that the write() is putting the character in the send buffer, but depending on how the UART is set up, it may not actually send the character until you tell it to. I'd be looking into that possibility if I were you.

Look at the fildes and the line control functions.

Last edited by jiml8; 06-01-2010 at 12:45 PM.
 
Old 06-01-2010, 07:49 PM   #6
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
serial port hardware baud rate and flow control

I have not implemented serial port IO on Linux, but I have worked with serial ports on other systems. Anyone who uses the phrase "simple serial port interface" has not actually tried it.

I am writing this from memory. You should look at the standard to be sure which signals are inputs and which ones are outputs.

I assume your serial port is configured as DTE. Most computer serial ports are configured that way.

The DTE may require DSR and CTS before it will transmit on the TD serial line. You will be able to write one or two characters into the device as it loads the transmit buffer and possibly the transmit shift register. But without the enabling flow control, the port will not transmit. Then it will not accept any more characters.

You may be able to get past this by building a loop back plug to connect RTS to CTS. You may also want to loop back TD to RD. You may need to assert DCD by connecting it to the looped back RTS to enable the serial receiver.

You may find that it just simply works when you connect it to an external device. You may need a null modem if the other device is also DTE.

One nice feature of the old RS-232 signaling, which used DTL logic levels, is that connecting a driver to a driver does not damage the equipment. It won't work, of course, but it won't damage the drivers. That is NOT necessarily true with RS-422/RS-485 differential connections.

The IOCTL (I/O control) functions were provided to handle the "out of band signaling" functions to do things like set baud rates and configure and detect status of the control lines on RS-232 serial ports. The original PC had circuitry to over ride the auto enables on the serial port transmitter and receiver. Legacy systems still have all that stuff in the North Bridge chip where all the old ISA stuff still resides.

You may be able to add serial ports by reconfiguring and building a new kernel. That worked pretty well back in Slackware 9.x. That may also be a reference for driver source code.
 
Old 06-02-2010, 12:01 AM   #7
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
not a duplicate post

Quote:
Originally Posted by michaelk View Post
Not sure what you mean by "Nobody ought to program serial ports anymore", There are still plenty of specialized devices plus most embedded single board computers use RS-232 for their console.

By read ok do you mean you can receive characters from another device? Without seeing your code it is difficult to suggest anything.

I am going to suggest this thread be moved to programming.

Edit:
Actually, I assume this thread is related to your chess board thread and consider it a duplicate post.
This is for another device, an ARM based vision system.
I shelved that chessboard problem for now.
Yes I am receiving characters under interrupt control.
It doesn't hang the port or the process and closes cleanly.
I can post the code tomorrow. It is late.
thanks........don
code posted below

Last edited by ddlawrence; 06-02-2010 at 12:36 AM. Reason: attaching file
 
Old 06-02-2010, 12:08 AM   #8
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
touche

Quote:
Originally Posted by business_kid View Post
Nobody ought to program serial ports anymore, imho. Most boxes only have one, there's only 115k baud on offer, and they are the easiest thing to blow. Laptops don't have earth, they have 0V, and someone brings one along and plugs it in sooner or later. 0V is brought to earth by your serial port circuitry and things are never the same after that :-(.

Check that that hasn't happened to you already - make sure other programs can write to your serial port. Are you writing under GPL? There's plenty out there already - copy it or call the appropriate library
Yes I had fried the ground pin on another job doing just that.
I need a program to capture jpegs from a microcontroller. Most of the freeware I found was very hard to understand and modify for my use. I am finding that code was very cryptic for a bare metal guy like myself. What library are you talking about?
thanks.............don
 
Old 06-02-2010, 12:26 AM   #9
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
serial blues

Quote:
Originally Posted by ArthurSittler View Post
I have not implemented serial port IO on Linux, but I have worked with serial ports on other systems. Anyone who uses the phrase "simple serial port interface" has not actually tried it.

I am writing this from memory. You should look at the standard to be sure which signals are inputs and which ones are outputs.

I assume your serial port is configured as DTE. Most computer serial ports are configured that way.

The DTE may require DSR and CTS before it will transmit on the TD serial line. You will be able to write one or two characters into the device as it loads the transmit buffer and possibly the transmit shift register. But without the enabling flow control, the port will not transmit. Then it will not accept any more characters.

You may be able to get past this by building a loop back plug to connect RTS to CTS. You may also want to loop back TD to RD. You may need to assert DCD by connecting it to the looped back RTS to enable the serial receiver.

You may find that it just simply works when you connect it to an external device. You may need a null modem if the other device is also DTE.

One nice feature of the old RS-232 signaling, which used DTL logic levels, is that connecting a driver to a driver does not damage the equipment. It won't work, of course, but it won't damage the drivers. That is NOT necessarily true with RS-422/RS-485 differential connections.

The IOCTL (I/O control) functions were provided to handle the "out of band signaling" functions to do things like set baud rates and configure and detect status of the control lines on RS-232 serial ports. The original PC had circuitry to over ride the auto enables on the serial port transmitter and receiver. Legacy systems still have all that stuff in the North Bridge chip where all the old ISA stuff still resides.

You may be able to add serial ports by reconfiguring and building a new kernel. That worked pretty well back in Slackware 9.x. That may also be a reference for driver source code.
here is a man that knows his hardware!
Yes this is a simple interface, I have written about 20 drivers in the old days when I was a corporate slave. It is a major headache in linux. the cts/rts signaling I will check the flags for.
As for DTE, this works with picocom and gtkterm fully. I am probably overwriting a flag that I should not.
As for ioctl, that looks scary for a linux noob like me.
thanks.........don
 
Old 06-02-2010, 12:33 AM   #10
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
likely culprit

Quote:
Originally Posted by jiml8 View Post
I would imagine that the write() is putting the character in the send buffer, but depending on how the UART is set up, it may not actually send the character until you tell it to. I'd be looking into that possibility if I were you.

Look at the fildes and the line control functions.
Yes. that is probably what is happening. I have tried flushing and draining after every write() call to no avail. The line control flags are much more difficult to figure out. Much harder than the UART h/w it is servicing......
 
Old 06-02-2010, 12:53 AM   #11
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
code posted

here it is.
120 lines in a single file.
it should not be difficult to follow as I like to keep things simple.
mucho thanks for everyone's interest....don
Attached Files
File Type: txt serirq7.txt (4.4 KB, 29 views)
 
Old 06-02-2010, 05:42 PM   #12
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
Quote:
Originally Posted by business_kid View Post
Nobody ought to program serial ports anymore, imho. Most boxes only have one, there's only 115k baud on offer, and they are the easiest thing to blow. Laptops don't have earth, they have 0V, and someone brings one along and plugs it in sooner or later. 0V is brought to earth by your serial port circuitry and things are never the same after that :-(.

Check that that hasn't happened to you already - make sure other programs can write to your serial port. Are you writing under GPL? There's plenty out there already - copy it or call the appropriate library
I assume this was intended to provoke some heartfelt comments...

I'm glad I didn't take your attitude before programming all the serial ports that I have over the years. That would have left a lot of stuff unused, only because it has a serial interface.

I don't think there are many cases, if any at all, where programming of serial ports has caused problems that are electrical in nature. You are correct that misuse of the electrical connections between serial ports can cause mishap, I would be hard pressed to imagine any similar mishap caused by bad programming.

I agree, also, that the [RS-232] serial interface is nearing the end of its life. However, there will be good reason to interface computers to serial devices for many years to come, and many of those computers are going to run Linux. I think one could argue that Linux will eventually become the principle OS for supporting serial devices, if it isn't already.

Linux has the unfortunate position of using serial ports in applications for which the OS did not intend they be used. Linux, inheriting from Unix, intended serial ports to be used primarily as interfaces to terminals and modems. Since this was the primarly purpose behind the RS-232 specification, it made quite a bit of sense. Today, serial terminals are little used, but the OS support for serial ports is still highly oriented to that application. More often than not, we use serial ports these days to talk to 'devices', often smart devices that do a particular thing but need some easy way to provide input and output. In virtually all such cases, the application of the serial port is a corruption of the RS-232 spec, although in most cases it still works well enough for its purpose. I mention all of this, because it is sometimes helpful to understand the background for why a particular behavior or standard exists.

As is my custom in replying to questions about serial ports, I recommend to the original poster that he consult three good online references:

Serial HOWTO
Serial Programming HOWTO
Serial Programming Guide for POSIX Operating Systems

--- rod.
 
Old 06-03-2010, 09:08 PM   #13
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
right on

theNbomer is quite right.
Most micro devices have an rs232 interface.
I think it will never die because USB is complicated and overkill for most microcontrollers.

I got the serial program working btw........don
I'll attach the code in my next post. thanks
 
Old 06-03-2010, 09:29 PM   #14
ddlawrence
LQ Newbie
 
Registered: Aug 2007
Location: Vancouver Island, Canada
Distribution: Fedora Core
Posts: 24

Original Poster
Rep: Reputation: 16
BINGO!! Serial Driver works!!

Quote:
Originally Posted by ArthurSittler View Post
I have not implemented serial port IO on Linux, but I have worked with serial ports on other systems. Anyone who uses the phrase "simple serial port interface" has not actually tried it.
...
You may be able to get past this by building a loop back plug to connect RTS to CTS. You may also want to loop back TD to RD. You may need to assert DCD by connecting it to the looped back RTS to enable the serial receiver.
...
You got it right Arthur! The RTS/CTS bit was enabled. I should have known. It was the last of many hurdles. Thanks, I was hung up on a Linux serial driver for 4 months!!! I agree with your opinion of Linux serial i/o. It is a major pain. I will post it for anyone to use/hack on their project. There are a few more serial & interrupt references in it that provided useful code snippets.

Is slackware better for bare metal programming?

thanks all............don
Attached Files
File Type: txt tte.c.txt (6.4 KB, 23 views)
 
1 members found this post helpful.
Old 06-04-2010, 12:16 AM   #15
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
previous experience

yep. I had the same problem with my Kaypro 2X.
 
  


Reply



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
[SOLVED] serial port RS232 Aquarius_Girl Linux - Hardware 3 10-26-2009 05:55 PM
USB to RS232 (serial port)? - no I need to configure anything? Ultrus Linux - Hardware 12 08-16-2007 06:00 PM
console login through RS232/serial port ? Yalla-One Linux - Software 3 02-24-2005 04:12 PM
RS232 serial port programing blackzone Programming 2 08-11-2004 12:54 AM
serial port: RS232 RS574? blackzone Linux - Hardware 1 08-10-2004 04:15 AM

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

All times are GMT -5. The time now is 07:57 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