以不统一格式接收Com端口数据 [英] Received Com Port data in ununiform format
问题描述
我想使用com端口从计量机输出Count作为输出
我的端口连接事件代码如下:
I want to get Count as output from weighing machine using com port
My Port Connect Event Code is Below:
Private Sub cmdConnect_Click()
If cmdConnect.Caption = "Connect Machine" Then
Dim str As String
str = Combo1.Text
Dim i As Integer, j As String
i = Mid(str, 4, 1)
MSComm1.CommPort = i
MSComm1.PortOpen = False
MSComm1.PortOpen = True
MsgBox "Communication Created Successfully"
MSComm1.Settings = "9600,N,8,1"
MSComm1.Output = "0" + Chr(13)
MSComm1.DTREnable = True
MSComm1.Handshaking = 1
MSComm1.NullDiscard = True
MSComm1.RTSEnable = True
MSComm1.EOFEnable = True
MSComm1.RThreshold = 12
MSComm1.SThreshold = 1
MSComm1.InputMode = comInputModeText
cmdConnect.BackColor = &HC0&
cmdConnect.Caption = "Disconnect Machine"
我的OnComm活动代码在这里:
My OnComm Event Code is Here:
<pre>Dim str As String, res As Variant, com As String, txt As String, i As Integer
'MSComm1.Settings = "9600,N,8,1"
'MSComm1.Output = "0" + Chr(13)
MSComm1.DTREnable = True
MSComm1.Handshaking = 1
MSComm1.NullDiscard = True
MSComm1.RTSEnable = True
MSComm1.EOFEnable = True
MSComm1.RThreshold = 15
MSComm1.SThreshold = 1
'MSComm1.InputLen = 4
MSComm1.InputMode = comInputModeText
Select Case MSComm1.CommEvent
Case comEvReceive
txtDisplay.Text = ""
Do While MSComm1.InBufferCount > 0
'txtDisplay.Text = txtDisplay.Text + Chr(13) + MSComm1.Input
txtDisplay.Text = txtDisplay.Text + vbNewLine + MSComm1.Input
Loop
If OptAuto.Value = True Then
ReportPrint
ElseIf OptManu.Value = True Then
cmdPrint.Visible = True
End If
Case Is > 1000
MsgBox "Unknown Weight"
End Select
我的输出格式是
相同的重量有两种不同的格式
第一次输出像
My Output Format is
Same Weight Come in two Different Format
First Output Like
0001.600013660S
这里数是1366
第二次输出像0.0001.60001366这里算是1366
其他一些重量给这样的输出
2.1852.185这里算是185
请告诉我这是正确的编码
或告诉我正确的编码以统一格式接收com端口数据
i想要从这个输出中分割计数这里的数量来自每个输出的最后一位数
here count is 1366
Second time Output like 0.0001.60001366 here count is 1366
some other weight give output like this
2.1852.185 here count is 185
Please tell me it's the correct coding
or tell me correct coding to receive com port data in uniform format
i want to split count from this output here count come in last digit of every single output
推荐答案
:叹息:
Com端口 - 特别是串口 - 不能那样工作。接收数据需要花费一些时间 - 在您的情况下,由于您将波特率设置为9600,因此每秒最多大约960个字符 - 并且它根本不会尝试框架或打包数据,因为它不会我不知道它会得到什么数据。
它只是一个字符流,它将它们作为单个字符传递给你。所以有时候,你得到4个字符,有时你会得到你上次没看过的字符和一堆新字符。只是阅读直到没有更多可用没有帮助 - 因为它需要实时接收每个角色!
看看机器应该给你的确切内容:它几乎肯定会是数据加上一个终结符,如换行符。所以你读取数据,并保存,直到终止符到达。然后你可以查看整数并将其转换为有效值,而不是之前。
:sigh:
Com ports - particularly serial ports - don't work like that. It takes time to receive the data - in your case a maximum of about 960 characters per second because you have the baud rate set to 9600 - and it doesn't try to "frame" or "package" the data at all because it doesn't know what data it will get.
All it is is a stream of characters, and it passes them to you as individual characters. So sometimes, you get 4 characters and sometimes you get the ones you didn't look at last time plus a bunch of new ones. Just reading until no more are available doesn't help - because it takes real time to receive each character!
Look at exactly what the machine is supposed to give you: it will almost certainly be the data plus a terminator such as newline. So you read the data, and save it until a terminator arrives. Then you can look at the whole number and convert it into a valid value, not before.
这篇关于以不统一格式接收Com端口数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!