home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2001 October / macformat-108.iso / Shareware / Math scientific / PsyScript / libraries / SC.lib / SC.lib.rsrc / TEXT_2000_Source Text.txt < prev    next >
Encoding:
Text File  |  2001-05-18  |  3.2 KB  |  104 lines

  1. property pPrinterPort : "Printer Port USB"
  2. property pModemPort : "P#2KeyUSB02"
  3. property kNewline : ASCII character 10
  4. property pThePort : "" --where the serial IDentifier will be stored
  5. property pSCScalefactor : 5.96046447753906E-6
  6.  
  7. to initialise()
  8.     set pThePort to open serial port pPrinterPort input buffer 8192
  9.     return pThePort
  10. end initialise
  11.  
  12. to clear()
  13.     --just a wrapper to make clear the purpose of a read event
  14.     set strRead to serial port read pThePort
  15. end clear
  16.  
  17. to close {}
  18.     close serial port pThePort with waiting
  19.     return pThePort
  20. end close
  21.  
  22.  
  23. to getSCDataList()
  24.     --reads all the data at the port and returns it as a list of SC values
  25.     --nb: to get the raw hex string, just say
  26.     --        set strRead to serial port read pThePort
  27.     
  28.     set oldDelims to AppleScript's text item delimiters
  29.     try
  30.         set AppleScript's text item delimiters to kNewline & return
  31.         set allData to {}
  32.         set strRead to serial port read pThePort
  33.         set theItemlist to every text item of strRead
  34.         repeat with thisitem in theItemlist
  35.             --set end of allData to (decimal equivalent of thisitem)
  36.             set end of allData to (pSCScalefactor * (my hex2Dec(thisitem)))
  37.         end repeat
  38.         set AppleScript's text item delimiters to oldDelims
  39.     on error
  40.         set AppleScript's text item delimiters to oldDelims
  41.         close serial port pThePort with waiting
  42.     end try
  43.     return allData
  44. end getSCDataList
  45.  
  46. to getRawSCDataList()
  47.     return serial port read pThePort
  48. end getRawSCDataList
  49.  
  50. to SCHexToDec(rawData)
  51.     set oldDelims to AppleScript's text item delimiters
  52.     try
  53.         set AppleScript's text item delimiters to kNewline & return
  54.         set allData to {}
  55.         set theItemlist to every text item of rawData
  56.         repeat with thisitem in theItemlist
  57.             --set end of allData to (decimal equivalent of thisitem)
  58.             set end of allData to my hex2Dec(thisitem)
  59.         end repeat
  60.         set AppleScript's text item delimiters to oldDelims
  61.     on error
  62.         set AppleScript's text item delimiters to oldDelims
  63.     end try
  64.     return allData
  65. end SCHexToDec
  66.  
  67. to SCHexTomicroSiemens(rawData)
  68.     set oldDelims to AppleScript's text item delimiters
  69.     try
  70.         set AppleScript's text item delimiters to kNewline & return
  71.         set scaledDecimalData to {}
  72.         set theItemlist to every text item of rawData
  73.         repeat with thisitem in theItemlist
  74.             set end of scaledDecimalData to pSCScalefactor * (my hex2Dec(thisitem))
  75.         end repeat
  76.         set AppleScript's text item delimiters to oldDelims
  77.     on error
  78.         set AppleScript's text item delimiters to oldDelims
  79.     end try
  80.     return scaledDecimalData
  81. end SCHexTomicroSiemens
  82.  
  83. --written by Paul Berkowitz, March/01
  84. on hex2Dec(hexNum)
  85.     set specChars to {"A", "B", "C", "D", "E", "F"}
  86.     set hexChars to characters of hexNum
  87.     set {tids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, tab}
  88.     set subText to "" & hexChars -- back to text, with tabs
  89.     repeat with i from 1 to 6
  90.         set specChar to item i of specChars
  91.         set AppleScript's text item delimiters to {specChar}
  92.         set textClumps to text items of subText
  93.         set AppleScript's text item delimiters to {"" & (i + 10)}
  94.         set subText to "" & textClumps
  95.     end repeat
  96.     set AppleScript's text item delimiters to {tab}
  97.     set subChars to text items of subText
  98.     set theResult to 0
  99.     repeat with theDigit in subChars
  100.         set theResult to 16 * theResult + theDigit
  101.     end repeat
  102.     set AppleScript's text item delimiters to tids
  103.     return theResult
  104. end hex2Dec