zzzcccj4649 发表于 2011-12-7 15:43:48

刚学VB,有问题请教大家

http://cache.amobbs.com/bbs_upload782111/files_48/ourdev_702487BRX9JR.JPG
(原文件名:12.JPG)

http://cache.amobbs.com/bbs_upload782111/files_48/ourdev_702488PVW7OM.JPG
设置不成功 (原文件名:13.JPG)

---------------------------------------------------------------------
代码
----------------------
Private Sub Form_Load()
aaa: Text1.Text = "0"
If Option1.Value = 1 Then

If Mscomm1.PortOpen = False Then
      Mscomm1.CommPort = 1
      Mscomm1.Settings = "4800,e,8,1"
      Mscomm1.PortOpen = True
      Mscomm1.RThreshold = 5
      Text1.Text = "1"
      
End If
Else

End If

If Option2.Value = 1 Then

If Mscomm1.PortOpen = False Then
      Mscomm1.CommPort = 2
      Mscomm1.Settings = "4800,e,8,1"
      Mscomm1.PortOpen = True
      Mscomm1.RThreshold = 5
      Text1.Text = "2"
End If
Else

End If

If Option3.Value = 1 Then

If Mscomm1.PortOpen = False Then
      Mscomm1.CommPort = 3
      Mscomm1.Settings = "4800,e,8,1"
      Mscomm1.PortOpen = True
      Mscomm1.RThreshold = 5
      Text1.Text = "3"
End If
Else

End If

End Sub
---------------------------------------------------------------------
说明:
程序运行时,串口设置不成功。
代码里面的Text1.Text =是为了调试方便而加入的。

请教大家:这代码错在哪里了?如何才能设置串口成功?

.titrwh 发表于 2011-12-7 15:50:30

这个应该放在定时器里吧

jianchangd 发表于 2011-12-7 19:09:26

这个Option1.Value = 1应该改成Option1.Value = True

marshallemon 发表于 2011-12-7 19:39:49

你把这段判断程序写到Form_Load()中肯定是不行的,Form_Load()是为了初始化一些东西的,只在串口加载时它才执行一次,DEBUG一下看看就知道了

dong889 发表于 2011-12-7 19:56:49

回复【3楼】marshallemon
-----------------------------------------------------------------------

正解
把每个If Option1.Value = 1 Then...判断语句用各自的Option1_Click()事件代替!不要放在Form_Load()中,那个只执行一次的~Option1_Click()每点击一下就执行一遍里面的代码~~


回复【2楼】jianchangd
-----------------------------------------------------------------------

Option1.Value = 1 这样,可以的吧~
页: [1]
查看完整版本: 刚学VB,有问题请教大家