home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 26 / CD_ASCQ_26_1295.iso / vrac / fluke13.zip / FLUKEINT.ASP < prev    next >
Text File  |  1995-07-05  |  6KB  |  251 lines

  1. ;
  2. ; Part of the flukecom system, see comments in flukecom.asp for information
  3. ; on how this is used and what is required 
  4. ;
  5.  
  6. ; globals
  7. long     currbaud        ; current baud rate
  8.  
  9. include "flukecom.h"
  10.  
  11. proc main
  12.     integer reterror
  13.     string retstr
  14.  
  15.     call init_pcplus
  16.     call write_user with versionstr
  17.     call grab_meter with &reterror,&retstr
  18.     if reterror
  19.         call write_user with "ERROR: can't find meter, aborting"
  20.         execute "flukexit"
  21.     endif
  22.     call write_user with &retstr
  23.     execute "flukecom"
  24. endproc
  25.  
  26. ; initialize the script
  27.  
  28. proc init_pcplus
  29.     set aspdebug on
  30.     set display off
  31.     set rxdata on
  32.     set keys on
  33.     set softflow off    ; off handshake to prevent echoing to screen
  34.     set duplex full        ; to prevent echoing to screen
  35.     set msg_crlf off    ; prevent default cr/lf to double space screen
  36.     set cr cr        ; prevent multiple cr to double space screen
  37.     set fgets_crlf off    ; prevent fgets from grabbing cr
  38.     set parity none
  39.     set stopbits 1
  40.     set databits 8
  41. endproc
  42.  
  43. ; this routine cycles to the next acceptable baud rate and leaves
  44. ; the value in the global currbaud.
  45. ; return is normally zero with the exception of when a complete cycle
  46. ; has been completed (returned to 1200), then 1
  47.  
  48. proc cycle_baud
  49.     intparm reterror
  50.  
  51.     reterror = 0
  52.     if currbaud == 19200
  53.         currbaud = 1200
  54.         return
  55.     endif
  56.     if currbaud == 1200
  57.         currbaud = 9600
  58.         return
  59.     endif
  60.     if currbaud == 9600
  61.         currbaud = 4800
  62.         return
  63.     endif
  64.     if currbaud == 4800
  65.         currbaud = 2400
  66.         return
  67.     endif
  68.     if currbaud == 2400
  69.         currbaud = 1200
  70.         reterror = 1
  71.         return
  72.     endif
  73.     currbaud = 19200        ; in case nothing worked
  74. endproc
  75.  
  76. ; this routine synchronizes with the scopemeter by determining the
  77. ; current serial port speed and then returns.  Meter and PCPLUS are
  78. ; left at rate defined above
  79. ; 0 if successful
  80. ; 1 if not
  81.  
  82. proc grab_meter
  83.     intparm    reterror    ; return from this procedure
  84.     strparm idstring    ; identification string captured
  85.     integer ret,retget    ; return code from called routines
  86.     integer bauddone    ; complete baudrate cycle
  87.     integer doneflag    ; done with sync up, skip to end
  88.     integer numsync        ; number of sync attempts at buad
  89.     string    tmpstr        ; string to send to meter to reset baud
  90.  
  91.     doneflag = 0
  92.     bauddone = 0
  93.     currbaud = 19200
  94.     set baud currbaud    ; try the default case of high speed
  95.     
  96.     while (!bauddone) && (!doneflag)
  97.         numsync = 0
  98.         while (!doneflag) && (numsync < numsynctries)
  99.             call send2meter with "id",&ret
  100.             numsync++
  101.  
  102.             ; if everything really ok
  103.             if (ret == 0)
  104.                 doneflag = 1
  105.                 call getmeter with &tmpstr,&retget
  106.  
  107.                 ; error on response string
  108.                 if retget
  109.                     doneflag = 0
  110.                 endif
  111.             endif
  112.         endwhile
  113.  
  114.         ; try the next baud rate if applicable
  115.         if (!doneflag)
  116.             call cycle_baud with &bauddone
  117.             set baud currbaud
  118.         endif
  119.     endwhile
  120.     if (!doneflag)
  121.         reterror = 1
  122.         return
  123.     endif    
  124.  
  125.     ; synced up and the current baud rate, set to that defined
  126.     strfmt tmpstr "pc %li,n,8,1" fullbaudcom
  127.     call send2meter with tmpstr,&ret
  128.     if ret
  129.         reterror = 1 
  130.         return
  131.     endif
  132.  
  133.     ; wait for meter to reset baud rate
  134.     pause 1
  135.  
  136.     currbaud = fullbaudcom
  137.     set baud fullbaudcom
  138.  
  139.     ; confirm that we really have a link at the fast rate
  140.     call send2meter with "id",&ret
  141.     if ret
  142.         reterror = 1 
  143.         return
  144.     endif
  145.     call getmeter with &idstring,&retget
  146.     if retget
  147.         reterror = 1 
  148.         return
  149.     endif
  150.     reterror = 0
  151. endproc
  152.  
  153. ; this routine gets the response string from the meter
  154. ; If the response was succesful, then a string is returned and
  155. ; of 0 indicates successful
  156. ; of 1 indicates a continuation packet is pending
  157. ; of 2 indicates a failure
  158. ; NOTE THIS ROUTINE IS LIMITED TO 80 characters per string
  159.  
  160. proc getmeter
  161.     strparm outstr            ; string to return
  162.     intparm reterror        ; error code to return
  163.  
  164.     integer newchar            ; the latest character acquired
  165.     integer    counter            ; length of the string to check for 
  166.                     ; overflow of string
  167.  
  168.     outstr = ""
  169.     comgetcd newchar
  170.     while (newchar != -1) && (newchar != cret)
  171.         strfmt outstr "%s%c" outstr newchar
  172.         strlen outstr counter
  173.  
  174.         ; test to see if this is a implicit continuation string
  175.         ; should never happen
  176.         if (counter >= maxstrlen)
  177.             reterror = 1
  178.             return
  179.         endif
  180.         comgetcd newchar
  181.     endwhile
  182.     if (newchar == cret)
  183.         reterror = 0
  184.         return
  185.     endif
  186.     reterror = 2
  187. endproc
  188.  
  189. proc write_user
  190.     strparm outstr
  191.     string tmpstr
  192.  
  193.     strfmt tmpstr "%s`r`n" outstr
  194.     message tmpstr
  195. endproc
  196.  
  197. ; this routine only sends the data in the input string
  198. ; return error code
  199. ; of 1 indicates an ack error existed on the receive
  200. ; this is a warning that the ack code is incorrect
  201. ; of 2 indicates a sync problem existed on the receive (or timeout)
  202. ; this is a fatal
  203. ; Response string must be handled separately.
  204.  
  205. proc send2meter
  206.     strparm instr
  207.     intparm reterror
  208.  
  209.     string tmpstr
  210.     integer retack            ; acknowledge byte
  211.     integer terminator
  212.     integer comcounter        ; how many times have we tried?
  213.  
  214.     rflush                ; clean up anything that has been 
  215.                     ; received
  216.     strfmt tmpstr "%s`r" instr    ; format up the data to send
  217.     transmit tmpstr
  218.  
  219.     comcounter = 0
  220.     comgetc retack
  221.     while (comcounter < pausetimes) && (retack == -1)
  222.         comcounter++
  223.         mspause pausecheck
  224.         comgetc retack
  225.     endwhile
  226.     if retack == -1            ; timeout!
  227.         reterror = 2
  228.         return
  229.     endif
  230.  
  231.     comcounter = 0
  232.     comgetc terminator        ; pitch the terminator
  233.     while (comcounter < pausetimes) && (terminator == -1)
  234.         comcounter++
  235.         mspause pausecheck
  236.         comgetc terminator
  237.     endwhile
  238.  
  239.     if terminator != cret        ; sync up problem or timeout
  240.         reterror = 2
  241.         return
  242.     endif
  243.  
  244.     if retack != '0'        ; check the ack code
  245.         reterror = 1
  246.         return
  247.     endif
  248.  
  249.     reterror = 0
  250. endproc
  251.