注册 登录
编程论坛 VB6论坛

求助,在win7下的串口通信问题

outhearth 发布于 2015-05-25 23:22, 730 次点击
本人算是不会任何高级编程语言,请各位老大不要因为我问的问题低级嘲笑我。
    因为需要非常简单的控制下位设备,我就在网上查了点VB的资料。
    用了一个下午做了一个“四个按钮,一个文本框”的小工程,通过RS232串口发送4个指令,下位机也算正常工作了,虽然文本框里没收到下位机发过来的反馈,不过心里还是非常高兴的。后来我就离开了电脑,等回来后发现程序崩溃,然后再打开后,报错,串口被占用。用任务管理器一看,那个我命名的“工程1.exe”的小软件还在运行,而且还结束不掉。

程序在公司电脑上,没弄回来,我在照着原来的改改:

Private Sub Command1_Click()
  MSComm1.Output = A
End Sub
-----
Private Sub Command2_Click()
  MSComm1.Output = B
End Sub
-----
Private Sub Command3_Click()
  MSComm1.Output = C
End Sub
-----
Private Sub Command4_Click()
  MSComm1.Output = D
End Sub
-----
Private Sub Form_Load()
   = 1
  MSComm1.Settings = "9600,E,7,2"
  'MSComm1.InBufferSize = 8
  'MSComm1.OutBufferSize = 8
  MSComm1.Handshaking = comNone

  If MSComm1.PortOpen = True Then
    MSComm1.PortOpen = False
  End If

  MSComm1.RThreshold = 1
  MSComm1.SThreshold = 1
  MSComm1.PortOpen = True
  MSComm1.InputLen = 0
  MSComm1.InputMode = comInputModeText

  If MSComm1.PortOpen = False Then
    MSComm1.PortOpen = True
  End If

  Dim buf$
  buf = Trim(MSComm1.Input)
  If Len(buf) = 0 Then           
    Text1.Text = "empty"
  Else
    Text1.Text = buf
  End If
End Sub
请各位大侠指教下:
1.为什么端口被占用
2.为什么TEXT不能收到东西呢,里面既没有A、B、C、D,也没有empty。我看资料里说是什么事件驱动,怎么改呢?

[ 本帖最后由 outhearth 于 2015-5-25 23:24 编辑 ]
8 回复
#2
wmf20142015-05-26 06:48
我理解是:你的“A、B、C、D”是发给下位机的,难道下位机设置的就是把这四个字符反送回?,你从你代码看你根本没有写数据接受代码。
#3
lianyicq2015-05-26 08:40
#4
outhearth2015-05-26 20:29
回复 2楼 wmf2014
我今天改了下,用的onComm事件,接收的字符不是很好,可能是因为没有开始符和结束符的原因。

在上面那个例子里,我就直接用的
Dim buf$
  buf = Trim(MSComm1.Input)
  If Len(buf) = 0 Then           
    Text1.Text = "empty"
  Else
    Text1.Text = buf
  End If

接受不到,或者丢失数据。
#5
outhearth2015-05-26 21:28
回复 3楼 lianyicq
谢谢。
版主,我想在TEXT框里限制输入的数值,应该怎么弄
#6
lianyicq2015-05-28 15:25
回复 5楼 outhearth
举个具体的想限制输入什么的例子.
#7
outhearth2015-05-28 20:50
回复 6楼 lianyicq
因为要控制电机的位置,我希望操作人员里面只能输入数字,可以带小数点,但是要有范围,比如10.00~450.00,不能超过范围。

第二个就是想在TEXT里显示背景,比如“输入位置:______MM”,我选择TEXT框的时候,也不能消失。
#8
lianyicq2015-05-29 09:44
回复 7楼 outhearth
这个要求感觉不简单。只输入数字、小数点和退格键容易实现。
Private Sub Text1_KeyPress(KeyAscii As Integer)
  If KeyAscii <> 46 And KeyAscii <> 8 And Not (KeyAscii < 58 And KeyAscii > 47) Then KeyAscii = 0: Exit Sub
End Sub
其它功能不容易。
有时间继续试试
...
没有好好规划,写完我也凌乱了

[ 本帖最后由 lianyicq 于 2015-5-29 10:45 编辑 ]
#9
lianyicq2015-05-29 10:44
程序代码:
Option Explicit
Dim TextStr As String
Dim TempKey As Integer

Private Sub Command1_Click()
  Form1.Caption = Val(TextStr)
End Sub

Private Sub Form_Load() 'Initialize Text1
  Text1.Text = "输入位置: " & " mm"
End Sub


Private Sub Text1_Click()
  Text1.SelStart = Len(Text1.Text) - 3
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) 'Disable other operatable keys,such as "Delete","Left",...
  KeyCode = 0
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
  Dim temp As String
  TempKey = KeyAscii 'Use TempKey replace KeyAscii,and KeyAscii set to 0.
  KeyAscii = 0
  If TempKey = 8 And Len(TextStr) > 0 Then 'Press Backspace to delete the last character in TextStr.The limitation is the length of TextStr no less than 0
    TextStr = Mid(TextStr, 1, Len(TextStr) - 1)
  Else
    If TempKey = 46 Or (TempKey < 58 And TempKey > 47) Then 'Not  Backspace pressed,only "0" to "9" or "." add to end of TextStr
      temp = TextStr & Chr(TempKey)
      If Val(temp) < 450 Then 'If no more than upper limit
        If InStr(1, temp, ".") = 0 Or InStr(1, temp, ".") > Len(temp) - 3 Then 'Check exist or not exist "." in TextStr.
          TextStr = TextStr & Chr(TempKey) 'Update TextStr
        End If
      End If
    End If
  End If
  Text1.Text = "输入位置: " & TextStr & " mm"
  Text1.SelStart = Len(Text1.Text) - 3
End Sub
加上注释好看一些,自己有需要再调整。
1