One existing electronic scale, using serial ports to communicate with the computer. Write a VB program to access the serial port to reach the data displayed on the electronic scale. The electronic scale is a BE01 type meter, which is output as an RS-232C standard interface. The baud rate is 300-9600, even check, 7 data bits, 2 stop bits. All characters are sent 11 ASCII codes, one start bit. In VB, you need to introduce control MSCOMM serial communication controls (in Microsoft Comm Control 6.0). The specific procedures are as follows: Control abbreviation: MSC
DIM OUT (12) as byte 'Receives values in VAR
DIM VAR AS VARIANT 'Receives values in MSC.input
DIM NRECE AS INTEGER 'calculates the number of msc.inputbuffers
DIM I AS INTEGER, J AS INTEGER 'Take the change, calculation cycle
*********************************************************** **********************************
Private sub flow_load ()
Cleartext
With msc
.Commport = 1 'Sets COM1 for communication port
.SETTINGS = "9600, E, 7, 2" Sets the communication port parameter 9600 Hz, even check, seven data bits, 1 stop bit. (Further explain here: .SETTING = "BBBB, P, D, S ".
Meaning is: B: Baud Rate; P: Parity (odd); D: Data bit; s: stop bit)
.INBUFFERSIZE = 40 'Setting the buffer receive data is 40 bytes
.Inputlen = 1 'Setting INPUT once from the reception buffer reading by 1 number 1
.RtHRESHOLD = 1 'Sets to receive one byte to generate an ONCOMM event
End with
End Sub
*********************************************************** **********************************
Private sub cleartext ()
TEXT3.TEXT = "" "
TEXT2.TEXT = "5"
TEXT1.TEXT = "" "
End Sub
Private submmand1_click ()
Cleartext
'NRECE = 0' counter clear zero
With msc
.InputMode = cominputmodebinary 'Setting data reception mode is binary form
.INBUFFERCOUNT = 0 'Clear the receive buffer
IF not .portopen then
.Petopen = true 'Opens the communication port
END IF
End with
End Sub
Private sub msc_oncomm ()
DELAYTIME 'is used to continue
Cleartext
With msc
SELECT CASE .Commevent 'Judging Communication Event
Case Comevreceive: 'Received Receive Events Generated by Rthreshold bytes
Swichvar 1
If OUT (1) = 2 THEN 'Judging whether it is the start of data
.RtHRESHOLD = 0 'Close the oncomm event reception
END IF
DO
Doevents
Loop unsinbuffercount> = 3 'loop waiting to receive buffer> = 3 bytes
'NRECE = NRECE 1
FOR i = 2 to 12
Swichvar I
Text1.text = text1.text & chr (OUT (i))
NEXT
Text1.text = Ltrim (Text1.Text)
Text2.text = text2.text & cstr (NRECE)
.Rthreshold = 1 'Open MSCOMM event reception
Case Else
'.Portopen = false
End SELECT
End with
End Sub
*********************************************************** **********************************
Private sub delaytime ()
Dim bdt as boolean
Dim Sprevious as single, Slast As Single
BDT = TRUE
Sprevious = Timer (Timer can calculate the number of seconds passing from the sub-night, in Microsoft Windows, the Timer function can return a second decumination)
Do While BDT
IF Timer - Sprevious> = 0.3 Then BDT = FALSE
Loop
BDT = TRUE
End Sub
(The communication transfer rate is 9600bps, the fastest speed is 1.04ms sends a byte, and the meter sends 50 frames per second. There are 4 bytes per frame, that is, 200 bytes per second, the average 5.0ms sends a word. Section, add a loop wait program in the program when reading serial port data continuously)
Private sub swichvar (byval nnum as integer)
DelayTime
Var = null
Var = msc.input
OUT (nnum) = var (0)
End Sub
(Setting the receiving data mode is used in binary form, "iNPutMode = CominputModeBinary, but when reading data with the input property, you cannot directly assign the value to the BYTE type variable. You can only assign a Variant type variable first, return a binary data, and then Transformation is saved to the Byte type variable.)
Private sub text1_change ()
Text3.text = ctext (text1.text) - CText (Text2.Text)
End Sub
*********************************************************** **********************************
Private function ctext (byval str as string) AS Currency
IF str <> "" "
CText = CCur (Val (STR))
Else
Ctext = 0
END IF
END FUNCTION