Find the answer to your Linux question:
Results 1 to 6 of 6
All, I'm building an embedded system and I've got a bit a quandary reading a serial port. Here's why and how: I've got a program, written in C, which I ...
  1. #1
    Just Joined! groovygroundhog's Avatar
    Join Date
    Mar 2005
    Location
    Arlington, VA
    Posts
    77

    Serial + C + bash script

    All,

    I'm building an embedded system and I've got a bit a quandary reading a serial port. Here's why and how:

    I've got a program, written in C, which I didn't write and don't want to modify at this point. It talks on ttyS1. I am interested in monitoring that traffic to decide if there's been a failure. I'd like to right a really simple bash script to read characters off ttyS1 but I'm worried that it could mess up that C program. So here's my big question: can a command like "cat /dev/ttyS1" block the serial port? Or is that all software and it won't upset the C program?

    Thanks for any help!

  2. #2
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Ft. Polk, LA
    Posts
    796
    Have you tried it? I tried running tail on /dev/ttyUSB0 which my modem uses, and it won't let me, saying it's unavailable. I figured it would already be blocked, and it appears I was right. I'm guessing only one thing will be able to use the device at a time. But you might go ahead and try it to find out.

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I can see lots of problems with this approach. The biggest one is that even if your shell script can read from /dev/ttyS1, remember than a given byte will be read by either your shell script or the C program, not by both.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Linux Engineer
    Join Date
    Nov 2004
    Location
    Ft. Polk, LA
    Posts
    796
    I just now remembered something that might be helpful. Awhile back I read about some device which takes an input and can branch it to multiple other devices. Can't remember what it's called though, but I'll keep looking around for it.

  5. #5
    Just Joined! groovygroundhog's Avatar
    Join Date
    Mar 2005
    Location
    Arlington, VA
    Posts
    77
    Quote Originally Posted by valan View Post
    Have you tried it? I tried running tail on /dev/ttyUSB0 which my modem uses, and it won't let me, saying it's unavailable.
    Yes, I can read from the serial port doing cat /dev/ttyS1.


    Quote Originally Posted by wje_lf View Post
    I can see lots of problems with this approach. The biggest one is that even if your shell script can read from /dev/ttyS1, remember than a given byte will be read by either your shell script or the C program, not by both.
    This is sort of what I've been wondering about. I wasn't sure how Linux handled the serial port and if more than one process could read from it simultaneously. Do you think if one process (my C program) is reading the port and then a shell script does a read it could mess up the C program?


    Quote Originally Posted by valan View Post
    I just now remembered something that might be helpful. Awhile back I read about some device which takes an input and can branch it to multiple other devices.
    That sounds like exactly what I need. I'm going to poke around the Internets for that--if you think of the name please let me know!


    Thanks for all the help. I'm still figuring out how to get this thing working reliably, but now I know what not to do and possibly what to do.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Do you think if one process (my C program) is reading the port and then a shell script does a read it could mess up the C program?
    It won't mess up the program, exactly, but it will mess up the data that the program's reading from the serial port, because the data can go to only one place, either the C program or your shell script. This is assuming that you don't find some magic bullet (either software or hardware) which takes the input data and sends it to two places.

    Sounds to me like the most straightforward solution is to mess with the C program that you said you don't want to mess with.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...