dslreports logo
site
 
    All Forums Hot Topics Gallery
spc

spacer




how-to block ads


Search Topic:
uniqs
4360
share rss forum feed

dlsauers

join:2005-10-25
Wellston, OH

BASH script serial input

I am trying to use serial input for a BASH script. I have a USB/Serial converterd connected to the device and using gtkterm or minicom I can get the output of the device with out issue..

Problem is getting it into my script. I have another similar device connected via different USB/serial converter and using my script it works great...Can't use the same converter as the original is goes USB to serial with a specific non DB9/DB25 connector for this device. The new device uses a standard DB9 connector.

The original script uses ser2net to send the data out to the network and I use BASH exec 6/dev/tcp/192.168.0.2/9200

And then use read in a while loop without issue...

Doing the same thing with the new converter and new device, no joy.

I can use dd to get the output, but its not in real time, and real time is part of the requirement. (Not real time in that dd takes time to "copy" and that delay is too long.)

 
#! /bin/sh
 
stty -F /dev/ttyUSB0 ispeed 19200 ospeed 19200 cs8 -parenb -cstopb -echo -hupcl
 
# Loop
while [ 1 ]; do
READ=`dd if=/dev/ttyUSB0 count=1`
echo $READ
done
 
 

Plus dd pollutes the out put with crap I don't need.

AB A 999999 99999999
0+1 records in
0+1 records out
168 bytes (168 B) copied, 0.139692 s, 1.2 kB/s
 

I only need the data line, the line starting with AB, the dd stuff I don't need.. and I didn't see a way in dd on the man page to surpress that stuff like --quiet or something.

I can cat the input

cat /dev/ttyUSB0
 

but trying to do:

#!/bin/bash
 
 cleanup()
 # example cleanup function
 {
  exec 6>&-
  exec 6<&-
  return $?
 }
 
 control_c()
 # run if user hits control-c
 {
  echo -en "\n*** Exiting ***\n"
  cleanup
  exit $?
 }
 
# trap keyboard interrupt (control-c)
 trap control_c SIGINT
 
# Connect TCP socket 
exec 6<>/dev/tcp/192.168.0.2/9200
 
while read <&6
    # Show the line on standard output just for debugging.
   LOGDATETIME=`date +%D_%R:%S`
   echo "$LOGDATETIME $REPLY" >&1
done
 
 

Serial converter:

~ $ sudo lsusb -v
 
Bus 004 Device 002: ID 067b:2303 Prolific Technology, Inc. PL2303 Serial Port
Device Descriptor:
  bLength                18
  bDescriptorType         1
  bcdUSB               1.10
  bDeviceClass            0 (Defined at Interface level)
  bDeviceSubClass         0 
  bDeviceProtocol         0 
  bMaxPacketSize0        64
  idVendor           0x067b Prolific Technology, Inc.
  idProduct          0x2303 PL2303 Serial Port
  bcdDevice            3.00
  iManufacturer           1 Prolific Technology Inc.
  iProduct                2 USB 2.0 To COM Device
  iSerial                 0 
  bNumConfigurations      1
  Configuration Descriptor:
    bLength                 9
    bDescriptorType         2
    wTotalLength           39
    bNumInterfaces          1
    bConfigurationValue     1
    iConfiguration          0 
    bmAttributes         0x80
      (Bus Powered)
    MaxPower              100mA
    Interface Descriptor:
      bLength                 9
      bDescriptorType         4
      bInterfaceNumber        0
      bAlternateSetting       0
      bNumEndpoints           3
      bInterfaceClass       255 Vendor Specific Class
      bInterfaceSubClass      0 
      bInterfaceProtocol      0 
      iInterface              0 
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x81  EP 1 IN
        bmAttributes            3
          Transfer Type            Interrupt
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x000a  1x 10 bytes
        bInterval               1
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x02  EP 2 OUT
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
      Endpoint Descriptor:
        bLength                 7
        bDescriptorType         5
        bEndpointAddress     0x83  EP 3 IN
        bmAttributes            2
          Transfer Type            Bulk
          Synch Type               None
          Usage Type               Data
        wMaxPacketSize     0x0040  1x 64 bytes
        bInterval               0
Device Status:     0x0000
  (Bus Powered)
 

I can use screen to get the input as well:

screen /dev/ttyUSB0 19200,cs8,-parenb,-cstopb,-hupcl
 

But again, one line and it overwrites it self.

using stty to set RAW mode fubar's the device, and it quits working even via minicom or gtkterm.

User is part of the dialout group, and I am accesing it via dd, cat, screen as that user with out issue.

Any good reliable way to read serial in via BASH... sorry C/C++ is not an option here, I don't use it. I would prefer to use ser2net to feed this to another system running this script to do various updates with the script, since I should really just need to change the exec line and then the part that does the updates in the while loop for the new data ....but no joy so far...

Possibly due to no new line at the end of data when telnet via ser2net or cat directly... but in gtkterm or minicom each output is on its own line, like exepected...

But BASH doesn't seem to want to read this via exec even if ser2net will spit it out when I telnet to it..

Linux: kubuntu based distro
Linux 2.6.35-22-generic #35-Ubuntu SMP Sat Oct 16 20:36:48 UTC 2010 i686 GNU/Linux

Any good BASH serial input methods???

Thanks...


EHLO

@pnap.net
With regard to dd, what about dd|head -n 1 or dd 2>/dev/null

Seems the data you want is in stdout and the data on dd you dont want is on stderr.


EHLO

@pnap.net
Also you can cat /dev/ttyUSB0 directly with IFS="" and read -r. With dd, read up on the ibs, obs, and bs arguments too in case this can aid in speed to data acquisition since you're waiting for ibs,obs, and bs to fill the buffer which defaults to 512.

Is Perl out of the question here?

dlsauers

join:2005-10-25
Wellston, OH
said by EHLO :
Is Perl out of the question here?
Yes.. BASH only.

I try the other suggestions to see if they do what I want....

Thanks.


Maxo
Your tax dollars at work.
Premium,VIP
join:2002-11-04
Tallahassee, FL

1 edit
reply to dlsauers
Is it possible something like this may work?
while read LINE; do
  #whatever
done < /dev/ttyUSB0
 
I have my laptop with me, but my serial cable is at home, and I'm out traveling right now so I can't test it. I'm sure that this exactly wouldn't work, but perhaps a variation on the loop.

--
"Padre, nobody said war was fun now bowl!" - Sherman T Potter

»maxolasersquad.com/

»maxolasersquad.blogspot.com

»www.facebook.com/maxolasersquad

dlsauers

join:2005-10-25
Wellston, OH
said by Maxo:
Is it possible something like this may work?

I'll try that ... I have things working with dd and nuking its extraneous output off to /dev/null land...but it seems to miss data at points due to dd or seomthing else and thats an issue,

I really would like to get this to work with exec and telnet via ser2net.. but BASH doesn't seem to want to co-operate with

exec 6/dev/tcp/192.168.0.2/9200

When its this device.

I can telnet into it and see the line on the screen change with the data, but its one line it keeps over writing, but if I use exec in the script it never gets any data.


leibold
Premium,MVM
join:2002-07-09
Sunnyvale, CA
kudos:10
Reviews:
·SONIC.NET
said by dlsauers:

I have things working with dd and nuking its extraneous output off to /dev/null land...but it seems to miss data at points due to dd or seomthing else and thats an issue,

You put the dd command into a loop and keep opening and closing the serial device. It is no surprise that this causes you to loose data.
Make sure that you keep the serial device open ( Maxo See Profile showed an example of how to do a loop that keeps the serial port open).

As for the "one line it keeps over writing" there is apparently a difference in end-of-line handling between the old and new adapter. You may be able to compensate by using the stty options icrnl and -inlcr to convert carriage returns into new lines (line feed).
--
Got some spare cpu cycles ? Join Team Helix or Team Starfire!