RS232无法处理VB2008 [英] RS232 Can't handle VB2008

查看:81
本文介绍了RS232无法处理VB2008的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我在VB2008/NET2.0中编写了一个代码,在操作系统出现问题后,我重新编写了该代码.我正在使用该代码来与我的PC通信Atmel板并通过NI Measurement显示一些图形数据工作室.

问题是,现在我无法使其与Atmel进行通信,你们中的一个人可以告诉我错误在哪里吗?

该程序正在运行,但无法从RS232读取数据,另一方面,我使用超级终端来测试我的连接,并且可以正常运行...
我也尝试使用其他版本的.NET(2.0和3.0).
另一个问题是在执行了这段代码后,我的操作系统崩溃(WinXP主页).
这两个问题有什么解决办法吗?
非常感谢您的宝贵时间! :-D :-D
----------------- CODE -----------------------------

Hi to all !
I wrote a code in VB2008/NET2.0,after a problem with my operating system i re-wrote this code.I''m using this code in order to communicate my Atmel board with my PC and display some graphic data via NI Measurement Studio.

The problem is,that now i can''t make it communicate with Atmel,can someone of you tell me where is the error ?

The program is running but does not reads data from RS232,on the other hand i use Hyper Terminal to test my connection and it works perfect...
I have also try to different version of .NET (2.0 & 3.0).
Another question is that after some executions of this code my O.S. crashes (WinXP Home).
Is there any solution for these two problems?
Thank''s a lot for your time! :-D :-D
-----------------CODE-----------------------------

Imports System.IO.Ports.SerialPort
Imports System.Windows.Forms.Control
Imports System.Windows.Forms.Form

Public Class Form1

    Public Delegate Sub myDelegate()

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If SerialPort1.IsOpen Then
            SerialPort1.Close()
        End If

        Try
            With SerialPort1
                .PortName = "Com1"
                .BaudRate = 9600
                .Parity = IO.Ports.Parity.None
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .ReceivedBytesThreshold = 1
            End With
            SerialPort1.Open()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Dispose()
        Try
            SerialPort1.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try

    End Sub

    Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)

        TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
    End Sub

    Public Sub updateTextBox()
        Dim input As String
        input = SerialPort1.ReadExisting
        If input = "A" Then alive()
        If input = "T" Then tank()
        If input = "G" Then gauge()
        If input = "O" Then outemp()
        If input = "B" Then boiler()
        If input = "W" Then water()

        'Try

        'Catch ex As Exception

        'End Try
        With TextBox1
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .AppendText(SerialPort1.ReadLine)
            .ScrollToCaret()
        End With
    End Sub

    Sub tank()
        Dim tankval As Integer
        tankval = Integer.Parse(SerialPort1.ReadLine.Trim())
        Tank1.Value = tankval
        If tankval < 200 Then
            Beep()
            Led3.Value = True
            Led2.Value = False
            Led1.Value = False
        ElseIf tankval > 200 And tankval < 750 Then
            Led3.Value = False
            Led2.Value = True
            Led1.Value = False
        ElseIf tankval > 750 Then
            Led3.Value = False
            Led2.Value = False
            Led1.Value = True
        End If

    End Sub
    Sub water()
        Dim tankval1 As Integer
        tankval1 = Integer.Parse(SerialPort1.ReadLine.Trim())

        Tank2.Value = tankval1
        If tankval1 < 300 Then
            Beep()
            Led6.Value = True
            Led5.Value = False
            Led4.Value = False
        ElseIf tankval1 > 300 And tankval1 < 4500 Then
            Led6.Value = False
            Led5.Value = True
            Led4.Value = False
        ElseIf tankval1 > 4500 Then
            Led6.Value = False
            Led5.Value = False
            Led4.Value = True
        End If

    End Sub

    Sub gauge()
        Dim gauge As Double
        gauge = Integer.Parse(SerialPort1.ReadLine.Trim())
        If gauge < 0 Then gauge = 0
        gauge = gauge / 0.434 / 100
        Gauge1.Value = gauge
    End Sub
    Sub outemp()
        Dim temper As Integer
        temper = Integer.Parse(SerialPort1.ReadLine.Trim())

        Thermometer1.Value = temper
    End Sub

    Sub boiler()
        Dim boiler As Integer
        boiler = Integer.Parse(SerialPort1.ReadLine.Trim())

        Thermometer2.Value = boiler
    End Sub

    Sub alive()
        SerialPort1.Write("L")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        SerialPort1.Close()
        Me.Close()
    End Sub
End Class


---------------------------------------------


---------------------------------------------

推荐答案

优点:

    • 您似乎正在捕获任何关闭意图并关闭(并因此处置)COM端口
The pluses:

    • It looks like you''re trapping any shutdown intent and closing (and thus disposing) the COM port
  • 您正在正确地将对UI的更改推回正确的线程

    • ReadLine 正在阻止...如果在COM端口上没有换行符,您将只是挂起线程

      • ReadLine is blocking...if there is no NewLine character coming across the COM port you''re going to just be hanging the thread
      • 您正在使用input = ReadExisting ...然后比较单个字符.您确定阅读时缓冲区中只有一个字符吗?
      • You''re using input = ReadExisting...then comparing single characters. Are you sure there is only one char in the buffer when you read?
      • 也许还可以在比较之前尝试使用.ToUpper() ...不确定您的数据是什么样子.
      • Maybe also try using .ToUpper() before the compare...not sure what your data looks like.


      T0
      W0.0
      G317
      O502
      B1023
      T0
      W0.0
      G131
      O328
      B1023
      T0


      这是超级终端的视图.
      请注意,直到我再次安装WinXp,上面的代码才能正常工作,这可能是.NET或Service Pack 2的问题吗?
      我不记得格式化PC之前已经安装了什么.
      来自μC的字节之后总是一个CR字节.
      我不是VB专家.
      您可以看到我的另一篇文章,我已将此代码用作基础",还有其他用户的一些建议-更正.


      this is a view of hyper terminal.
      Note that the code above was working perfect until i install again WinXp,might be a problem of .NET or of Service Pack 2 ?
      i can''t remember what i had installed before i format my PC.
      The bytes from μC followed always by a CR byte.
      I''m not an expert on VB.
      You can see another post of my,i have used this code as a "base",there are some suggestions-corrections from other users.


      注释掉这些行,您应该在您的文本框:

      Comment out these lines and you should see something in your textbox:

      Dim input As String
      input = SerialPort1.ReadExisting
      If input = "A" Then alive()
      If input = "T" Then tank()
      If input = "G" Then gauge()
      If input = "O" Then outemp()
      If input = "B" Then boiler()
      If input = "W" Then water()



      另外...取出.Font = New Font("Garamond", 12.0!, FontStyle.Bold)行,因为每次字节碰到COM端口时都不要实例化新字体.

      在上面的代码中,您正在将串行端口上的所有数据读入一个字符串.因此,当您得到T0然后针对单个字符对其进行测试时,每次都会出错.

      吃完所有数据后,您尝试将ReadLine的结果放入文本框中.我什么都看不见.



      Also...take out the .Font = New Font("Garamond", 12.0!, FontStyle.Bold) line as you shouldn''t be instantiating a new font every time a byte hits your COM port.

      In the above code you are reading all data on the serial port into a string. So when you get T0 and then you test it against a single character you will get false every time.

      After eating all the data you attempt to place the result of a ReadLine into the textbox. I can''t see anything ever appearing there.


      这篇关于RS232无法处理VB2008的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆