This will be good information for me to further develop as documentation for our raspberryPI hackers day we are trying to get together here in Melbourne Australia this year
Serial port setup for atlas devices....
Disable Console Messages as per this post. this will free up the serial port so that the shell can use it.
Reference:
abelectronics.co.uk
Script to read serial port
Code:
#!/bin/bash
#title :AquaPi Read Script
#description :Read sensor data, range test then write data to file
#author :R Conway
#date :July 2013
#version :23
#usage :Start script in background [nohup /var/script/DataScan >/dev/null &]
#notes :if you kill the script you also kill the cat process left behind
#=======================================================================
stty -F /dev/ttyAMA0 raw 38400 cs8 -ignpar -cstopb eol 255 eof 255
(pkill -x cat)
while true ;do
### Read pH and ORP from Atlas Scientific Devices ###
( cat < /dev/ttyAMA0 > ./serial_in ) &
bkgPID=$!; disown $PID
sleep 2
printf "\x72\xff\x0D" >/dev/ttyAMA0 ; sleep 1
printf "\x72\xff\x0D" >/dev/ttyAMA0 ; sleep 2
kill $bkgPID >/dev/null
read ReefPH <./serial_in
ReefPH=$(echo "$ReefPH" | sed 's/.\{1\}$//') # Delete Trailing Carriage return
This is how it works.
I could of used python and the serial library however this worked for me and never bothered changing it. I know programmers hate doing things in shell scripts especially serial port stuff but I had all the rest of my program already running in bash.
Some more comments on the code above
Sets up the serial port for baud rate and communications suitable for the atlas devices
stty -F /dev/ttyAMA0 raw 38400 cs8 -ignpar -cstopb eol 255 eof 255
If the script terminates it can leave open a previous cat command. If this happens the script will not run. So on startup of this script we simply kill any processes called cat that maybe running
(pkill -x cat)
OK here comes the spot you would start a continuous loop (I show it commented out (##) as this is just a part of the overall script.
## while true ;do
### Read pH and ORP from Atlas Scientific Devices ###
Start up a process and CATenate the serial port and store it in a file called serial_in in the current directory
( cat < /dev/ttyAMA0 > ./serial_in ) &
The cat process started above has to be disowned from this script so it can be killed further down
bkgPID=$!; disown $PID
sleep 2
OK here we go send out a read command to the serial port. x72 is hex for the ascii character r xOD is carriage return. The Atlas manual states you send the board “r” and it responds with the value. I have found I needed to do this twice to get reliable data thus the two lines the same.
printf "\x72\xff\x0D" >/dev/ttyAMA0 ; sleep 1
printf "\x72\xff\x0D" >/dev/ttyAMA0 ; sleep 2
Whilst we have sent the character r to the atlas device it has responded with the actual value. That’s why we started uop the “cat process at the start. Cat has captured the serial rcv port and placed it in a file called serial_in. before we can read what in the file we have to kill the cat process as the process has locked the file and we cannot read it until we kill the cat.
kill $bkgPID >/dev/null
This line simply reads whatever is in the serial_in file into a variable called ReefPH
read ReefPH <./serial_in
The data from the atlas device contains an annoying carriage return. We have to strip the carriage return from the value so that you can use the data as a floating point number. If you do not do this ReefPH contains ASCII letters and the script cannot do any calculations etc on an ASCII data so basically this turns an ascii data into a floating point number.
ReefPH=$(echo "$ReefPH" | sed 's/.\{1\}$//') # Delete Trailing Carriage return
You are now free to do whatever you want to do with the variable ReefPH
I will post up my entire script however may confuse you at this stage. So lets see if you get the idea above before I go onto error checking and logging for you.
Oh just remembered we have a code manager Ill try and find this feature and post my entire logging file there.