Private serialPort as SerialPort ' definition
Public Function Createserialport () as String
Dim strweights as String = ""
Dim strdata as String = ""
Dim Bsetpointnum as Double = 0.0
If SerialPort IsNot Nothing Then
If Serialport.isopen Then
Me.serialPort.Close ()
End If
Me.serialPort.Dispose ()
End If
Try
SerialPort = New SerialPort ("COM" + clxml.read ("login", "Bserialportnames", "" "), Integer.parse (Clxml.read (" Login "," Bbaudrate ",")), CType ([Enum]. Parse (GetType (Parity), Clxml.read ("Login", "bparity", ""), Parity), Integer.parse (Clxml.read ("Login", "Bdatabits", " ")), CType ([Enum]. Parse (GetType (stopbits), Clxml.read ("Login", "Bstopbits", "")), StopBits))
Serialport.writetimeout = 20000
Serialport.readtimeout = 20000
Serialport.readbuffersize = 20480
Serialport.writebuffersize = 20480
Serialport.open ()
Dim portname as String = "COM" + clxml.read ("Login", "Bserialportnames", "" ")
If Serialport.isopen Then
Dim readlength as Integer = 8
Dim offset as Integer = 0
Dim data as Byte () = New byte (readLength-1) {}
Do
Dim bytelenght as Integer = serialport.read (data, offset, readlength)
Offset + = Bytelenght
Readlength-= Bytelenght
Loop while Serialport.bytestoread > 0
Strdata = Encoding. [Default]. GetString (data)
Bsetpointnum = Clxml.read ("Login", "Bsetpoint", "" ")
SetPoint ()
If IsNumeric (strdata) = True Then
Strweights = IIf (Strdata/bsetpointnum < "0", Bsetpoint, Val (Strdata/bsetpointnum))
Application.doevents ()
Else
Strweights = Bsetpoint
End If
If strweights = "Then
Return Createserialport ()
End If
End If
Catch ex as Exception
C.msginfro (C.getresource ("MyResource", "Public_serialporterror"), "notices", True)
End Try
Return strweights
End Function
. NET Call window serial to read the electronic scale data