Serial Communication in PB6 (MSComm32.ocx) Full Solution Edition

zhaozj2021-02-08  228

Serial Communication in PB6 (MSComm32.ocx) Full Solution Edition

This article gives some basic knowledge of how to collect serial port data in PB6 and some basic knowledge of MSComm32.ocx controls.

One: Problem Description

This article solves the problem of pound values ​​to collect pounds. The scale and computer (COM1 / COM2) are connected to the RS-232 standard serial line. The baud rate is 1200, the data format is as follows: a set of data per 100ms, each set of data has 5 frames, 11 digits per frame, 1 start position (0), 8-bit data bits (D0-D7), 2 Stop bit (1), no parity. Definitions are as follows:

Frame 1: D0-D7 ---- 0FFH (start bit)

Episode 2: D0-D2 ---- For a small number location (0-5)

D3 ----- NC

D4 ----- 1 means weighing stability 0 means weighing unstable

D5 ----- 1 means weighing ignition 0 means weighing

D6 ----- NC

D7 ----- 1 means overload

Article 3: D0-D7 ---- BCD1 (Weighing value)

Fourth Frame: D0-D7 ---- BCD2 (Weighing)

Fifth Frame: D0-D7 ---- BCD3 (Weighing Value)

BCD1, BCD2, BCD3 are BCD codes from low to high weighted values.

2: Solution

Using the MScomm32.ocx control in VB, first register this control in the Insert Control in the OLE in the PB. If the registration is used, there is no license, there is no registration code problem, and there is a lot of solutions in this area. But I haven't solved the problem later, my solution is to install the ActiveX when installing VB. Funding VB will definitely solve the problem.

Add this control on the window, name OLE_1. Set the properties of OLE_1 in the OPEN event of the window.

OLE_1.Object.commport = 1

OLE_1.Object.Settings = "1200, n, 8, 1"

OLE_1.Object.Portopen = TRUE

OLE_1.Object.rthreshold = 10 // Take 10 bytes Each time, it will definitely contain a full // (5 byte) pound value data in 10 bytes.

OLE_1.Object.inputlen = 1

OLE_1.Object.inputMode = 0

The meaning of each property is explained in detail below.

Add the following code in the OLE_1 control on the OLE_1 control

Choose Case Ole_1.object.commmevent

Case 2

OLE_1.Object.rthreshold = 0

String ls_t

INT II

INT W1, W2, W3, FLAG1, FLAG2

For ii = 1 to 10

LS_T = OLE_1.Object.input

FLAG1 = ASC (LS_T) // Take the value of bytes

If FLAG1 = 255 THEN / / Judgment is the starting position

LS_T = OLE_1.Object.input

Flag2 = ASC (LS_T)

if Flag2 = 8 THEN / / Distinguished value is correct

// Read the value of BCD1, BCD2, BCD3

W1 = ASC (OLE_1.Object.input)

W2 = ASC (OLE_1.Object.input)

W3 = ASC (OLE_1.Object.input)

END IF

END IF

NEXT

// convert BCD code to decimal value

W1 = W1 - 6 * INT (W1 / 16)

W2 = W2 - 6 * INT (W2 / 16) W3 = W3 - 6 * INT (W3 / 16)

// calculate high low

ST_1.Text = String (W1 W2 * 100 W3 * 10000)

// Read 10 values, trigger an ONCOMM event

OLE_1.Object.rthreshold = 10

Case 1006

ST_1.Text = "Over RUN"

Case 8020

ST_2.Text = "Read Comm Device Error"

Case 1001

ST_1.Text = "Break Signal Received"

Case Else

ST_1.Text = String (OLE_1.Object.input)

Return

End chaoose

Basic properties of three MSCOMM32 controls

The MSCOMM control is transmitted and received by serial port, providing a serial communication function for the application. MSCOMM controls are very convenient when serial port programming, and programmers don't have to take time to learn more complex API functions, and can be used in languages ​​such as VC, VB, Delphi. Microsoft Communications Control (hereinafter referred to as MSCOMM) is an ActiveX control provided by Microsoft's simplified Windows serial communication programming, which provides an easy way for the application to send and receive data through serial interfaces. Specifically, it provides two methods of processing communication issues: First, an event-driven method, one is the query method.

1. MSCOMM control ways to process communication

The MSCOMM control provides the following two ways of processing communication: event driver and query mode. 1.1 Event Drive

Event Drive Communication is a very effective way to handle serial port interactions. In many cases, a notification is required when an event occurs, for example, in a serial port receiving buffer, or a character arrival or a change occurs in the carrier detect (CD) or REQUEST TO Send (RTS) line. In these cases, these communication events can be captured and processed using the ONCOMM event of the MSCOMM control. OnComm events can also check and process communication errors. All communication events and communication errors are reviews the commevent attribute. In the programming process, you can add your own processing code in the OnComm event handler. The advantage of this method is that the program response is timely and reliability. Each MSCOMM control corresponds to a serial port. If the application needs to access multiple serial ports, multiple MSCOMM controls must be used.

1.2 query mode

The inquiry method is essentially the event driver, but in some cases, this way is more convenient. After each key function of the program, you can query events and errors by checking the value of the commevent attribute. This method may be more desirable if the application is small and self-maintained. For example, if you write a simple phone dialing program, there is no need to generate events per received, because the unique characters wait for the received characters are the "determination" response of the modem.

2. Common Properties of MScomm Control MSCOMM controls have many important properties, but must first be familiar with several properties. CommPort settings and returns the communication port number. Settings sets and returns baud rate, parity, data bit, stop bit in the form of string. Portopen settings and returns the status of the communication port. You can also turn the port and close the port. INPUT returns and deletes characters from the received buffer. OUTPUT writes a string to the transfer buffer. The following describes: CommPort property settings and returns the communication port number. Syntax Object.commport [Value] (VALUE is an integer value, indicating port number.) When designing, Value can set any number from 1 to 16 (the default is 1). However, if you open an unsaved port with the Portopen property, the MSCOMM control generates an error 68 (if the device is invalid). Note: You must set the CommPort property before opening the port. Rthreshold Property: The number of characters to receive before the MSCOMM control is set to COMEVRECEIVE and generate ONCOMM, set and returned. Syntax object.rthreshold [= value] (Value integer expression, illustrating the number of characters to receive before generating the ONCOMM event.) Description When the character is received, if the RTHRESHOLD property is set to 0 (default), the ONCOMM event is not generated. . For example, set RTHRESHOLD to 1, and receive the buffer to receive each character causes the MSCOMM control to generate an ONCOMM event. CTSHOLDING Properties: Determine if data can be sent by querying the status of the Clear To Send (CTS) line. The Clear TO Send is a signal that the modem sends to the associated computer, indicating that the transfer can be performed. This attribute is invalid when designing, is read-only at runtime. Syntax: Object.ctings (Boolean) CTSHOLDING Properties for MSCOMM Control: True Clear TO Send is high. False Clear TO Send is low. Description: If the CTSHolding = false is low (CTSHOLDING = FALSE) and timeout, the MSCOMM control sets the commevent attribute to COMEVENTCTSTO (CLEAR TO Send Timeout) and generates an ONCOMM event. The Clear to Send is used for RTS / CTS (Request to Send / Clear TO Send) hardware handshake. If you need to determine the status of the Clear to Send line, the CTSHOLDING property gives a method of manually query. For more information, please refer to the handshaking property. Sthreshold Property: MSCOMM Control Sets the COMMEVENT property to COMEVSEND, set and return to the minimum number of characters allowed in the transfer buffer before the ONCOMM event is generated. Syntax Object.sthreshold [= value] Value plastic expression, representing the minimum number of characters in the transfer buffer before the OnComm event is generated. Note: If the sthreshold property is set to 0 (default), the data transfer event does not generate an ONCOMM event. If the STHRESHOLD attribute is set, when the transfer buffer is fully empty, the MSCOMM control generates an ONCOMM event.

If the number of characters in the transport buffer is less than Value, the Commmevent property is set to COMEVSEND and generates an ONCOMM event. The COMEVSEND event is activated once when the character number is crossed in the character. For example, if STHRESHOLD is equal to 5, COMEVSEND occurs only when the number of characters from 5 in the output queue will occur. If there is no character than these characters in the output queue, the COMEVSEND event will never happen. Handshake constant constant value describes COMNON 0 No handshake. Comxonxoff 1 xon / xoff handshake. COMRTS 2 Request-to-send / clean-to-send handshake. COMRTSXONXOFF 3 Request-to-send and Clear-to-Send are shaking hands. ONCOMM constant constant value describes comevsend 1 Send an event. ComeVReceive 2 Receive events. COMEVCTS 3 CLEAR-TO-SEND line changes. Comevdsr 4 Data-set Ready line changes. Comevcd 5 Carrier Detect line changes. ComeVRING 6 ringing detection. ComeVeOf 7 file ends. Error constant constants described comEventBreak 1001 receives 1002 Clear-to-send timeout interrupt signal comEventCTSTO comEventDSRTO 1003 Data-set ready frame error timeout comEventFrame 1004 comEventOverrun 1006 port timeout overspeed comEventCDTO 1007 Carrier detect comEventRxOver 1008 receive buffer overflow comEventRxParity 1009 Parity Error comEventTxFull 1010 Transmission Buffer Full COMEVENTDCB 1011 Retrieval Port Device Control Block (DCB) AccountMode constant constant value description COMININPUTMODETEXT 0 (default) Retrieve data in text by input attribute. CominputModeBinary 1 Checks data in binary in binary by input attributes. CDHOLDING Properties: Determine whether there is transmission in the current state by querying the status of the Carrier Detect (CD) line. Carrier Detect is a signal from the modem to the associated computer, indicating that the modem is online. This attribute is invalid when designing, is read-only at runtime. Syntax Object.cdholding Settings: CDHOLDING Attribute Settings to: Settings True Carrier Detect Line for High Level False Carrier Detect Line For low level Description: Note When the carrier detect line is high level (cdholding = true) and super Timely, the MSCOMM control sets the commevent attribute to COMEVENTCDTO (Carrier Detect timeout error) and generates an ONCOMM event. Note that it is especially important to capture a loss in the host application, such as a bulletin board, because the caller can hang (abandon the transmission) at any time. Carrier Detect is also known as Receive Line Signal Detect (RLSD). Data Type BooleandSRHOLDING Properties: Determine the status of the Data Set Ready (DSR) line. The DATA SETETY signal is sent by the modem to the connected computer, indicating the work preparation. This attribute is invalid when designing, is read-only at runtime.

Syntax: Object.dsrholdingObject Indicates an object expression, which is an object in the "Apply to" list. The DSRHOLDING attribute returns the following value: Value Description True Data Set Ready Line Low Description: When the Data Set Ready line is a high level (DSRHOLDING = true), the MSCOMM control sets the commevent attribute to COMEVENTDSRTO (data preparation Timeout) and generate an ONCOMM event. This property is very useful when writing a Data Terminal Equipment (DTE) machine. This property is very useful when the Data Set Ready / Data Terminal Ready handshake routine. Data Type: BooleanSettings Properties: Set and return baud rate, parity, data bits, stop bit parameters. Syntax: Object.Settings [= value] Description: When the port is opened, if the value is illegal, the MSCOMM control generates an error 380 (illegal attribute value). Value consists of four set values, with the following format: "BBBB, P, D, S" BBBB is the baud rate, P is parity, D is the number of data bits, and S is the number of stop bits. The default value of Value is: "9600, N, 8, 1" Inputlen property: Set and return the number of characters read from the received buffer. Syntax object.inputlen [= value] Inputlen attribute syntax includes the following: Value integer expressions, indicating that the INPUT number of characters read from the receive buffer. Description: The default value of the Inputlen property is 0. When INPUTLEN is 0, use INPUT will read all the contents in the receiving buffer. If the Inputlen characters are invalid in the receiving buffer, the input property returns a zero-length string (""). Before using Input, the user can select Check the InbufferCount property to determine if there is a number of characters in the buffer. This attribute is very useful when reading data from the output format for the machine. EOFENABLE Attribute: OK determines if the MSCOMM control is looking for file end (EOF) characters during the input. If you find an EOF character, you will stop the input and activate the oncomm event. At this time, the Commevent property is set to COMEVEOF. Syntax: Object.eOfenable [= value] EOFENABLE The syntax includes the following sections: Value Boolean expression, make sure when you find an EOF character, ONCOMM Whether the event is activated, as described in "Settings". Value settings value: TRUE When the EOF character is found, the ONCOMM event is activated. False (Default) When the EOF character is found, the ONCOMM event is not activated. Description: When the Eofenable property is set to false, the ONCOMM control will not look for EOF characters in the input stream.

转载请注明原文地址:https://www.9cbs.com/read-2656.html

New Post(0)