PB is a good development tool for database management systems, but PB is not very useful in Web and computer interface communication. Fortunately, we have Ole and COM technologies that make it easy for us to use the functions of some components without having to focus on the underlying business logic. There are two methods to use Pb for serial programming: one is to use Microsoft's communication control (MSComm control) and the other is to use Win32 API functions. The following describes how to use the MSComm control to implement serial programming. The serial communication control mscomm32.ocx (MSComm for short) is a 32-bit ActiveX Control for serial communication programming in Windows provided by Microsoft. This control encapsulates most of the operations of serial communication into the control and provides interface attributes and events for a series of communication commands. It allows the establishment of a serial port connection, you can connect to other communication devices (such as modem), send commands, exchange data, and monitor and respond to various errors and events that may occur during communication, this allows you to use it to create full-duplex event-driven communications. Program . The MSComm control provides two communication methods: event-driven and query. The query method checks the commevent attribute values to determine communication events and errors. When the serial port receives or sends a specified amount of data or the status changes, the MSComm communication control captures these events and errors to trigger the oncomm event, check the value of the commevent attribute of the MSComm control to obtain the events and errors. Code Value to execute the corresponding processing. 1. mscomm32.ocx registration we can create a registration program separately and package it into your application. If the target computer to be installed already has the "MSComm" control, no work is required. If you do not have the "MSComm" control, you need to run the registration program. The PB encoding of the program is as follows: registryset ("hkey_classes_root \ licenses \ javase830-6ac2-11cf-8adb-00aa00c00905", "", regstring !, "Callback") Run ("regsvr32 mscomm32.ocx") MessageBox ('hprompt ', 'Restart the computer') is a simple code, and the mscomm32.ocx file is in the current running directory, after successful registration, a system message is displayed. To make the interface more friendly, you can perform step-by-step modification, such as "Run (" regsvr32/s mscomm32.ocx ")", so that no system message is displayed on the screen. You can also use the API function to restart the Windows system through the "exitwindowsex" function (user32.dll. 2. Use the mscomm32 Control for serial communication mscomm32 control. The common attributes of the control are as follows: comport: set or return the serial port number. The default value is 1 setting: set or return the baud rate, parity bit, data bit, and stop bit of the serial end. The default value is 9600, N, 8, and 1 ". Portopen: if it is set to true, the port is opened. If it is set to false, the port is closed. Inbuffersize: set or return the size of the receiving buffer. The default value is 1024 characters. Inbuffercount: return the number of bytes read by the same generation in the receiving buffer. Set the attribute to 0 to clear the receiving buffer. Outbuffersize: set or return the sending buffer, Which is 512 bytes by default. Outbuffercounter: return the number of characters waiting for sending in the sending buffer. You can use 0 to clear the buffer. Output: send data to the sending buffer rthreshold: This attribute determines that when the number of bytes in the receiving buffer reaches or exceeds this value, an oncomm event with the code "comeventreceive" is generated, and the bit is set to 0, the oncomm event is not generated. Commevent: An oncomm event occurs when a communication error or event occurs, the value of the commevent event can be used to determine the specific errors or events that cause the oncomm event. For more information, see the MSComm control help file. comeventbreak: received interrupt signal comeventframe: frame error detected by hardware. Comeventrxover: receives Buffer Overflow. Comeventtxfull: The sending buffer overflows. Comeventreceive: receives the specified number of characters. Inputlen: set or return the number of inputs read in the receiving buffer. If the value is 0, the content of the entire buffer zone is read. Input: return and delete the data that accepts the buffer. 3. We can set the above attributes based on the needs of our program. The MSComm control provides two communication methods: event-driven and query. Since I only need to communicate with the POS machine, I use a simple query method. For the query method, we only need to use PB to operate the output and input attributes of mscomm32. The following is the sample code: oleobject I _ole_comm // declare the OLE object to connect to the MSComm control I _ole_comm = create oleobjectvresult = I _ole_comm.connecttonewobject ('mscommlib. MSComm ') // initialize the mscomm32 parameter below and enable the serial port if p_comport = 1 then I _ole_comm.commp ORT = 1 // 1 indicates the COM1 port, 2 indicates com2 port elseif p_comport = 2 then I _ole_comm.commp ORT = 2 else I _errcode =-1 I _errtext = 'uo _ rs232ld. f_setdevice: An error occurred while opening the serial port com '+ String (p_comport) +'. Make sure this port exists and is not used by other programs 'Return I _errcodeend ifi_ole_comm.settings = "9600, n, 8, 1 "// The baud rate is set to 9600, this configuration must be consistent with the baud rate set by the poser I _ole_comm.inputlen = fill = bandwidth = trueif not I _ole_comm.portopen then bandwidth = true end if // send the packet I _ole_comm.output = "Hello World" // receives serial packets, wait cyclically for receiving packets uploaded by the POS machine. If the timeout time is exceeded, do while true yield () sleep (20) vinput + = I _ole_comm.input // retrieve data from the COM port if pos (vinput, vend)> 0 Then exit // If a message Terminator exists, a loop is introduced.