LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices


Reply
  Search this Thread
Old 07-09-2019, 09:38 PM   #1
mike_canada
LQ Newbie
 
Registered: Jul 2019
Posts: 23

Rep: Reputation: Disabled
Linux Writes to serial at incorrect speed


My question is a bit similar to this one:

https://www.linuxquestions.org/quest...arbage-936622/

In my code, I have two file handles open. One for writing data to serial and One for reading data from serial. I used the same serial settings as the poster in the above question except I didn't use
CRTSCTS flag because my microcontroller hardware doesn't use flow control. I also didn't use the BAUDRATE flag because the compiler wouldn't compile it.


But I'm trying to get data flowing at a high speed. It seems to be ok at 19.2Kbps but I want 57.6Kbps and when I ran strace on my code, I get this:

Code:
open("/dev/ttyS0", O_RDONLY|O_NOCTTY|O_SYNC) = 3 <0.000109>
open("/dev/ttyS0", O_WRONLY|O_NOCTTY|O_SYNC) = 4 <0.000013>
sync()                                  = 0 <0.015698>
write(4, "@"..., 1)                     = 1 <0.000027>
write(4, "020102030000000000000000000002"..., 30) = 30 <0.000008>
write(4, "\n"..., 1)                    = 1 <0.000006>
sync()                                  = 0 <0.008830>
ioctl(3, TCFLSH, 0x2)                   = 0 <0.000012>
ioctl(4, TCFLSH, 0x2)                   = 0 <0.000049>
read(3, "\2?\3 "..., 4)                 = 4 <0.001097>
close(3)                                = 0 <0.000016>
close(4)                                = 0 <0.000045>
The only way the numbers make sense in triangle brackets is when I issue sync(). The microcontroller does put out correct data but the PC is timing it all wrong. Is there some way I can make it where the write function itself is the delayed function, because in my hardware setup, at 57.6Kbps speed, it should take about 173 microseconds to send out a byte. In the strace log above, it takes an average of 450 nanoseconds which doesn't make sense.

Is there a way to fix this? or is there a specialized linux serial driver I could use that can help me fix this? I tried fopen() in the past but got no better results.

Or what could even be better, is if theres a way I can put in a command to cause the entire system to lock up except the program until the serial I/O occurs then unlock the entire system.
 
Old 07-11-2019, 06:12 AM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,252

Rep: Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321
This could actually be messy.

REAL rs232 used fixed I/O & interrupts, used +/- 5V, and never went above 115,200 baud. I'll bet you haven't got that. Please detail what you have. There was also serial controller chips (Through hole & probably long obsolete). The MAX232 was one that springs to mind. It actually made a -5V rail from the +5V one. IIRC, rs232 hardware always reserved the right to slow itself down if it wasn't getting through. So although you set a high speed it may choose a slower one. How you interrogate the hardware is beyond me. IIRC, there was a byte received and you got the signal (pin high or low) and could read it.

Your wise course would be to try for a lib or a python module or something which looked after all this. And whoever chose rs232 to do anything is a serious case. Are you working in a museum?
 
Old 07-11-2019, 05:35 PM   #3
mike_canada
LQ Newbie
 
Registered: Jul 2019
Posts: 23

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by business_kid View Post
This could actually be messy.

REAL rs232 used fixed I/O & interrupts, used +/- 5V, and never went above 115,200 baud. I'll bet you haven't got that.
My computers do have the real RS232 port. Its just messy in the software land. I found out that linux isn't as real-time as DOS but on the other hand, DOS doesn't support alot of devices or applications.

Quote:
There was also serial controller chips (Through hole & probably long obsolete). The MAX232 was one that springs to mind.
And my hardware uses that exact IC. MAX232 with 1uF MLCC capacitors for the charge-pumps and a 10uF electrolytic for the reservoir capacitor.

I find it odd tho that when I run the screen program I get successful output from my hardware every time, but when I run my own code, that's when I have problems. Maybe the screen program uses different calls to access the port but I'm not sure.


Quote:
Your wise course would be to try for a lib or a python module or something which looked after all this. And whoever chose rs232 to do anything is a serious case. Are you working in a museum?
I'm doing a major project which eventually will become a business. Yes RS232 is old technology, but along with old comes lower prices and lower replacement costs should parts break. In fact, I even use fully-made radio modules that also require a uart running at 9600bps to setup.

In the meantime, I'm gonna find a good used mini-computer and install the lightest-weight linux that at least comes with the kernel, a shell, apache, RS232 driver and network driver. I don't care about X.
 
Old 07-11-2019, 06:24 PM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,679

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
The MAX232 is just a driver chip i.e. converts TTL to RS-232 signals. What real hardware UART you are using and operating system interrupt latency would determine the max baud rate. I know I played with 115.2 Kbps but it has been awhile.

True, linux is not a real time system unless you are running a RT kernel or a low latency kernel. I would think that 57.6Kps is possible.

Have you looked at any programming howtos?
http://tldp.org/HOWTO/Serial-Program...WTO/index.html
 
Old 07-12-2019, 02:02 PM   #5
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware, Slarm64 & Android
Posts: 16,252

Rep: Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321Reputation: 2321
I did a bit of RS232 & linux in my day, too. Linux multitasks, so cannot be entrusted with timing duties. Industrial PCs will do it, but they're mired in the last millenium.
 
  


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
Internet Speed shows incorrect result? SHENGTON Linux - Networking 11 02-10-2011 12:54 AM
RS232 serial port errors when performing mutliple writes riffy Linux - Newbie 0 07-11-2006 12:12 PM
Incorrect Mouse, Incorrect Keymap, and Trapped in X Kenji Miyamoto Debian 8 08-24-2005 02:42 PM
incorrect cpu speed detected with pII 333, non-mobile processor maku Linux - Hardware 0 12-30-2004 12:59 PM
SNMP Reports Incorrect Speed meshcurrent Linux - Networking 1 02-11-2004 08:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware

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

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