home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 21.ddi / VBTERM.FR_ / VBTERM.bin (.txt)
Encoding:
Visual Basic Form  |  1993-02-14  |  18.2 KB  |  592 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "MSComm Terminal "
  5.    ForeColor       =   &H00000000&
  6.    Height          =   3945
  7.    Icon            =   VBTERM.FRX:0000
  8.    Left            =   870
  9.    LinkMode        =   1  'Source
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   3255
  12.    ScaleWidth      =   7470
  13.    Top             =   1050
  14.    Width           =   7590
  15.    Begin CommonDialog OpenLog 
  16.       CancelError     =   -1  'True
  17.       Color           =   &H00C0C0C0&
  18.       DefaultExt      =   "LOG"
  19.       DialogTitle     =   "Open Communications Log File"
  20.       Filename        =   "*.log"
  21.       Filter          =   "*.log"
  22.       Left            =   120
  23.       Top             =   900
  24.    End
  25.    Begin MSComm MSComm1 
  26.       CommPort        =   2
  27.       InBufferSize    =   8192
  28.       Interval        =   1000
  29.       Left            =   120
  30.       RThreshold      =   1
  31.       Settings        =   "2400,n,8,1"
  32.       Top             =   420
  33.    End
  34.    Begin TextBox Term 
  35.       BorderStyle     =   0  'None
  36.       Height          =   516
  37.       Left            =   768
  38.       MultiLine       =   -1  'True
  39.       ScrollBars      =   3  'Both
  40.       TabIndex        =   0
  41.       Top             =   480
  42.       Width           =   1116
  43.    End
  44.    Begin Label Label2 
  45.       BackColor       =   &H00C0C0C0&
  46.       Caption         =   "Status - "
  47.       Height          =   192
  48.       Left            =   120
  49.       TabIndex        =   2
  50.       Top             =   0
  51.       Width           =   732
  52.    End
  53.    Begin Line Line1 
  54.       BorderColor     =   &H00808080&
  55.       BorderWidth     =   3
  56.       X1              =   0
  57.       X2              =   7320
  58.       Y1              =   240
  59.       Y2              =   240
  60.    End
  61.    Begin Label Label1 
  62.       BackColor       =   &H00C0C0C0&
  63.       Height          =   192
  64.       Left            =   840
  65.       TabIndex        =   1
  66.       Top             =   0
  67.       Width           =   6612
  68.    End
  69.    Begin Menu MFile 
  70.       Caption         =   "&File"
  71.       Begin Menu MOpenLog 
  72.          Caption         =   "&Open Log File..."
  73.       End
  74.       Begin Menu MCloseLog 
  75.          Caption         =   "&Close Log File"
  76.          Enabled         =   0   'False
  77.       End
  78.       Begin Menu M3 
  79.          Caption         =   "-"
  80.       End
  81.       Begin Menu MSendText 
  82.          Caption         =   "&Transmit Text File..."
  83.          Enabled         =   0   'False
  84.       End
  85.       Begin Menu Bar2 
  86.          Caption         =   "-"
  87.       End
  88.       Begin Menu MFileExit 
  89.          Caption         =   "E&xit"
  90.       End
  91.    End
  92.    Begin Menu MPort 
  93.       Caption         =   "&CommPort"
  94.       Begin Menu MOpen 
  95.          Caption         =   "Port &Open"
  96.       End
  97.       Begin Menu MSettings 
  98.          Caption         =   "&Settings..."
  99.       End
  100.       Begin Menu MBar1 
  101.          Caption         =   "-"
  102.       End
  103.       Begin Menu MDial 
  104.          Caption         =   "&Dial Phone Number..."
  105.       End
  106.       Begin Menu MHangup 
  107.          Caption         =   "&Hang Up Phone"
  108.          Enabled         =   0   'False
  109.       End
  110.    End
  111.    Begin Menu MProp 
  112.       Caption         =   "&Properties"
  113.       Begin Menu MInputLen 
  114.          Caption         =   "&InputLen..."
  115.       End
  116.       Begin Menu MRThreshold 
  117.          Caption         =   "&RThreshold..."
  118.       End
  119.       Begin Menu MSThreshold 
  120.          Caption         =   "&SThreshold..."
  121.       End
  122.       Begin Menu MParRep 
  123.          Caption         =   "P&arityReplace..."
  124.       End
  125.       Begin Menu MDTREnable 
  126.          Caption         =   "&DTREnable"
  127.       End
  128.       Begin Menu Bar3 
  129.          Caption         =   "-"
  130.       End
  131.       Begin Menu MHCD 
  132.          Caption         =   "&CDHolding..."
  133.       End
  134.       Begin Menu MHCTS 
  135.          Caption         =   "CTSH&olding..."
  136.       End
  137.       Begin Menu MHDSR 
  138.          Caption         =   "DSRHo&lding..."
  139.       End
  140.    End
  141. '--------------------------------------------------
  142. ' VBTerm - Demonstration program for the MSComm
  143. ' communications custom control.  Demonstrates the
  144. ' functionality of the control in the context of a
  145. ' terminal program.
  146. ' Copyright (c) 1992, Crescent Software, Inc.
  147. ' by Don Malin and Carl Franklin.
  148. '--------------------------------------------------
  149. DefInt A-Z
  150. Option Explicit
  151.                         
  152. Dim Ret                 'Scratch integer
  153. Dim Temp$               'Scratch string
  154. Dim hLogFile            'Handle of open log file
  155. Sub Form_Resize ()
  156.    '--- Resize the Term (display) control and
  157.    '    status bar.
  158.    Line1.X2 = ScaleWidth
  159.    Term.Move 0, Line1.Y2 + 15, ScaleWidth, ScaleHeight - Line1.Y2 + 15
  160. End Sub
  161. Sub Form_Unload (Cancel As Integer)
  162.     Dim T&
  163.     If MSComm1.PortOpen Then
  164.        '--- Wait 10 seconds for data to be transmitted
  165.        T& = Timer + 10
  166.        Do While MSComm1.OutBufferCount
  167.           Ret = DoEvents()
  168.           If Timer > T& Then
  169.              Select Case MsgBox("Data cannot be sent", 34)
  170.                 '--- Abort
  171.                 Case 3
  172.                    Cancel = True
  173.                    Exit Sub
  174.                 '--- Retry
  175.                 Case 4
  176.                    T& = Timer + 10
  177.                 '--- Ignore
  178.                 Case 5
  179.                    Exit Do
  180.              End Select
  181.           End If
  182.        Loop
  183.        MSComm1.PortOpen = 0
  184.     End If
  185.     '--- If log file is open, flush and close it
  186.     If hLogFile Then MCloseLog_Click
  187.     End
  188. End Sub
  189. Sub MCloseLog_Click ()
  190.    '--- Close the log file.
  191.    Close hLogFile
  192.    hLogFile = 0
  193.    MOpenLog.Enabled = True
  194.    MCloseLog.Enabled = False
  195.    Form1.Caption = "MSComm Terminal"
  196. End Sub
  197. Sub MDial_Click ()
  198.     On Local Error Resume Next
  199.     Static Num$
  200.     '--- Get a number from the user.
  201.     Num$ = InputBox$("Enter Phone Number:", "Dial Number", Num$)
  202.     If Num$ = "" Then Exit Sub
  203.     '--- Open the port if it isn't already
  204.     If Not MSComm1.PortOpen Then
  205.        MOpen_Click
  206.        If Err Then Exit Sub
  207.     End If
  208.     '--- Dial the number
  209.     MSComm1.Output = "ATDT" + Num$ + Chr$(13) + Chr$(10)
  210. End Sub
  211. '--- Toggle DTREnabled property
  212. Sub MDTREnable_Click ()
  213.     MSComm1.DTREnable = Not MSComm1.DTREnable
  214.     MDTREnable.Checked = MSComm1.DTREnable
  215. End Sub
  216. Sub MFileExit_Click ()
  217.     '--- Use Form_Unload since it has code to check
  218.     '    for un sent data and open log file
  219.     Form_Unload Ret
  220. End Sub
  221. '--- Toggle DTREnable to hang up the line
  222. Sub MHangup_Click ()
  223.     Ret = MSComm1.DTREnable     'Save current setting
  224.     MSComm1.DTREnable = True    'Turn DTR on
  225.     MSComm1.DTREnable = False   'Turn DTR off
  226.     MSComm1.DTREnable = Ret     'Restore old setting
  227. End Sub
  228. '--- Display the value of the CDHolding property.
  229. Sub MHCD_Click ()
  230.     If MSComm1.CDHolding Then
  231.         Temp$ = "True"
  232.     Else
  233.         Temp$ = "False"
  234.     End If
  235.     MsgBox "CDHolding = " + Temp$
  236. End Sub
  237. '--- Display the value of the CTSHolding property.
  238. Sub MHCTS_Click ()
  239.     If MSComm1.CTSHolding Then
  240.         Temp$ = "True"
  241.     Else
  242.         Temp$ = "False"
  243.     End If
  244.     MsgBox "CTSHolding = " + Temp$
  245. End Sub
  246. '--- Display the value of the DSRHolding property.
  247. Sub MHDSR_Click ()
  248.     If MSComm1.DSRHolding Then
  249.         Temp$ = "True"
  250.     Else
  251.         Temp$ = "False"
  252.     End If
  253.     MsgBox "DSRHolding = " + Temp$
  254. End Sub
  255. '*************************************************
  256. 'Sets the InputLen property. The InputLen property
  257. 'determines how many bytes of data are read each
  258. 'time Input is used to retreive data from the
  259. 'input buffer. Setting InputLen to 0 specifies that
  260. 'the entire contents of the buffer should br read.
  261. '*************************************************
  262. Sub MInputLen_Click ()
  263.     On Error Resume Next
  264.     Temp$ = InputBox$("Enter New InputLen:", "InputLen", Str$(MSComm1.InputLen))
  265.     If Len(Temp$) Then
  266.         MSComm1.InputLen = Val(Temp$)
  267.         If Err Then MsgBox Error$, 48
  268.     End If
  269. End Sub
  270. '--- Toggles the state of the port (open or closed).
  271. Sub MOpen_Click ()
  272.     On Error Resume Next
  273.     Dim OpenFlag
  274.     MSComm1.PortOpen = Not MSComm1.PortOpen
  275.     If Err Then MsgBox Error$, 48
  276.     OpenFlag = MSComm1.PortOpen
  277.     MOpen.Checked = OpenFlag
  278.     MSendText.Enabled = OpenFlag
  279.     MHangup.Enabled = OpenFlag
  280. End Sub
  281. Sub MOpenLog_Click ()
  282.    Dim replace
  283.    On Error Resume Next
  284.    '--- Get Log File name from the user
  285.    OpenLog.DialogTitle = "Open Communications Log File"
  286.    OpenLog.Filter = "Log Files (*.LOG)|*.log|All Files (*.*)|*.*"
  287.    Do
  288.       OpenLog.Filename = ""
  289.       OpenLog.Action = 1
  290.       If Err = CDERR_CANCEL Then Exit Sub
  291.       Temp$ = OpenLog.Filename
  292.       '--- If file already exists, do they want to
  293.       '    overwrite or add to it.
  294.       Ret = Len(Dir$(Temp$))
  295.       If Err Then
  296.          MsgBox Error$, 48
  297.          Exit Sub
  298.       End If
  299.       If Ret Then
  300.          replace = MsgBox("Replace existing file - " + Temp$ + "?", 35)
  301.       Else
  302.          replace = 0
  303.       End If
  304.    Loop While replace = 2
  305.    '--- User picked "Yes" button - Delete file.
  306.    If replace = 6 Then
  307.       Kill Temp$
  308.       If Err Then
  309.          MsgBox Error$, 48
  310.          Exit Sub
  311.       End If
  312.    End If
  313.    '--- Open the log file
  314.    hLogFile = FreeFile
  315.    Open Temp$ For Binary Access Write As hLogFile
  316.    If Err Then
  317.       MsgBox Error$, 48
  318.       Close hLogFile
  319.       hLogFile = 0
  320.       Exit Sub
  321.    Else
  322.       '--- Seek to the end so we append new data
  323.       Seek hLogFile, LOF(hLogFile) + 1
  324.    End If
  325.    Form1.Caption = "MSComm Terminal - " + OpenLog.Filetitle
  326.    MOpenLog.Enabled = False
  327.    MCloseLog.Enabled = True
  328. End Sub
  329. '*************************************************
  330. 'Sets the ParityReplace property. The
  331. 'ParityReplace property holds the character that
  332. 'will replace any incorrect characters that are
  333. 'received due to a parity error.
  334. '*************************************************
  335. Sub MParRep_Click ()
  336.     On Error Resume Next
  337.     Temp$ = InputBox$("Enter Replace Character", "ParityReplace", Form1.MSComm1.ParityReplace)
  338.     Form1.MSComm1.ParityReplace = Left$(Temp$, 1)
  339.     If Err Then MsgBox Error$, 48
  340. End Sub
  341. '*************************************************
  342. 'Sets the RThreshold property.  The RThreshold
  343. 'property determines how many bytes can arrive at
  344. 'the receive buffer before the OnComm event is
  345. 'triggered and the CommEvent property is set to
  346. 'MSCOMM_EV_RECEIVE
  347. '*************************************************
  348. Sub MRThreshold_Click ()
  349.     On Error Resume Next
  350.     Temp$ = InputBox$("Enter New RThreshold:", "RThreshold", Str$(MSComm1.RThreshold))
  351.     If Len(Temp$) Then
  352.         MSComm1.RThreshold = Val(Temp$)
  353.         If Err Then MsgBox Error$, 48
  354.     End If
  355. End Sub
  356. '*************************************************
  357. 'The OnComm event is used for trapping
  358. 'communications events and errors.
  359. '*************************************************
  360. Static Sub MSComm1_OnComm ()
  361.     Dim EVMsg$
  362.     Dim ERMsg$
  363.     '--- Branch according to the CommEvent Prop..
  364.     Select Case MSComm1.CommEvent
  365.         '--- Event messages
  366.         Case MSCOMM_EV_RECEIVE
  367.             ShowData Term, (MSComm1.Input)
  368.         Case MSCOMM_EV_SEND
  369.             
  370.         Case MSCOMM_EV_CTS
  371.             EVMsg$ = "Change in CTS Detected"
  372.         Case MSCOMM_EV_DSR
  373.             EVMsg$ = "Change in DSR Detected"
  374.         Case MSCOMM_EV_CD
  375.             EVMsg$ = "Change in CD Detected"
  376.         Case MSCOMM_EV_RING
  377.             EVMsg$ = "The Phone is Ringing"
  378.         Case MSCOMM_EV_EOF
  379.             EVMsg$ = "End of File Detected"
  380.         '--- Error messages
  381.         Case MSCOMM_ER_BREAK
  382.             EVMsg$ = "Break Received"
  383.         Case MSCOMM_ER_CTSTO
  384.             ERMsg$ = "CTS Timeout"
  385.         Case MSCOMM_ER_DSRTO
  386.             ERMsg$ = "DSR Timeout"
  387.         Case MSCOMM_ER_FRAME
  388.             EVMsg$ = "Framing Error"
  389.         Case MSCOMM_ER_OVERRUN
  390.             ERMsg$ = "Overrun Error"
  391.         Case MSCOMM_ER_CDTO
  392.             ERMsg$ = "Carrier Detect Timeout"
  393.         Case MSCOMM_ER_RXOVER
  394.             ERMsg$ = "Receive Buffer Overflow"
  395.         Case MSCOMM_ER_RXPARITY
  396.             EVMsg$ = "Parity Error"
  397.         Case MSCOMM_ER_TXFULL
  398.             ERMsg$ = "Transmit Buffer Full"
  399.         Case Else
  400.             ERMsg$ = "Unknown error or event"
  401.     End Select
  402.     If Len(EVMsg$) Then
  403.         '--- Display event messages in label
  404.         Label1.Caption = EVMsg$
  405.         EVMsg$ = ""
  406.     ElseIf Len(ERMsg$) Then
  407.         '--- Display error messages in an alert
  408.         '    message box.
  409.         Beep
  410.         Ret = MsgBox(ERMsg$, 1, "Press Cancel to Quit, Ok to ignore.")
  411.         ERMsg$ = ""
  412.         '--- If Cancel (2) was pressed
  413.         If Ret = 2 Then
  414.             MSComm1.PortOpen = 0    'Close the port and quit
  415.         End If
  416.     End If
  417. End Sub
  418. Sub MSendText_Click ()
  419.    On Error Resume Next
  420.    Dim hSend, BSize, LF&
  421.    MSendText.Enabled = False
  422.    '--- Get Text File name from the user
  423.    OpenLog.DialogTitle = "Send Text File"
  424.    OpenLog.Filter = "Text Files (*.TXT)|*.txt|All Files (*.*)|*.*"
  425.    Do
  426.       OpenLog.Filename = ""
  427.       OpenLog.Action = 1
  428.       If Err = CDERR_CANCEL Then Exit Sub
  429.       Temp$ = OpenLog.Filename
  430.       '--- If file doesn't exist, go back
  431.       Ret = Len(Dir$(Temp$))
  432.       If Err Then
  433.          MsgBox Error$, 48
  434.          MSendText.Enabled = True
  435.          Exit Sub
  436.       End If
  437.       If Ret Then
  438.          Exit Do
  439.       Else
  440.          MsgBox Temp$ + " not found!", 48
  441.       End If
  442.    Loop
  443.    '--- Open the log file
  444.    hSend = FreeFile
  445.    Open Temp$ For Binary Access Read As hSend
  446.    If Err Then
  447.       MsgBox Error$, 48
  448.    Else
  449.       '--- Display the Cancel dialog box
  450.       CancelSend = False
  451.       Form2.Label1.Caption = "Transmitting Text File - " + Temp$
  452.       Form2.Show
  453.       
  454.       '--- Read the file in blocks the size of our
  455.       '    transmit buffer.
  456.       BSize = MSComm1.OutBufferSize
  457.       LF& = LOF(hSend)
  458.       Do Until EOF(hSend) Or CancelSend
  459.          '--- Don't read too much at the end
  460.          If LF& - Loc(hSend) <= BSize Then
  461.             BSize = LF& - Loc(hSend) + 1
  462.          End If
  463.       
  464.          '--- Read a block of data
  465.          Temp$ = Space$(BSize)
  466.          Get hSend, , Temp$
  467.       
  468.          '--- Transmit the block
  469.          MSComm1.Output = Temp$
  470.          If Err Then
  471.             MsgBox Error$, 48
  472.             Exit Do
  473.          End If
  474.       
  475.          '--- Wait for all the data to be sent
  476.          Do
  477.             Ret = DoEvents()
  478.          Loop Until MSComm1.OutBufferCount = 0 Or CancelSend
  479.       Loop
  480.    End If
  481.    Close hSend
  482.    MSendText.Enabled = True
  483.    CancelSend = True
  484.    Form2.Hide
  485. End Sub
  486. Sub MSettings_Click ()
  487.     '--- Show the communications settings form
  488.     ConfigScrn.Show
  489. End Sub
  490. '*************************************************
  491. 'Sets the SThreshold property. The SThreshold
  492. 'property determines how many characters (at most)
  493. 'have to be waiting in the output buffer before
  494. 'the CommEvent property is set to EV_SEND and the
  495. 'OnComm event is triggered.
  496. '*************************************************
  497. Sub MSThreshold_Click ()
  498.     On Error Resume Next
  499.     Temp$ = InputBox$("Enter New SThreshold Value", "SThreshold", Str$(MSComm1.SThreshold))
  500.     If Len(Temp$) Then
  501.         MSComm1.SThreshold = Val(Temp$)
  502.         If Err Then MsgBox Error$, 48
  503.     End If
  504. End Sub
  505. '**************************************************
  506. 'Adds data to the Term control's .Text property.
  507. 'Also filters control characters such as Back Space
  508. 'Charriage Return and Line Feed, and writes data to
  509. 'an open log file.
  510. 'Back Space chars. delete the character to the left,
  511. 'either in the .Text property, or the passed string.
  512. 'Line Feed characters are appended to all Charriage
  513. 'Returns.  The size of the Term control's Text
  514. 'property is also monitored so that it never
  515. 'excedes 16384 characters.
  516. '**************************************************
  517. Static Sub ShowData (Term As Control, Dta$)
  518.     On Error Resume Next
  519.     Dim Nd, I
  520.     '--- Make sure the existing text doesn't get
  521.     '    too large.
  522.     Nd = Len(Term.Text)
  523.     If Nd >= 16384 Then
  524.        Term.Text = Mid$(Term.Text, 4097)
  525.        Nd = Len(Term.Text)
  526.     End If
  527.     '--- Point to the end of Term's data
  528.     Term.SelStart = Nd
  529.     '--- Filter/handle Back Space characters
  530.     Do
  531.        I = InStr(Dta$, Chr$(8))
  532.        If I Then
  533.           If I = 1 Then
  534.              Term.SelStart = Nd - 1
  535.              Term.SelLength = 1
  536.              Dta$ = Mid$(Dta$, I + 1)
  537.           Else
  538.              Dta$ = Left$(Dta$, I - 2) + Mid$(Dta$, I + 1)
  539.           End If
  540.        End If
  541.     Loop While I
  542.     '--- Elliminate Line Feeds (put back below)
  543.     Do
  544.        I = InStr(Dta$, Chr$(10))
  545.        If I Then
  546.           Dta$ = Left$(Dta$, I - 1) + Mid$(Dta$, I + 1)
  547.        End If
  548.     Loop While I
  549.     '--- Make sure all Charriage Returns have a
  550.     '    Line Feed
  551.     I = 1
  552.     Do
  553.        I = InStr(I, Dta$, Chr$(13))
  554.        If I Then
  555.           Dta$ = Left$(Dta$, I) + Chr$(10) + Mid$(Dta$, I + 1)
  556.           I = I + 1
  557.        End If
  558.     Loop While I
  559.     '--- Add the filtered data to .Text
  560.     Term.SelText = Dta$
  561.     '--- Log data to file if requested
  562.     If hLogFile Then
  563.        I = 2
  564.        Do
  565.           Err = 0
  566.           Put hLogFile, , Dta$
  567.           If Err Then
  568.              I = MsgBox(Error$, 21)
  569.              If I = 2 Then
  570.                 MCloseLog_Click
  571.              End If
  572.           End If
  573.        Loop While I <> 2
  574.     End If
  575. End Sub
  576. '*************************************************
  577. 'Key strokes trapped here are sent to the Comm
  578. 'control where they are echoed back via the
  579. 'OnComm/MSCOMM_EV_RECEIVE event, and displayed
  580. 'through the ShowData procedure.
  581. '*************************************************
  582. Sub Term_KeyPress (KeyAscii As Integer)
  583.     '--- If the port is openned,
  584.     If MSComm1.PortOpen Then
  585.        '--- Send the key stroke to the port
  586.        MSComm1.Output = Chr$(KeyAscii)
  587.        '--- Unless Echo is on, there is no need to
  588.        '    let the Text control display the key.
  589.        If Not Echo Then KeyAscii = 0
  590.     End If
  591. End Sub
  592.