⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmsetupcom2.frm

📁 这是一本学习串口编程喝计算机监控的好书里面是用VB开发的源代码
💻 FRM
📖 第 1 页 / 共 2 页
字号:
      Height          =   1650
      Left            =   1560
      TabIndex        =   30
      Top             =   1320
      Width           =   1650
      Begin VB.Label Label2 
         Caption         =   "Group Parity"
         Height          =   255
         Left            =   120
         TabIndex        =   32
         Top             =   840
         Width           =   975
      End
      Begin VB.Label lblLength 
         Caption         =   "Length"
         Height          =   255
         Left            =   240
         TabIndex        =   31
         ToolTipText     =   "Error Records"
         Top             =   360
         Width           =   495
      End
   End
   Begin VB.Label Label1 
      Caption         =   "Port:"
      Height          =   315
      Left            =   1680
      TabIndex        =   25
      Top             =   3240
      Width           =   375
   End
End
Attribute VB_Name = "frmSetupCOM2"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private iFlow As Integer

Private Sub LoadPropertySettings()
  'only for serial port
  Dim I As Integer
  Dim strSettings As String

  ' Load Port Settings
  For I = 1 To 16
    cboPort.AddItem "Com" & Trim$(Str$(I))
  Next I

  ' Load Speed Settings
  cboSpeed.AddItem "110"
  cboSpeed.AddItem "300"
  cboSpeed.AddItem "600"
  cboSpeed.AddItem "1200"
  cboSpeed.AddItem "2400"
  cboSpeed.AddItem "4800"
  cboSpeed.AddItem "9600"
  cboSpeed.AddItem "14400"
  cboSpeed.AddItem "19200"
  cboSpeed.AddItem "28800"
  cboSpeed.AddItem "38400"
  cboSpeed.AddItem "56000"
  cboSpeed.AddItem "57600"
  cboSpeed.AddItem "115200"
  cboSpeed.AddItem "128000"
  cboSpeed.AddItem "256000"

  ' Load Data Bit Settings
  cboDataBits.AddItem "4"
  cboDataBits.AddItem "5"
  cboDataBits.AddItem "6"
  cboDataBits.AddItem "7"
  cboDataBits.AddItem "8"
  
  ' Load Parity Settings
  cboParity.AddItem "Even"
  cboParity.AddItem "Odd"
  cboParity.AddItem "None"
  cboParity.AddItem "Mark"
  cboParity.AddItem "Space"
  
  ' Load Stop Bit Settings
  cboStopBits.AddItem "1"
  cboStopBits.AddItem "1.5"
  cboStopBits.AddItem "2"
    
  ' Load Block Parities
  cboGroupParity.AddItem "None"
  cboGroupParity.AddItem "Xor"
  cboGroupParity.AddItem "Add"
  cboGroupParity.AddItem "Crc"
  cboGroupParity.AddItem "CheckSum"
  cboGroupParity.ListIndex = nBlockParity2

  ' Set Default Settings
  With frmMain.MSComm2
    strSettings = .Settings
    cboSpeed.Text = GetNoString(strSettings, ",", 0)
    
    Select Case GetNoString(strSettings, ",", 1)
      Case "e"
        cboParity.ListIndex = 0
      Case "o"
        cboParity.ListIndex = 1
      Case "n"
        cboParity.ListIndex = 2
      Case "m"
        cboParity.ListIndex = 3
      Case "s"
        cboParity.ListIndex = 4
    End Select
      
    cboDataBits.Text = GetNoString(strSettings, ",", 2)
    cboStopBits.Text = GetNoString(strSettings + ",", ",", 3)
    
    cboPort.ListIndex = .CommPort - 1
  
    optFlow(.Handshaking).Value = True
      
    chkDTR.Value = IIf(.DTREnable = True, 1, 0)
    chkRTS.Value = IIf(.RTSEnable = True, 1, 0)
      
    If .InputMode = comInputModeBinary Then
      optMode(1).Value = True
    Else
      optMode(0).Value = True
    End If
  End With
End Sub

Private Sub cmdCancel_Click()
  Unload Me
End Sub

Private Sub cmdOK_Click()
  Dim nOldPort As Integer
  Dim nNewPort As Integer
  Dim bReOpen As Boolean
  Dim strTmp As String
  Dim strDate As String
  Dim bTimerEnable As Boolean
  Dim strSettings As String
  On Error Resume Next
    
  With frmMain.MSComm2
    nOldPort = .CommPort
    nNewPort = cboPort.ListIndex + 1

    If nNewPort <> nOldPort Then                   ' If the port number changes, close the old port.
      If .PortOpen Then
        .PortOpen = False
        
        strSettings = .Settings
        ResultString = ResultString + GetTimeStamp(0) + "Current serial port settings: " _
                  + "COM" + Trim(Str(.CommPort)) + "; " + strSettings + "; " + _
                  IIf(.PortOpen = True, "Open", "Close")
        Call DisplayString(frmMain.txtResult, ResultString)
                
        frmMain.SetComStatus
        bReOpen = True
      End If
  
      .CommPort = nNewPort          ' Set the new port number.
    End If

    .Settings = Trim$(cboSpeed.Text) & "," & Left$(cboParity.Text, 1) _
          & "," & Trim$(cboDataBits.Text) & "," & Trim$(cboStopBits.Text)

    If Err Then
      MsgBox Error$, vbExclamation + vbOKOnly
      GoTo ERROR_MARK
    End If

    .DTREnable = IIf(chkDTR.Value = 1, True, False)
    .RTSEnable = IIf(chkRTS.Value = 1, True, False)
    .Handshaking = iFlow
    If iFlow >= 2 Then .RTSEnable = True
    
    If Err Then
      MsgBox Error$, vbExclamation + vbOKOnly
      GoTo ERROR_MARK
    End If

    strSettings = .Settings
    If bReOpen = True Then
      Call OpenAndAdjustPort(frmMain.MSComm2)
      
      If Err Then
        MsgBox Error$, vbCritical + vbOKOnly
        .CommPort = nOldPort
        GoTo ERROR_MARK
      End If
      
      ResultString = ResultString + GetTimeStamp(0) + "Current serial port settings: " _
                + "COM" + Trim(Str(.CommPort)) + "; " + strSettings + "; " + _
                IIf(.PortOpen = True, "Open", "Close")
      Call DisplayString(frmMain.txtResult, ResultString)
    End If
  End With
  
  With frmMain.commTimer2
    strTmp = Trim(txtMaxChars.Text)
    bTimerEnable = .Enabled
    .Enabled = False
    If Len(strTmp) > 0 Then
      nMaxReceiveChars2 = Val(strTmp)
      .Interval = nMaxReceiveChars2 * 10 * 1000 \ _
              Val(GetNoString(strSettings, ",", 0)) + 5
    Else
      .Interval = 5
    End If
    .Enabled = bTimerEnable
  End With
    
  Select Case cboGroupParity.Text
    Case "None"
      nBlockParity2 = CHK_NONE
    Case "Xor"
      nBlockParity2 = CHK_XOR
    Case "Add"
      nBlockParity2 = CHK_ADD
    Case "Crc"
      nBlockParity2 = CHK_CRC
    Case "CheckSum"
      nBlockParity2 = CHK_BCS
  End Select
    
  SaveSetting App.Title, "Properties2", "Settings", frmMain.MSComm2.Settings
  SaveSetting App.Title, "Properties2", "CommPort", frmMain.MSComm2.CommPort
  SaveSetting App.Title, "Properties2", "Handshaking", frmMain.MSComm2.Handshaking
  SaveSetting App.Title, "Properties2", "EndMark", Str(nEndMark2)
  SaveSetting App.Title, "Properties2", "MaxChars", Str(nMaxReceiveChars2)
  SaveSetting App.Title, "Properties2", "BlockParity", Str(nBlockParity2)
  
ERROR_MARK:
  frmMain.SetComStatus
  Unload Me
End Sub

Private Sub Form_Load()
  'Set the form's size
  Call SetForm(Me, 500, 700)

  ' Load current property settings
  LoadPropertySettings
  
  txtMaxChars.Text = Trim(Str(nMaxReceiveChars2))
    
  Select Case nEndMark2
    Case ADD_NONE
      optEndMark(0).Value = True
    Case ADD_CR
      optEndMark(1).Value = True
    Case ADD_CRLF
      optEndMark(2).Value = True
  End Select
End Sub

Private Sub optEndMark_Click(Index As Integer)
  nEndMark2 = Index
End Sub

Private Sub optMode_Click(Index As Integer)
  With frmMain.MSComm2
    If Index = 1 Then
      .InputMode = comInputModeBinary
    Else
      .InputMode = comInputModeText
    End If
  End With
End Sub

Private Sub optFlow_Click(Index As Integer)
  iFlow = Index
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -