Attribute VB_Name = "mainvb"
Public IERROR% '通訊錯誤指標
Public PATHNAME$ '執行程式所在資料夾名稱
Public COMNAME As Object 'RS232通訊物件所在表單名稱
Public comntype% '通訊方式 0:GPIB, 1:RS232
Public Const ARRAYSIZE = 40   'Size of read buffer
Public rxd As String * ARRAYSIZE
Public itest%   '取得機器設定相關資料 0:未取得, 1:已取得
Public istart% '啟動系統
Public typeids$  '機型編號
Public typeid%  '機型編號代號 0:PSM-2010, 1:PSM-3004, 2:PSM-6003
Public rfile_name$ 'Recall 資料檔案名稱
Public data_title$ '資料檔之標題列
Public maxsetv0!(2)  '於低檔位時最大輸出電壓
Public maxsetv1!(2)  '於高檔位時最大輸出電壓
Public maxsetc0!(2)  '於低檔位時最大輸出電流
Public maxsetc1!(2)  '於高檔位時最大輸出電流
Public range% '1:高檔位, 0:低檔位
Public maxovp!(2)  '最大過電壓
Public maxocp!(2)  '最大過電流
Public iovpen As Boolean  'OVP是否致能
Public iocpen As Boolean  'OCP是否致能
Public ibaud%  '鮑率設定編號 0:9600, 1:4800, 2:2400, 3:1200
Public baudset$(3) '鮑率設定內容
Public comn% 'RS232 Port編號
Public gpibadr%    'PSM之GPIB的位址
Public igpib% '是否有gpib介面
Public iremote%    '是否處於REMOTE Mode 1:是, 0:否
Public ilevel%     'PSM處於低檔位或高檔位之指標
Public ichange%    '是否需更改PSM之檔位設定
Public Const maxrecord% = 99  '記憶體最大儲存編號
Public Const maxdelay& = 59999  '最大delay time (x100ms)
Public Const maxcycle& = 99999  '最大Cycle
Public frecord%(maxrecord) '儲存於檔案之record number
Public fvoltage!(maxrecord) '儲存於檔案之voltage
Public fcurrent!(maxrecord) '儲存於檔案之current
Public fdelay(maxrecord) As Long  '儲存於檔案之delay time
Public fovervolt!(maxrecord) '儲存於檔案之over voltage
Public fovercurr!(maxrecord) '儲存於檔案之over current
Public fenoverv(maxrecord) As Boolean '儲存於檔案之enable over voltage
Public fenoverc(maxrecord) As Boolean '儲存於檔案之enable over voltage
Public IOVERTRIP As Boolean '是否產生過載
Public icom%(4) '有效通訊埠編號
Public itcom% '有效通訊埠個數
Public XL As Object
Public XLBOOK As Object
Public XLSHEET As Object

Public Sub Main()
 PATHNAME = App.Path '取得執行程式所在資料夾名稱
 If Right(PATHNAME, 1) <> "\" Then
  PATHNAME = PATHNAME & "\"
 End If
 itest = 0
 istart = 0
 data_title = "Record,Voltage (V),Current (A),Delay (100ms),OVP (V),OCP (A),OVP status,OCP status"
 Form1.Show
End Sub
Sub create_xls_file(id)
 id = True
 On Error Resume Next
 Set XL = GetObject(, "EXCEL.APPLICATION")
 If Err Then
       Err.Clear
       Set XL = CreateObject("EXCEL.APPLICATION")
       If Err Then
        MsgBox "無法執行Excel應用程式！", vbCritical, "Warning"
        id = False
        Exit Sub
       End If
       XL.Application.Visible = True
 End If
End Sub

Public Sub sendcmd(sxd)
 If comntype = 1 Then '通訊方式為RS232
  Send232 sxd
 Else   '通訊方式為GPIB
  On Error Resume Next
  Form1.CWGPIB1.Write sxd
 End If
 If Right(sxd, 1) = "?" Then delayt (0.1)
End Sub
Public Sub receivecmd()
 IERROR = 1
 If comntype = 1 Then '通訊方式為RS232
  Receive232 rxd
 Else   '通訊方式為GPIB
  On Error Resume Next
  rxd = ""
  rxd = Form1.CWGPIB1.Read
  If rxd <> "" And Asc(rxd) <> 32 Then IERROR = 0
 End If
End Sub

Public Sub Send232(sxd) '由RS232 Port所傳送之資料
 Call start_comm
 COMNAME.MSComm1.output = sxd & Chr(13)
End Sub
Public Sub Receive232(rxd) '由RS232 Port所接收之資料
 IERROR = 0
 Call start_comm
 rxd = ""
 T0 = Timer
 Do While (1)
  rxd = rxd & COMNAME.MSComm1.Input
  If rxd <> "" Then
   If Asc(Right(rxd, 1)) = 10 Then Exit Do
  End If
  Do While (Timer - T0) > 1
   IERROR = 1
   Call stop_comm
   If itest = 0 Then
    MsgBox "Communication error!", , "Warning"
    COMNAME.SBar1.Panels(1).Text = "Communication error"
   End If
   Exit Sub
  Loop
 Loop
 On Error Resume Next
 rxd = Mid(rxd, 1, Len(rxd) - 1)
End Sub

Public Sub delayt(N) 'DELAY N SECONDS
 T0 = Timer
 Do While (Timer - T0) < N
  DoEvents
 Loop
End Sub
Public Sub start_comm()
 If COMNAME.MSComm1.PortOpen = False Then
  COMNAME.MSComm1.PortOpen = True
 End If
End Sub

Public Sub stop_comm()
 If COMNAME.MSComm1.PortOpen = True Then COMNAME.MSComm1.PortOpen = False
End Sub
