Script runs in open window, but won't work in background

The following script used to work fine in the background, but I modded it. Now it runs fine from a Script Editor window, but won’t work when called from Xtension.
when called, the script fails to get the proper response from portterm for the variable “TempString”.

Any Ideas?
[–Program Setup
–Initialize variables
set temperature to “initial value Temp”
set TempString to “initial value TS”
set SensorAddress to “initial value SA”
set iData to “initial value iData”

–Setup information for PortTerm
–set PortSelect to “cu.KeySerial1”
–set baud rate to 9600 with 8 data bits, no parity, one stop bit, and
–flowcontrol to Xon / Xoff

–Set up Sensor ID and Locations
–By using these lists instead of the 1-wire search routines, sensors can be processed
–in any order.
set NumberOfSensors to 1
set CurrentSensorList to {“CC000000BC2FC328”, “CD000000BC87A928”}
set SensorLocationList to {“Sunroom Temperature”, “Outdoor Temperature”}

–Main Program
–This script obtains the temperature from each sensor in the list and stores the values in a
–corresponding psudo unit in the master list.
repeat with I from 1 to NumberOfSensors
set CurrentSensor to item I of CurrentSensorList
get DataFromSensor(CurrentSensor, TempString)
getTemperatureFromSensorData(TempString, temperature)
if temperature is greater than 80 then
tell application “XTension”
write log “Temp reading in error, rerunning Temp Reader” color red
end tell
else
tell application “XTension”
dim item I of SensorLocationList to temperature with no transmit
if item I of SensorLocationList is equal to “Sunroom Temperature” then Set Unit Property “actualTemp” to temperature in unit “Sunroom Temperature”
write log “The " & (item I of SensorLocationList) & " is " & temperature & " degrees celsius.” color green
end tell
end if
end repeat

on DataFromSensor(CurrentSensor)
–This routine transfers control to the program PortTerm, available from kulesh-software.com,
–which in turn links to an HA-7E 1-wire controller with one or more DS18B20 temperature sensors
–connected.
global TempString
tell application “PortTerm”
-----------------------------------
–Setup for PortTerm
–once this section is set up, it should
–never need to be changed again.
connect to port “cu.KeySerial1”
configure baud rate “9600” parity “none”
-----------------------------------
write data “A” & CurrentSensor format “atcm”
delay 0.3
write data “W044E4B467F” format “atcm”
delay 0.3
write data “M” format “atcm”
delay 0.2
write data “W0144” format “atcm”
delay 0.9
write data “M” format “atcm”
delay 0.35
read
set iData to “”
write data “W0ABEFFFFFFFFFFFFFFFFFF” format “atcm”
delay 0.4
read
set (TempString) to iData
tell PortTerm App “XTension”
write log (TempString) & “//ends here”
end tell
disconnect

end tell
return (TempString)

end DataFromSensor

on getTemperatureFromSensorData(theData)
–This script extracts the temperature from the returned data string (e.g. “BE52014B467FFF0E10FF”)
–and converts the temperature (Celsius) from a hexadecimal number to a decimal number, and
–then converts the temperature to Fahrenheit. Temperature data is contained in characters
–3, 4, 5, and 6 of the data string.
– degrees are characters 5, 6 and 3 (the integer part, in that order) and the fraction is character 4
– the temperature is a two byte signed integer (so -0.0625 degree C is represented as FFFF)
– to convert hex to decimal is easiest with the scripting addition “24U Hex OSAX” from 24U Software,
– but it can be done without:
global temperature
set hundredsHex to (ASCII number (character 5 of theData)) - 48 – ASCII (48) is “0”, (49) is “1” etc
if hundredsHex > 9 then set hundredsHex to hundredsHex - 7 – ASCII “distance” between 9 and A
set tensHex to (ASCII number (character 6 of theData)) - 48
if tensHex > 9 then set tensHex to tensHex - 7
set unitsHex to (ASCII number (character 3 of theData)) - 48
if unitsHex > 9 then set unitsHex to unitsHex - 7
set fractionHex to (ASCII number (character 4 of theData)) - 48
if fractionHex > 9 then set fractionHex to fractionHex - 7
set temperature to ({round {10 * (hundredsHex * 256 + tensHex * 16 + unitsHex + fractionHex / 16)}} / 10)
–calculates temperature in Celsius
if temperature < 80 then
–set temperature to round (32 + Temperature * 9 / 5) – converts Celsius to Fahrenheit
return temperature
else
return -(4096 - temperature)
end if
end getTemperatureFromSensorData]



Model: G-5 1.8ghx
AppleScript: 1.10.7
Browser: Netscape/7.2
Operating System: Mac OS X (10.4)

Nothing catches my eye when I read it - how does it fail? Do some or none of the log readings appear?

Your two handlers only expect one parameter each, but the calls to them contain two. That should cause errors.

The handlers also return values that aren’t actually used. Instead, you’re relying on global variables. I don’t know if that’s what’s causing the problem (I don’t have either app.), but it’s worth checking out. Instead of making ‘TempString’ and ‘temperature’ global, try setting them to the results returned by the relevant handlers, eg.:

set TempString to DataFromSensor(CurrentSensor)
set temperature to getTemperatureFromSensorData(TempString)

Certainly, at the bottom of the script, ‘return -(4096 - temperature)’ is currently totally ineffective, since it doesn’t change the value of ‘temperature’. Using the above suggestion should rectify that.

Thank you Nigel

Your suggested changes seem to have fixed the problem, AND make the script run a little quicker.
However, I still don’t know why the original script would run when in the foreground, but not when called to run in the background.
Perhaps I forgot to feed the gremlins.

Bob