frmProps.frm
上传用户:song66628
上传日期:2013-07-20
资源大小:7k
文件大小:11k
源码类别:

串口编程

开发平台:

Visual Basic

  1. VERSION 5.00
  2. Begin VB.Form frmProperties 
  3.    BorderStyle     =   3  'Fixed Dialog
  4.    Caption         =   "PORT SETUP"
  5.    ClientHeight    =   4920
  6.    ClientLeft      =   2775
  7.    ClientTop       =   1740
  8.    ClientWidth     =   5955
  9.    Icon            =   "frmProps.frx":0000
  10.    LinkTopic       =   "Form3"
  11.    MaxButton       =   0   'False
  12.    MinButton       =   0   'False
  13.    ScaleHeight     =   4920
  14.    ScaleWidth      =   5955
  15.    ShowInTaskbar   =   0   'False
  16.    StartUpPosition =   2  '屏幕中心
  17.    Begin VB.CommandButton cmdCancel 
  18.       Caption         =   "CANCAL"
  19.       Height          =   405
  20.       Left            =   3300
  21.       TabIndex        =   21
  22.       Top             =   4260
  23.       Width           =   1080
  24.    End
  25.    Begin VB.CommandButton cmdOK 
  26.       Caption         =   "OK"
  27.       Default         =   -1  'True
  28.       Height          =   420
  29.       Left            =   1620
  30.       MaskColor       =   &H00000000&
  31.       TabIndex        =   20
  32.       Top             =   4260
  33.       Width           =   1080
  34.    End
  35.    Begin VB.Frame fraSettings 
  36.       BorderStyle     =   0  'None
  37.       Height          =   3615
  38.       Left            =   420
  39.       TabIndex        =   0
  40.       Top             =   300
  41.       Width           =   5145
  42.       Begin VB.Frame Frame7 
  43.          Caption         =   "&Echo"
  44.          Height          =   870
  45.          Left            =   2880
  46.          TabIndex        =   17
  47.          Top             =   420
  48.          Width           =   1590
  49.          Begin VB.OptionButton optEcho 
  50.             Caption         =   "ON"
  51.             Height          =   195
  52.             Index           =   1
  53.             Left            =   795
  54.             MaskColor       =   &H00000000&
  55.             TabIndex        =   19
  56.             Top             =   420
  57.             Width           =   555
  58.          End
  59.          Begin VB.OptionButton optEcho 
  60.             Caption         =   "OFF"
  61.             Height          =   315
  62.             Index           =   0
  63.             Left            =   135
  64.             MaskColor       =   &H00000000&
  65.             TabIndex        =   18
  66.             Top             =   360
  67.             Width           =   615
  68.          End
  69.       End
  70.       Begin VB.ComboBox cboPort 
  71.          Height          =   300
  72.          Left            =   900
  73.          Style           =   2  'Dropdown List
  74.          TabIndex        =   15
  75.          Top             =   150
  76.          Width           =   1425
  77.       End
  78.       Begin VB.Frame fraConnection 
  79.          Caption         =   "OPTION"
  80.          Height          =   1770
  81.          Left            =   180
  82.          TabIndex        =   8
  83.          Top             =   1635
  84.          Width           =   2325
  85.          Begin VB.ComboBox cboDataBits 
  86.             Height          =   300
  87.             Left            =   1050
  88.             Style           =   2  'Dropdown List
  89.             TabIndex        =   11
  90.             Top             =   330
  91.             Width           =   1140
  92.          End
  93.          Begin VB.ComboBox cboParity 
  94.             Height          =   300
  95.             Left            =   1050
  96.             Style           =   2  'Dropdown List
  97.             TabIndex        =   10
  98.             Top             =   810
  99.             Width           =   1140
  100.          End
  101.          Begin VB.ComboBox cboStopBits 
  102.             Height          =   300
  103.             Left            =   1050
  104.             Style           =   2  'Dropdown List
  105.             TabIndex        =   9
  106.             Top             =   1260
  107.             Width           =   1140
  108.          End
  109.          Begin VB.Label Label3 
  110.             AutoSize        =   -1  'True
  111.             Caption         =   "BIT:"
  112.             Height          =   180
  113.             Left            =   180
  114.             TabIndex        =   14
  115.             Top             =   375
  116.             Width           =   360
  117.          End
  118.          Begin VB.Label Label4 
  119.             AutoSize        =   -1  'True
  120.             Caption         =   "PARITY:"
  121.             Height          =   180
  122.             Left            =   180
  123.             TabIndex        =   13
  124.             Top             =   855
  125.             Width           =   630
  126.          End
  127.          Begin VB.Label Label5 
  128.             AutoSize        =   -1  'True
  129.             Caption         =   "STOP:"
  130.             Height          =   180
  131.             Left            =   180
  132.             TabIndex        =   12
  133.             Top             =   1320
  134.             Width           =   450
  135.          End
  136.       End
  137.       Begin VB.Frame Frame1 
  138.          Caption         =   "BAUD"
  139.          Height          =   870
  140.          Left            =   180
  141.          TabIndex        =   6
  142.          Top             =   630
  143.          Width           =   2340
  144.          Begin VB.ComboBox cboSpeed 
  145.             Height          =   300
  146.             Left            =   375
  147.             Style           =   2  'Dropdown List
  148.             TabIndex        =   7
  149.             Top             =   330
  150.             Width           =   1695
  151.          End
  152.       End
  153.       Begin VB.Frame Frame5 
  154.          Caption         =   "Flow"
  155.          Height          =   1770
  156.          Left            =   2940
  157.          TabIndex        =   1
  158.          Top             =   1620
  159.          Width           =   1620
  160.          Begin VB.OptionButton optFlow 
  161.             Caption         =   "NONE"
  162.             Height          =   255
  163.             Index           =   0
  164.             Left            =   150
  165.             MaskColor       =   &H00000000&
  166.             TabIndex        =   5
  167.             Top             =   345
  168.             Width           =   855
  169.          End
  170.          Begin VB.OptionButton optFlow 
  171.             Caption         =   "SOFT"
  172.             Height          =   255
  173.             Index           =   1
  174.             Left            =   150
  175.             MaskColor       =   &H00000000&
  176.             TabIndex        =   4
  177.             Top             =   645
  178.             Width           =   1095
  179.          End
  180.          Begin VB.OptionButton optFlow 
  181.             Caption         =   "HARD"
  182.             Height          =   255
  183.             Index           =   2
  184.             Left            =   150
  185.             MaskColor       =   &H00000000&
  186.             TabIndex        =   3
  187.             Top             =   945
  188.             Width           =   1300
  189.          End
  190.          Begin VB.OptionButton optFlow 
  191.             Caption         =   "BOTH"
  192.             Height          =   255
  193.             Index           =   3
  194.             Left            =   150
  195.             MaskColor       =   &H00000000&
  196.             TabIndex        =   2
  197.             Top             =   1275
  198.             Width           =   1155
  199.          End
  200.       End
  201.       Begin VB.Label Label1 
  202.          AutoSize        =   -1  'True
  203.          Caption         =   "PORT"
  204.          Height          =   180
  205.          Left            =   330
  206.          TabIndex        =   16
  207.          Top             =   180
  208.          Width           =   360
  209.       End
  210.    End
  211. End
  212. Attribute VB_Name = "frmProperties"
  213. Attribute VB_GlobalNameSpace = False
  214. Attribute VB_Creatable = False
  215. Attribute VB_PredeclaredId = True
  216. Attribute VB_Exposed = False
  217. Private iFlow As Integer, iTempEcho As Boolean
  218. Sub LoadPropertySettings()
  219. Dim i As Integer, Settings As String, Offset As Integer
  220. ' Load Port Settings
  221. For i = 1 To 16
  222.     cboPort.AddItem "Com" & Trim$(Str$(i))
  223. Next i
  224. ' Load Speed Settings
  225. cboSpeed.AddItem "110"
  226. cboSpeed.AddItem "300"
  227. cboSpeed.AddItem "600"
  228. cboSpeed.AddItem "1200"
  229. cboSpeed.AddItem "2400"
  230. cboSpeed.AddItem "4800"
  231. cboSpeed.AddItem "9600"
  232. cboSpeed.AddItem "14400"
  233. cboSpeed.AddItem "19200"
  234. cboSpeed.AddItem "28800"
  235. cboSpeed.AddItem "38400"
  236. cboSpeed.AddItem "56000"
  237. cboSpeed.AddItem "57600"
  238. cboSpeed.AddItem "115200"
  239. cboSpeed.AddItem "128000"
  240. cboSpeed.AddItem "256000"
  241. ' Load Data Bit Settings
  242. cboDataBits.AddItem "4"
  243. cboDataBits.AddItem "5"
  244. cboDataBits.AddItem "6"
  245. cboDataBits.AddItem "7"
  246. cboDataBits.AddItem "8"
  247. ' Load Parity Settings
  248. cboParity.AddItem "Even"
  249. cboParity.AddItem "Odd"
  250. cboParity.AddItem "None"
  251. cboParity.AddItem "Mark"
  252. cboParity.AddItem "Space"
  253. ' Load Stop Bit Settings
  254. cboStopBits.AddItem "1"
  255. cboStopBits.AddItem "1.5"
  256. cboStopBits.AddItem "2"
  257. ' Set Default Settings
  258. Settings = Form1.MSComm1.Settings
  259. ' In all cases the right most part of Settings will be 1 character
  260. ' except when there are 1.5 stop bits.
  261. If InStr(Settings, ".") > 0 Then
  262.     Offset = 2
  263. Else
  264.     Offset = 0
  265. End If
  266. cboSpeed.Text = Left$(Settings, Len(Settings) - 6 - Offset)
  267. Select Case Mid$(Settings, Len(Settings) - 4 - Offset, 1)
  268. Case "e"
  269.     cboParity.ListIndex = 0
  270. Case "m"
  271.     cboParity.ListIndex = 1
  272. Case "n"
  273.     cboParity.ListIndex = 2
  274. Case "o"
  275.     cboParity.ListIndex = 3
  276. Case "s"
  277.     cboParity.ListIndex = 4
  278. End Select
  279. cboDataBits.Text = Mid$(Settings, Len(Settings) - 2 - Offset, 1)
  280. cboStopBits.Text = Right$(Settings, 1 + Offset)
  281.     
  282. cboPort.ListIndex = Form1.MSComm1.CommPort - 1
  283. optFlow(Form1.MSComm1.Handshaking).Value = True
  284. If Echo Then
  285.     optEcho(1).Value = True
  286. Else
  287.     optEcho(0).Value = True
  288. End If
  289. End Sub
  290. Private Sub cmdCancel_Click()
  291. Unload Me
  292. End Sub
  293. Private Sub cmdOK_Click()
  294. Dim OldPort As Integer, ReOpen As Boolean
  295. On Error Resume Next
  296. Echo = iTempEcho
  297. OldPort = Form1.MSComm1.CommPort
  298. NewPort = cboPort.ListIndex + 1
  299. If NewPort <> OldPort Then                   ' If the port number changes, close the old port.
  300.     If Form1.MSComm1.PortOpen Then
  301.            Form1.MSComm1.PortOpen = False
  302.            ReOpen = True
  303.     End If
  304.     Form1.MSComm1.CommPort = NewPort          ' Set the new port number.
  305.     
  306.     If Err = 0 Then
  307.         If ReOpen Then
  308.             Form1.MSComm1.PortOpen = True
  309.         End If
  310.     End If
  311.         
  312.     If Err Then
  313.         MsgBox Error$, 48
  314.         Form1.MSComm1.CommPort = OldPort
  315.         Exit Sub
  316.     End If
  317. End If
  318. Form1.MSComm1.Settings = Trim$(cboSpeed.Text) & "," & Left$(cboParity.Text, 1) _
  319.     & "," & Trim$(cboDataBits.Text) & "," & Trim$(cboStopBits.Text)
  320. If Err Then
  321.     MsgBox Error$, 48
  322.     Exit Sub
  323. End If
  324. Form1.MSComm1.Handshaking = iFlow
  325. If Err Then
  326.     MsgBox Error$, 48
  327.     Exit Sub
  328. End If
  329. SaveSetting App.Title, "Properties", "Settings", Form1.MSComm1.Settings
  330. SaveSetting App.Title, "Properties", "CommPort", Form1.MSComm1.CommPort
  331. SaveSetting App.Title, "Properties", "Handshaking", Form1.MSComm1.Handshaking
  332. Form1.Label2 = Form1.MSComm1.Settings
  333. Form1.Label4 = Form1.MSComm1.CommPort
  334. Form1.Label6 = Form1.MSComm1.Handshaking
  335. Unload Me
  336. End Sub
  337. Private Sub Form_Load()
  338. ' Set the form's size
  339. Me.Left = (Screen.Width - Me.Width) / 2
  340. Me.Top = (Screen.Height - Me.Height) / 2
  341. ' Make sure the frame is the top most control
  342. fraSettings.ZOrder
  343. ' Load current property settings
  344. LoadPropertySettings
  345. End Sub
  346. Private Sub optEcho_Click(Index As Integer)
  347. If Index = 1 Then
  348.     iTempEcho = True
  349. Else
  350.     iTempEcho = False
  351. End If
  352. End Sub
  353. Private Sub optFlow_Click(Index As Integer)
  354. iFlow = Index
  355. End Sub