问题描述
大家好,我是第一次问你一个问题.
我是VB的新手,我想通过rs232将Atmel与PC连接,我还已经将NI Measurement Components安装到了Visual Studio 2008中.
atmel通过UART端口打印值"发送从ADC端口读取的值. (我不发送chr(13)-CR)
我在VB中的代码可以从COM正确读取数据并将其打印到文本框中.
我如何使用这些数据才能将其用于tank1.value = integer?
我尝试了很多转换,但一无所获.
导入System.IO.Ports.SerialPort
公共课程表格1
公共代表Sub myDelegate()
私有子Form1_Load(ByVal发送者作为对象,ByVal e作为System.EventArgs)处理Me.Load
如果SerialPort1.IsOpen然后
SerialPort1.Close()
如果结束
试试
使用SerialPort1
.PortName ="Com4"
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
.ReceivedBytesThreshold = 1
结尾为
SerialPort1.Open()
异常捕获
结束尝试
结束子
私有子Form1_FormClosing(ByVal发送者作为对象,ByVal e作为System.Windows.Forms.FormClosingEventArgs)处理Me.FormClosing
试试
SerialPort1.Close()
异常捕获
MsgBox(ex.ToString)
结束尝试
结束子
私有子DataReceived(ByVal发送者作为对象,ByVal e作为System.IO.Ports.SerialDataReceivedEventArgs)_
处理SerialPort1.DataReceived
TextBox1.Invoke(New myDelegate(AddressOf updateTextBox),New Object(){})
结束子
公共子updateTextBox()
昏暗tankval为整数
tankval = AscW(CChar(SerialPort1.ReadExisting))
使用TextBox1
.Font = New Font("Garamond",12.0 !, FontStyle.Bold)
.AppendText(SerialPort1.ReadExisting)
.ScrollToCaret()
Tank1.Value = tankval
TextBox2.Text = tankval
结尾为
结束子
结束类
Hi all,i''m asking you a question for first time.
I''m new in VB and i want to connect an Atmel with a PC via rs232,i have also installed the NI Measurement Components into my Visual Studio 2008.
The atmel is sending the value that it is reading from ADC port thru is UART port "Print Value". (i don''t send chr(13)-CR)
My code in VB reads correctly the data from COM and print them into a text box.
How can i use these data in order to use them into the tank1.value=integer ?
i have try many conversions but nothing.
Imports System.IO.Ports.SerialPort
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 = "Com4"
.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
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) _
Handles SerialPort1.DataReceived
TextBox1.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
End Sub
Public Sub updateTextBox()
Dim tankval As integer
tankval = AscW(CChar(SerialPort1.ReadExisting))
With TextBox1
.Font = New Font("Garamond", 12.0!, FontStyle.Bold)
.AppendText(SerialPort1.ReadExisting)
.ScrollToCaret()
Tank1.Value = tankval
TextBox2.Text = tankval
End With
End Sub
End Class
推荐答案
tankval = AscW(CChar(SerialPort1.ReadExisting))
ReadExisting()返回一个字符串,您正在将其转换为字符,然后将其转换为字符串.
如果确定只有一条消息,则可以执行以下操作:
ReadExisting() returns a string and you are converting it to a character and then converting it to a string.
If you are sure you only have one message you can do this:
tankval = Integer.Parse(SerialPort1.ReadExisting().Trim())
如果不是,则可以考虑使用String.Split()或其他方法从返回字符串中获取各个值.
顺便说一句,在事件处理程序中进行大量处理通常不是一个好方法,但这是一个完全不同的问题.
If not you may consider using String.Split() or some other way to get the individual values out of the return string.
BTW- It''s generally not good form to do so much processing in the event handler, but that''s a whole different question.
这篇关于VB 2008 AtmelμC和PC通过RS232/Tank监控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!