The specific process
1, set up a serial port related parameter
Ole_1. Object. _CommPort=8
Ole_1. Object. Settings="9600, n, 8, 1", "
Ole_1. Object. InputLen=0
Ole_1. Object. InBufferSize=90
Ole_1. Object. OutBufferSize=60
Ole_1. Object. SThreshold=0
Ole_1. Object. RThreshold=9
Ole_1. Object. The Handshaking=0
Ole_1. Object. PortOpen=True
Ole_1. Object. InBufferCount=0
Ole_1. Object. DTREnable=true
2, open the serial port
Ole_1. Object. PortOpen=True//open the serial port
//empty the receive buffer
The parent. Ole_1. Object. Inbuffercount=0
//to empty send buffer
The parent. Ole_1. Object. Outbuffercount=0
3, send a serial port data and serial data read
String ls_data=', ls_data1='
//send command attention
The parent. Ole_1. Object. The Output="R" + char (13)
//wait for data,
Do
The Yield ()
//data from the Com port
Loop while ole_1. Object. Inbuffercount<1
Ls_data1=parent. Ole_1. Object. Input
ls_data=https://bbs.csdn.net/topics/ls_data + ls_data1
LOOP Until (Pos (ls_data, "OK" + char (13) + char (10)) & gt; 0)
//to the Com port to send data using the Output method
The parent. Ole_1. Object. The Output=ls_data
The parent. Mle_1. Text=ls_data
//close port,
The parent. Ole_1. Object. PortOpen=FALSE
Problem: the program is run to ls_data1=parent. The ole_1. Object. Input errors, please temporarily suspended
CodePudding user response:
Urgent urgent, urgentCodePudding user response:
What do you use controls, mscomm is a Microsoft?Serial communication of Pb (MSCOMM32. OCX) answer all version
In this paper, we give an example of how to gather the serial data in the PB6
A: the problem description
This paper is to solve the acquisition of pounds room pounds value read the problem, the scale and computer (COM1 or COM2) using standard RS - 232 serial line, between $1200 baud rate, data format is as follows: every 100 ms to send a set of data, data in each group have 5 frames, each frame 11, one start bit (0), 8 bits of data (D0 - D7), two stop bits (1), and white parity. Are defined as follows:
Frame 1: D0 - D7 - zero FFH (starting)
Frame 2: D0 - D2 - for the decimal point position (0 to 5)
D3 - NC
D4 said weighing stable -- -- -- -- - 1 0 means weighing not stable
D5-1 0 means weighing weighing is negative for is
D6 - NC
D7 said overload -- -- -- -- - 1
3 frames: D0 - D7, BCD1 (weighing value)
Frame 4: D0 - D7 - BCD2 (weighing value)
Frame: 5 D0 - D7 -- BCD3 (weighing value)
BCD1 BCD2, BCD3 is from low to high weighing value of BCD,
Solution 2:
Using VB MSCOMM32. OCX controls, OLE in PB insert first registered the control in the control, if there is no license after registration done when using tips, is no registration code, the Internet provides a lot of this aspect of the solution, but after the test has failed to solve the problem, my solution is to install the VB, VB when installation to must do choose to install the activeX, turn the VB will solve the problem for certain,
Add the controls in the window, the name is ole_1, set in the window open event ole_1 properties,
Mport=1
ole_1.object.comOle_1. Object. Settings="1200, n, 8, 1", "
Ole_1. Object. Portopen=true
Ole_1. Object. Rthreshold=10//every time take 10 bytes, in 10 bytes set contains a complete//the purpose (5 bytes) pounds value data
Ole_1. Object. Inputlen=1
Ole_1. Object. An inputmode=0
The significance of each attribute in the following detailed explanation,
In the oncomm event of ole_1 controls add the following code
Choose case mevent ole_1.object.com
Case 2
Ole_1. Object. Rthreshold=0
String ls_t
Int ii
Int w1, w2, w3, flag1, flag2
For 2=1 to 10
Ls_t=ole_1. Object. Input
Flag1=asc (ls_t)//select the byte number
If flag1=255 then//determine whether to start bit
Ls_t=ole_1. Object. Input
Flag2=asc (ls_t)
If flag2=8 then//whether the numerical right
//read BCD1 BCD2, BCD3 values
W1=asc (ole_1. Object. Input)
W2=asc (ole_1. Object. Input)
The w3=asc (ole_1. Object. Input)
End the if
End the if
Next
//the BCD code is converted to a decimal value
W1=w1-6 * int (w1/16)
W2=6 w2 - * int (w2/16)
The w3=w3-6 * int (w3/16)
As the hi-lo//
St_1. Text=string (w1 w2 + + w3 * 10000 * 100)
//in the time to read 10 numerical, trigger the oncomm event
Ole_1. Object. Rthreshold=10
Case 1006
St_1. Text="over the run"
Case 8020
St_2. Text="read comm device error"
Case 1001
St_1. Text="break signal received"
In case the else
St_1. Text=string (ole_1. Object. Input)
Return
The end choose
The basic attributes of three control MSCOMM32
MSComm control transmit and receive data through serial port, serial communication function, provide for the application MSComm control is very convenient when a serial port programming, programmers don't have to take the time to understand the more complex API function, and in VC, VB, Delphi, etc all can use in the language, MicrosoftCommunicationsControl is Microsoft company (hereinafter referred to as MSComm) provides a simplified under Windows serial communication programming ActiveX controls, it provides the application through the serial interface is simple and convenient way to send and receive data, specific, it provides two ways of dealing with communication: one is an event-driven (Event - driven) method, one is the query method,
1. The method of communication MSComm control two kinds of processing
MSComm control provides the following two kinds of processing way of communication: event-driven mode and query mode,
1.1 event-driven way
Event-driven communication is to deal with the interactions of serial port, a very effective method in many cases, the need to be notified when events occur, for example, in the serial receive buffer character, or CarrierDetect (CD) or RequestToSend (RTS) online to a character or a change occurs, in these cases, can use MSComm control OnComm event capture and deal with the communication events, OnComm event can also check and deal with the communication error, a list of all communications events and communication error, refer to the CommEvent property, in the process of programming, you can add their own processing in OnComm event handler code, and the advantage of this approach is the program timely response, high reliability, each MSComm control corresponds to a serial port, if your application needs to access multiple serial port, must use more than one MSComm control,
1.2 query mode
Query mode is essentially event-driven, but in some cases, this way is more convenient, in the program after each key function, can query by checking the value of the CommEvent property events and errors, if the application is lesser, and is the keep, this approach may be preferable, for example, if you write a simple telephone dial-up procedures, it should not be necessary to generate events for each receive a character, because the only waits to receive character is "sure" response of the modem,
2. The common attributes of MSComm control
MSComm control has many important properties, but first of all, must be familiar with several attributes,
CommPort set and returns the communication port number,
Settings set in the form of a string and returns the baud rate, parity, data bits, stop bits,
PortOpen set and returns the communication port state, also can open and close the port,
Input from the receive buffer and delete characters,
To transfer the Output buffer to write a string,
The following description:
CommPort property Settings and return to the communication port number,
Grammar object.Com mPort/value (the value type a value, show the port number,)
That, when the design value can be set to any number from 1 to 16 (the default value is 1), but if use PortOpen attribute is opening a port does not exist, the MSComm control will produce error 68 invalid (device),
Note: must be set before the opening ports CommPort property,
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull