您现在所在的是:

电脑编程

回帖:1个,阅读:680 [上一页] [1] [下一页]
* 帖子主题:

VB2005串口通信问题

6260
cialiaozilong
文章数:282
年度积分:107
历史总积分:6260
注册时间:2009/12/26
发站内信
发表于:2013/9/26 17:35:08
#0楼
Imports System.IO.Ports         '使用SerialPort所引用的命名空间



Public Class Form1

   Dim fx() As Byte       ' 待发送数据数组

   Dim Rc() As Byte       ' 接收数据数组


   '  数据定时接收与显示

   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

       Dim StrRc As String

       Dim i As Integer

       Dim n As Integer




       n = SerialPort1.BytesToRead   '  读缓冲区数据量,有数据则接收


       If n > 0 Then
           ReDim Rc(n)
           StrRc = ""
           For i = 1 To n
               Rc(i) = SerialPort1.ReadByte
               StrRc = CStr(Hex(Rc(i))) & ""
           Next
           TextBox2.Text = StrRc


       End If


   End Sub


   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       ComboBox1.Items.Clear() '(清空列表)
       ComboBox2.Items.Clear()
       For Each portname As String In My.Computer.Ports.SerialPortNames
           ComboBox1.Items.Add(portname)          ' 显示所有串口
           Try
               SerialPort1.PortName = portname
               SerialPort1.Open()
               ComboBox2.Items.Add(portname)      ' 显示可用串口
               SerialPort1.Close()
           Catch ex As Exception
               MsgBox("可用串口检查 " & portname)
           End Try
       Next
       If ComboBox1.Items.Count > 0 Then ComboBox1.SelectedIndex = 0
       If ComboBox2.Items.Count > 0 Then
           ComboBox2.SelectedIndex = 0
           Button2.Enabled = True        ' 有可用串口,可以打开操作
       End If
   End Sub

   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       If Button2.Text = "打开串口" Then
           SerialPort1.PortName = ComboBox2.Text
           If Not SerialPort1.IsOpen Then
               SerialPort1.Open()
               Button2.Text = "关闭串口"
               Button3.Enabled = True      '串口打开,可以发送数据
           End If
       Else
           If SerialPort1.IsOpen Then SerialPort1.Close()
           Button2.Text = " 打开串口"
           Button3.Enabled = False
       End If

   End Sub


   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

       If Button1.Text = "打开串口" Then

           SerialPort1.PortName = "COM1"

           SerialPort1.Open() '(串口打开与关闭)

           Button1.Text = "关闭串口"

           Button2.Enabled = True

           Timer1.Enabled = True

       Else

           If SerialPort1.IsOpen Then SerialPort1.Close()

           Button1.Text = "打开串口"

           Timer1.Enabled = False

           Button2.Enabled = False

       End If

   End Sub


   ' 待发送数据处理与发送

   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

       Dim i As Integer

       Dim n As Integer

       Dim Cmd As String = TextBox1.Text

       n = Len(Cmd) \ 2

       ReDim fx(n)

       For i = 1 To n

           fx(i) = CByte("&H" & Mid(Cmd, 2 * i - 1, 2))

       Next

       SerialPort1.Write(fx, 1, n)    ' 发送数组fx第1到n数据

   End Sub


   
 
End Class





以上代码在调试的时候出错,提示端口被占用。 好像是这一句出错   n = SerialPort1.BytesToRead  
我刚学VB2005 大家帮我分析下原因吧
745
q_gzhiguo
文章数:16
年度积分:50
历史总积分:745
注册时间:2013/9/18
发站内信
发表于:2013/9/26 23:21:13
#1楼
程序出错最后没有释放串口,建议用异常捕获语句最后释放串口
try
catch
finally
在finally段释放

关于我们 | 联系我们 | 广告服务 | 本站动态 | 友情链接 | 法律声明 | 非法和不良信息举报

工控网客服热线:0755-86369299
版权所有 工控网 Copyright©2024 Gkong.com, All Rights Reserved

46.8003