VB 2008 AtmelμC和PC通过RS232/Tank监控 [英] VB 2008 Atmel μC and PC via RS232/Tank monitoring

查看:91
本文介绍了VB 2008 AtmelμC和PC通过RS232/Tank监控的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我是第一次问你一个问题.
我是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

推荐答案

您可以使用Integer.Parse()方法.但是,您要确保仅使用包含1个整数表示形式的字符串部分.如果一行中有多个在一起,则会出现异常或可能解析错误的值.该数据是否以换行符或回车符或定界符终止?如果是这样,您可以使用SerialPort.ReadLine()一次获得一行. ReadExisting()并不是一个好函数,因为它可以返回部分消息或根本不返回任何消息.
You can use the Integer.Parse() method. However, You want to make sure to only use the part of the string that contains 1 representation of an integer. If you have several together in a row you get an exception or possibly parse the wrong value. Is this data terminated with a linefeed or carriage return or delimited? If so, you can use SerialPort.ReadLine() to get one line at a time. ReadExisting() is not a great function because it can return partial messages or no message at all.


integer.parse()方法非常接近成功,显示了第一个匹配项,但是之后,该应用程序停止并且出现错误输入字符串的格式不正确."
数据不在行中.在超级终端中,我无需发送CR就可以按行获取数据.我尝试使用chr(10)换行和chr(13)回​​车并遇到相同的错误.
atmel是在BASCOM中编程的,它只使用PRINT命令发送任何数据,我在我的值附近添加了10和13字符.我很困惑.
the integer.parse() method is very close to succeed,first hit was displayed,but after that the application stops and i get an error "Input string was not in a correct format."
data are not in line.In hyperterminal i get data by rows without need to send CR.I tried with chr(10) linefeed and chr(13) carriage return and faced the same error.
the atmel is programmed in BASCOM,it just send any data with the PRINT command,i add the chars 10 & 13 near my value.i''m comfused.


您的问题是这一行:
Your problem is this line:
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监控的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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