Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I have a spectrophotometer attached to a PC via RS232. I've successfully created some scripts to control it and receive data. The data is dumped to a file one ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    5

    Text data grabbed from /dev/ttyS0 - encoding issues

    Hi,

    I have a spectrophotometer attached to a PC via RS232. I've successfully created some scripts to control it and receive data.
    The data is dumped to a file one line at a time. When finished, it looks like the attached file.

    All the data is there, as is evident when you 'cat' the file. But it's behaving like a binary file. I don't know much about text encoding, so I don't know how to get it into simple ASCII, which will be viewable with a text editor, spreadsheet etc.

    The data file is attached.

    While I'm asking about this, I also want to be able to split the two columns from the attached file, into two separate files (column 1 into one file, column 2 into another).

    Any help would be greatly appreciated!

    Thanks
    Attached Files Attached Files

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Looks like you have embedded null bytes and an EOT (the EOT: \004 is near the end). Here's how to fix that:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate fix for embedded null bytes.
    
    # Utility functions: print-as-echo, print-line-with-visual-space.
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    
    FILE=${1-data1}
    
    # Clean original file, if no clean file found.
    
    if [ ! -f $FILE ]
    then
      pl " Sample line before for \"data\", type $(file data):"
      od -bc data | head -4
      ./p1 data >$FILE
      pl " Sample line after for $FILE, type $(file $FILE):"
      od -bc $FILE | head -4
      pl " perl code for transformation:"
      cat p1
    fi
    
    pl " Sample of $FILE:"
    head -5 $FILE
    
    exit 0
    which produces:
    Code:
    % ./s1
    
    -----
     Sample line before for "data", type data: data:
    0000000 066 060 060 056 060 040 040 060 056 060 061 060 000 012 065 071
              6   0   0   .   0           0   .   0   1   0  \0  \n   5   9
    0000020 070 056 060 040 040 060 056 060 061 060 000 012 065 071 066 056
              8   .   0           0   .   0   1   0  \0  \n   5   9   6   .
    
    -----
     Sample line after for data1, type data1: ASCII text:
    0000000 066 060 060 056 060 040 040 060 056 060 061 060 012 065 071 070
              6   0   0   .   0           0   .   0   1   0  \n   5   9   8
    0000020 056 060 040 040 060 056 060 061 060 012 065 071 066 056 060 040
              .   0           0   .   0   1   0  \n   5   9   6   .   0    
    
    -----
     perl code for transformation:
    #!/usr/bin/env perl
    
    # @(#) p1	Demonstrate removal of null bytes.
    
    use warnings;
    use strict;
    
    while ( <> ) {
      s/\000//g;
      s/\004//g;
      print;
    }
    
    -----
     Sample of data1:
    600.0  0.010
    598.0  0.010
    596.0  0.010
    594.0  0.010
    592.0  0.010
    For the column splitting, you should be able to use command cut, so look at man cut. (It would be easy for me to put that in the perl script or the shell script, but by doing work yourself, you'll learn more.)

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Just Joined!
    Join Date
    Jan 2011
    Posts
    5
    Yes, you're right about the NULs and the EOT, checked it out with og. They're from the serial device.

    Thanks for the code, very helpful!

    Cheers

Posting Permissions

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