从电子称重机输入重量 [英] Take input of weight from electronic weight machine
本文介绍了从电子称重机输入重量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们如何在Windows应用程序中从电子称重机获取输入.与机柜通过串行端口(IOIOIA A)(4-5公针)连接. (如Canta)
解决方案
您需要的是:
-您的减肥机的接口文档;
-SerialPort类;
-以及一些编程.
困难程度取决于他们对软件界面的构造程度.以NewLine结尾的ASCII消息将是最简单的.
我会推荐C#或VB.NET;无论您做什么,都不要去使用古老的VB.
:)
http://msdn .microsoft.com/en-us/library/system.io.ports.serialport.aspx [ 现在,它必须删除其他字符,这取决于比例.因此,需要花费一些时间才能真正获得想要的东西.可以将其与串行端口转USB适配器一起使用,但是某些适配器将无法正常工作,因此,如果一种类型不起作用,请尝试另一种适配器. Radio Shack有一个对我有用.
Imports System.Text Imports System Imports System.IO.Ports Imports System.IO Imports Microsoft.VisualBasic Module Module1 Public ComportSelect As String Public test1 As String Public filename As String = "standard" Public labelString As String Function getserialdata() '' GET DATA FROM COM PORT ComportSelect = My.Forms.LABELSELECT.ComportSelect Dim returnStr As String Dim a As Int16 = 0 Dim b As Int16 returnStr = "1" Do Try REM Dim buffer As New StringBuilder() Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort(ComportSelect) '' SEND "P" COMMAND TO SCALES TO START TRANSMITTING Dim outgoing As String outgoing = "P" comPort.WriteLine(outgoing) '' READ DATA comPort.ReadTimeout = 2000 Dim line As String = comPort.ReadLine() returnStr = (line) comPort.Close() returnStr = returnStr.Substring(1) End Using '' REMOVE ALL UNWANTED CHARACTERS If returnStr.Length > 2 Then a = 0 Dim TestString As String = returnStr Dim TestArray() As String = Split(TestString) '' TestArray holds {"chr2)", "", "", "", "0.0", "lb", "gr", ""} Dim LastNonEmpty As Integer = -1 For i As Integer = 0 To TestArray.Length - 1 If TestArray(i) <> "" Then LastNonEmpty += 1 TestArray(LastNonEmpty) = TestArray(i) End If Next If TestArray(0) = "-" Then TestArray(1) = TestArray(0) + TestArray(1) b = 1 returnStr = TestArray(1) + " " + TestArray(2) Else : b = 0 returnStr = TestArray(0) + " " + TestArray(1) End If ReDim Preserve TestArray(LastNonEmpty) '' TestArray now holds {"10.9", "Lb", "Gr" REM Test weight toloerence Dim weightD As Decimal ''Dim weightS As String = "1" Dim hightol As Decimal Dim lowtol As Decimal '' TestArray now holds {"10.9", "Lb", "Gr" weightD = Convert.ToDecimal(TestArray(b)) hightol = Convert.ToDecimal(My.Forms.LABELSELECT.maxweight.Text) lowtol = Convert.ToDecimal(My.Forms.LABELSELECT.minweight.Text) REM test for max weight If weightD < hightol And weightD > lowtol Then Exit Do If weightD > hightol Then My.Forms.COMPORT.Comla.Text = "WEIGHT IS TO HIGH" My.Forms.COMPORT.comlb.Text = "PRESS OK TO RE-MEASURE" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 1 End If REM test for min weight If weightD < lowtol Then My.Forms.COMPORT.Comla.Text = "WEIGHT IS TO LOW" My.Forms.COMPORT.comlb.Text = "PRESS OK TO RE-MEASURE" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 1 End If Else My.Forms.COMPORT.Comla.Text = "Comport Has no Connection, Check Connection" My.Forms.COMPORT.comlb.Text = "PRESS CANCEL" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 0 End If Catch e As System.ArgumentException If e Is Nothing Then Else My.Forms.COMPORT.Comla.Text = "No Comport selected" My.Forms.COMPORT.comlb.Text = "ArgumentException, PRESS CANCEL" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 0 End If Catch e As System.UnauthorizedAccessException If e Is Nothing Then Else My.Forms.COMPORT.Comla.Text = "Comport Has no Connection, Select another port" My.Forms.COMPORT.comlb.Text = "or UnauthorizedAccessException, press cancel" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 0 End If Catch e As TimeoutException If e Is Nothing Then a = 0 Else My.Forms.COMPORT.Comla.Text = "Scale in motion press ok to retry" My.Forms.COMPORT.comlb.Text = "or TimeoutException, press cancel" My.Forms.COMPORT.ShowDialog() a = 1 returnStr = "No Comport" End If REM Console.WriteLine(e) End Try If My.Forms.COMPORT.DialogResult = System.Windows.Forms.DialogResult.Cancel Then Exit Do If a = 0 Then Exit Do Loop Return returnStr End Function End Module
How we take input from electronic weight machine in over windows application. which connect with cabinet in Serial Port (IOIOI A) (4-5 male Pin). (like a canta)
解决方案
What you need is:
- the interface documentation of your weight machine;
- the SerialPort class;
- and some programming.
The level of difficulty will depend on how well they structured the software interface; ASCII messages ending on a NewLine would be easiest.
I would recommend C# or VB.NET; whatever you do, don''t go ancient VB.
:)
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx[^] this class might help..
There are several Ways to do this and everyone has some kind of drawback because of the serial port setup in .net. But I succeeded in do this in VB. This example sends a P command to the serial port which tells the scale to send the weight data. The scales have a lot of different data can be sent. This is with the scales seutup just to send the weight as follows {"10.9", "Lb", "Gr"). Also the scales esttings have to set the same as your port or visversa (Baud= 9600,Data= 8, Parity= none, 1 stop.
Now it has other characters that have to be removed and this depends on the scale. So it takes some playing with to get exactly what you want. This can be used with a serialport to usb adapter but some adapters just will not work correctly so if one type doesn''t work try a different one. Radio Shack has one that worked for me.
Imports System.Text Imports System Imports System.IO.Ports Imports System.IO Imports Microsoft.VisualBasic Module Module1 Public ComportSelect As String Public test1 As String Public filename As String = "standard" Public labelString As String Function getserialdata() '' GET DATA FROM COM PORT ComportSelect = My.Forms.LABELSELECT.ComportSelect Dim returnStr As String Dim a As Int16 = 0 Dim b As Int16 returnStr = "1" Do Try REM Dim buffer As New StringBuilder() Using comPort As SerialPort = My.Computer.Ports.OpenSerialPort(ComportSelect) '' SEND "P" COMMAND TO SCALES TO START TRANSMITTING Dim outgoing As String outgoing = "P" comPort.WriteLine(outgoing) '' READ DATA comPort.ReadTimeout = 2000 Dim line As String = comPort.ReadLine() returnStr = (line) comPort.Close() returnStr = returnStr.Substring(1) End Using '' REMOVE ALL UNWANTED CHARACTERS If returnStr.Length > 2 Then a = 0 Dim TestString As String = returnStr Dim TestArray() As String = Split(TestString) '' TestArray holds {"chr2)", "", "", "", "0.0", "lb", "gr", ""} Dim LastNonEmpty As Integer = -1 For i As Integer = 0 To TestArray.Length - 1 If TestArray(i) <> "" Then LastNonEmpty += 1 TestArray(LastNonEmpty) = TestArray(i) End If Next If TestArray(0) = "-" Then TestArray(1) = TestArray(0) + TestArray(1) b = 1 returnStr = TestArray(1) + " " + TestArray(2) Else : b = 0 returnStr = TestArray(0) + " " + TestArray(1) End If ReDim Preserve TestArray(LastNonEmpty) '' TestArray now holds {"10.9", "Lb", "Gr" REM Test weight toloerence Dim weightD As Decimal ''Dim weightS As String = "1" Dim hightol As Decimal Dim lowtol As Decimal '' TestArray now holds {"10.9", "Lb", "Gr" weightD = Convert.ToDecimal(TestArray(b)) hightol = Convert.ToDecimal(My.Forms.LABELSELECT.maxweight.Text) lowtol = Convert.ToDecimal(My.Forms.LABELSELECT.minweight.Text) REM test for max weight If weightD < hightol And weightD > lowtol Then Exit Do If weightD > hightol Then My.Forms.COMPORT.Comla.Text = "WEIGHT IS TO HIGH" My.Forms.COMPORT.comlb.Text = "PRESS OK TO RE-MEASURE" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 1 End If REM test for min weight If weightD < lowtol Then My.Forms.COMPORT.Comla.Text = "WEIGHT IS TO LOW" My.Forms.COMPORT.comlb.Text = "PRESS OK TO RE-MEASURE" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 1 End If Else My.Forms.COMPORT.Comla.Text = "Comport Has no Connection, Check Connection" My.Forms.COMPORT.comlb.Text = "PRESS CANCEL" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 0 End If Catch e As System.ArgumentException If e Is Nothing Then Else My.Forms.COMPORT.Comla.Text = "No Comport selected" My.Forms.COMPORT.comlb.Text = "ArgumentException, PRESS CANCEL" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 0 End If Catch e As System.UnauthorizedAccessException If e Is Nothing Then Else My.Forms.COMPORT.Comla.Text = "Comport Has no Connection, Select another port" My.Forms.COMPORT.comlb.Text = "or UnauthorizedAccessException, press cancel" My.Forms.COMPORT.ShowDialog() returnStr = "No Comport" a = 0 End If Catch e As TimeoutException If e Is Nothing Then a = 0 Else My.Forms.COMPORT.Comla.Text = "Scale in motion press ok to retry" My.Forms.COMPORT.comlb.Text = "or TimeoutException, press cancel" My.Forms.COMPORT.ShowDialog() a = 1 returnStr = "No Comport" End If REM Console.WriteLine(e) End Try If My.Forms.COMPORT.DialogResult = System.Windows.Forms.DialogResult.Cancel Then Exit Do If a = 0 Then Exit Do Loop Return returnStr End Function End Module
这篇关于从电子称重机输入重量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文