I’m trying to use applescript to get the temperature from a 1-wire device using an HA7E from Embedded Systems and Serialport X. THe script reads most of the time but sometimes reads 85 on first or second try then returns the temperature OK. I cannot get the resolution to work.
The DS18B20 supports anywhere from 9 to 12 bits of resolution during the conversion process. The time required to complete the conversion varies depending on the desired resolution. The conversion resolution is stored in the ‘Scratchpad’ memory of the DS18B20. This example starts by reading the existing scratchpad, then alters its contents, and writes the new scratchpad back to the DS18B20. By reading the existing scratchpad from the device, other parameters that are also stored in the scratchpad are preserved (i.e. alarm thresholds, etc.)
If anyone has any experience with this I’d be most appreciative.
set dev to "/dev/cu.USA19H1b1P1.1" -- Keyspan USA19HS USB Adapter
set portRef to serialport open dev bps rate 9600 data bits 8 parity 0 stop bits 1 handshake 0
delay 0.3
set delayN to 0.4
set theRes to "W044E4B463F" --uncomment for 9 bit, 1 dec
--set theRes to "W044E4B463F" --uncomment for 10 bit, 2 dec
--set theRes to "W044E4B465F" --uncomment for 11 bit, 3 dec
--set theRes to "W044E4B467F" --uncomment for 12 bit, 4 dec
if portRef is equal to -1 then
display dialog " could not open port "
else
try
--performing standard device search
serialport write "S" & return to portRef
delay delayN
set x to serialport bytes available portRef
set response to serialport read portRef for x
--set the found device ROMid as a variable
--set romID to response & return
--read scratchpad from physical device
--addressing device by previously found ROMid
serialport write "A69000001CFD7E328" & return to portRef
delay delayN
--set x to serialport bytes available portRef
--set response to serialport read portRef for x
--write block of data
serialport write "W0ABEFFFFFFFFFFFFFFFFFF" & return to portRef
delay delayN
set x to serialport bytes available portRef
set response to serialport read portRef for x
--reset 1-wire bus
serialport write "R" & return to portRef
delay delayN
set x to serialport bytes available portRef
--set response to serialport read portRef for x
--writing scratchpad to physical device
--addressing device by previously found ROMid
serialport write "A69000001CFD7E328" & return to portRef
delay delayN
--set x to serialport bytes available portRef
--set response to serialport read portRef for x
--write block of data
serialport write theRes
delay delayN
set x to serialport bytes available portRef
set response to serialport read portRef for x
--reset 1-wire bus
serialport write "R" & return to portRef
delay delayN
set x to serialport bytes available portRef
--set response to serialport read portRef for x
--initiate temperature conversion
--addressing device by previously found ROMid
serialport write "A69000001CFD7E328" & return to portRef
delay delayN
--set x to serialport bytes available portRef
--set response to serialport read portRef for x
--write block of data: 44
serialport write "W0144" & return to portRef
delay delayN
set x to serialport bytes available portRef
set response to serialport read portRef for x
--reset 1-wire bus
serialport write "R" & return to portRef
delay delayN
--set x to serialport bytes available portRef
--reading scratchpad from physical device
--addressing device by previously found ROMid
serialport write "A69000001CFD7E328" & return to portRef
delay delayN
set x to serialport bytes available portRef
set response to serialport read portRef for x
--write block of data
serialport write "W0ABEFFFFFFFFFFFFFFFFFF" & return to portRef
delay delayN
set x to serialport bytes available portRef
set response to serialport read portRef for x
--reset 1-wire bus
serialport write "R" & return to portRef
delay delayN
set x to serialport bytes available portRef
try
set a to numbers 5 thru 6 of response as string
set nHex to a & (numbers 3 thru 4 of response as string)
set nDec to (do shell script "perl -e 'printf(hex(\"" & nHex & "\"))'") as number
set theTemp to nDec / 16
display dialog "the temp is " & theTemp giving up after 10
on error the error_message number the error_number
set temperature to 180
return temperature
serialport close portRef
end try
end try
--reset 1-wire bus
serialport write "R" & return to portRef
delay delayN
set x to serialport bytes available portRef
--set response to serialport read portRef for x
end if
serialport close portRef
Model: mbook pro
Browser: Safari 533.21.1
Operating System: Mac OS X (10.6)