发表于:2005-05-17 13:23:00
楼主
小弟刚入行不久,写了个小程序,希望各位大侠能帮我指点一二,谢谢!
Public countRX As Integer
Public countTX As Integer
Public num As Byte
Public setport As String
Private Sub Combo1_Click()
openport
End Sub
Private Sub combo2_click()
openport
End Sub
Private Sub combo3_click()
openport
End Sub
Private Sub combo4_click()
openport
End Sub
Private Sub combo5_click()
openport
End Sub
Private Sub Command3_Click()
Text1.Text = ""
End Sub
Private Sub Command4_Click() '手动发送
Dim send() As Byte
Dim i As Integer
Dim length As Integer
Dim a() As Byte
Dim b As Byte
length = Len(Text2.Text)
If length > 0 Then
ReDim a(length - 1)
ReDim send(length - 1)
End If
send = Text2.Text
countTX = countTX + length
If Check2.Value = 0 Then
MSComm1.Output = send
'countTX = countTX + MSComm1.OutBufferCount
Else
i = 0
b = 0
Do While i <= (length - 1) * 2
If ((send(i) >= 48 And send(i) <= 57) Or (send(i) >= 65 And send(i) <= 70) Or (send(i) >= 97 And send(i) <= 102)) Then
If (send(i) >= 97 And send(i) <= 102) Then
a(b) = send(i) - 32
Else
a(b) = send(i)
End If
Else
If b > 0 Then
ReDim Preserve a(b - 1)
End If
Exit Do
End If
i = i + 2
b = b + 1
DoEvents
Loop
MSComm1.Output = a
End If
Label5.Caption = "TX:" & countTX
End Sub
Private Sub command5_Click()
Text2.Text = " "
End Sub
Private Sub Command6_Click()
Label4.Caption = "RX:0 "
Label5.Caption = "TX:0 "
End Sub
Private Sub Form_Load()
Combo1.ListIndex = 0 'initial
Combo2.ListIndex = 0
Combo3.ListIndex = 0
Combo4.ListIndex = 0
Combo5.ListIndex = 0
End Sub
Sub openport()
'Dim num As Byte
'Dim setport As String
On Error GoTo msg
num = Combo1.ListIndex + 1
If MSComm1.PortOpen = True Then
'MsgBox ("没有发现串口或被占用")
MSComm1.PortOpen = False
End If
MSComm1.CommPort = num
setport = Combo2.Text + ","
If Combo3.ListIndex = 0 Then
setport = setport + "N,"
End If
If Combo3.ListIndex = 1 Then
setport = setport + "O,"
End If
If Combo3.ListIndex = 2 Then
setport = setport + "E,"
End If
If Combo4.ListIndex = 0 Then
setport = setport + "8,"
End If
If Combo4.ListIndex = 1 Then
setport = setport + "7,"
End If
If Combo5.ListIndex = 0 Then
setport = setport + "1"
End If
If Combo5.ListIndex = 1 Then
setport = setport + "2"
End If
MSComm1.Settings = setport
MSComm1.PortOpen = True
'If MSComm1.PortOpen = True Then
'Label3.Caption = "stats:" & num & ",open" & "," & setport
'Else
' Label3.Caption = "stats:" & num & "close" & "," & setport
'End If
msg:
End Sub
Sub command1_click()
On Error GoTo msg
MSComm1.PortOpen = True
msg: MsgBox ("port had opened")
End Sub
Sub command2_click()
MSComm1.PortOpen = False
End Sub
Private Sub Timer1_Timer()
If MSComm1.PortOpen = True Then
Shape1.FillColor = RGB(0, 255, 0)
Command1.Visible = False
command2.Visible = True
Label3.Caption = "stats:" & num & ",open" & "," & setport
Else
Shape1.FillColor = RGB(255, 0, 0)
Command1.Visible = True
command2.Visible = False
Label3.Caption = "stats:" & "close" & "!"
End If
End Sub
Private Sub Timer2_Timer() '接收信号
Dim recBuf() As Byte
Dim recCnt As Integer
Dim str As String
Dim i As Integer
Dim s