home *** CD-ROM | disk | FTP | other *** search
/ Master 95 #1 / MASTER95_1.iso / microsof / vbasic4 / vb4-6.cab / vbterm.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-07-26  |  18.2 KB  |  590 lines

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