LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 02-02-2018, 09:45 AM   #1
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Rep: Reputation: Disabled
serial program design question


I am writing a simple C program (only running on a turnkey lamp box) that reads data from multiple com ports, translates it and sends it out another port. My plan is to start a thread for monitioring each port.

My question is, should I be concerned about receiving data from two ports similtaneously which would cause it to be written out at the same time.

Ex. ttyS0 and ttyS1 each receive a 5 byte message at the same time and then try and send the translated data out ttyS3.

My gut tells me shouldn't be a problem bc each thread will be getting a slice of processing time so the call to write out the data in theory could never happen at the same time but thought I would see if anyone else has more insight than I do.

Tks

Last edited by crabbit; 02-02-2018 at 09:46 AM.
 
Old 02-02-2018, 09:55 AM   #2
hydrurga
LQ Guru
 
Registered: Nov 2008
Location: Pictland
Distribution: Linux Mint 21 MATE
Posts: 8,048
Blog Entries: 5

Rep: Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925Reputation: 2925
I don't have insight, but I would suggest that you could develop a locking mechanism since, for example, all of a "chunk" of data may not be sent out of a port in one go. So, ttyS0 would lock the port for its own use, send its chunk out, and then unlock the port after using it. Of course, such port locking might already be a function of the operating system.
 
Old 02-02-2018, 11:35 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,881
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Are there some sort of 'messages'? If yes, you might want them not to get mixed, so you might wish to use some serialization.
 
Old 02-02-2018, 11:46 AM   #4
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
I should have been more clear. The ports that I am reading from are locked. I am concerned about writing to the one output port from two threads at the same time. The order of the messages to the output port is not important. One thought was to create a thread for writing and queue up the messages for writing but if it's not possible to actually write them at the same time I would not go through the trouble.
 
Old 02-02-2018, 12:38 PM   #5
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931Reputation: 4931
I have several blog entries. They are very old, however serial programming and system V programming is similarly old, but well proven. They are all in the C language.

If you take the time, you'll see an entire architecture where there is a daemon that creates child processes which can then parse serial interfaces, and finally communicate with each other, as well as be monitored by the daemon to be restarted if they failed for some reason.

USB Receive and Rapid Parse, GPS Example

Creating a daemon to launch and monitor your processes

Using PIPES for Interprocess Communications

Select(2) and Why I Use It

How to kill those Zombies - by which I mean zombie processes.

Edit Addition:
  • I don't necessarily prefer threads, but do use them. These examples are multiple processes and the performance was fine for several serial links
  • Write your receiver and parser to be re-entrant and never make assumptions that you're going to get exactly an entire packet or frame during a single receive, in fact for me this rarely occurred.
  • Yes it is helpful to minimize copying data, what I typically do is think about what I need to do and also realize that if I put information into a buffer, do not overwrite it or expire it, use a semaphore to protect it when various parts are running and operating on the data, that I do not need to copy. But ultimately there usually is a ton of data within a serial packet where some smaller amount is really needed. Such as my example of GPS, you probably want coordinates and time+date, but little else.
Just some added tips.

Last edited by rtmistler; 02-02-2018 at 12:43 PM.
 
2 members found this post helpful.
Old 02-05-2018, 03:04 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,881
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Short writes aren't likely to be mixed with other writes (i.e. they are atomic). The only open points are the meaning of "short" and "likely". (It would be good to find some standard (e.g. POSIX) that clarifies this.)
 
Old 02-10-2018, 10:24 AM   #7
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
My project requirements have grown to being able to handle data beginning with <STX> (0x02) and ending with <ETX> (0x03). I do not have the device that will be sending the data.

Any ideas on how to test?

I am developing in a virtual box (lamp), running my program, then opening up putty on my host OS using a pipe that is set up as com1 for the VM and configured to use CP437 so I can send alt+nnnn values. I suspect that putty is not sending characters as they are typed, but rather waiting until it sees <ETX>.

I've tried writing to /dev/ttyS0 using the following with no success

echo -n -e '\x02\x30\x48\x03' > /dev/ttyS0


Here are my flags I am setting when opening tty in my program.

newtio.c_oflag = 1;
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;
 
Old 02-10-2018, 10:52 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
doesn't threads have a means to sleep and wake them up, read from one port sleep to hold the information, maybe queue it to keep it from losing stuff, then wake up send, wake the other send, read sleep queue, read the other send wake that sleepy one send. kind of synchronicity. read 1 read 2, sleep 1, send 2 , wake 1 send 1 ~ complex as that sounds.

Last edited by BW-userx; 02-10-2018 at 10:53 AM.
 
Old 02-10-2018, 01:29 PM   #9
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
doesn't threads have a means to sleep and wake them up, read from one port sleep to hold the information, maybe queue it to keep it from losing stuff, then wake up send, wake the other send, read sleep queue, read the other send wake that sleepy one send. kind of synchronicity. read 1 read 2, sleep 1, send 2 , wake 1 send 1 ~ complex as that sounds.
I have no need to thread but I got it working as expected using cfmakeraw();
 
Old 02-10-2018, 01:42 PM   #10
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by crabbit View Post
I have no need to thread but I got it working as expected using cfmakeraw();
Quote:
Originally Posted by crabbit
I am writing a simple C program (only running on a turnkey lamp box) that reads data from multiple com ports, translates it and sends it out another port. My plan is to start a thread for monitioring each port.
...

Last edited by BW-userx; 02-10-2018 at 01:53 PM.
 
Old 02-10-2018, 02:15 PM   #11
crabbit
LQ Newbie
 
Registered: Feb 2018
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by BW-userx View Post
...
My bad. design has changed. start daemon, spawn() now. no need for interprocess communication.
 
  


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
Design of serial port logging program and then another to store in database swmcl Linux - Software 2 03-19-2017 05:33 PM
Program/website design? kalleanka General 3 05-22-2009 07:04 AM
Program for website? (design, ... ) lelieng Linux - Newbie 6 03-03-2009 08:05 PM
I need a graphic design program izquierdista Linux - Software 4 06-15-2005 08:54 PM
GNU program's design tda Programming 3 03-16-2003 01:15 AM

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

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