Developing serial communication program with Pcommpro

Source: Internet
Author: User
Tags manual

Using C++builder to develop serial communication programs under Win9x is a common headache for programmers, not only to understand many complex API functions, but also to master multithreaded programming. Thankfully, some companies have developed tools for C++builder to write serial communications programs, such as the Moxa Company's Pcomm (which can be downloaded at http:\www.moxa.com.tw), thus helping us solve the problem of serial programming.

The following is a concrete example to illustrate the development of serial communication programs. The programming environment of this program is WIN98 and C ++builder3.0. This programming example is powerful and has the dual function of sending data and automatically receiving data. It is based on a slight modification, that is, you can allow the user to select the communication port for transmission, and set the relevant parameters of the port, including baud rate, data bits, stop bit, parity and flow control.

First, the setting of Pcomm

Start c++builder3.0, click File/newapplication, create a project file, modify the form's Name property to Comm, and then save the name Item (project) as CommTest, and the named unit as Comm.

Pcommlibrary is a dynamic connection library (DLL) file, and when compiling the PComm.dll library using the C++builder compiler, we must tell the C++builder compiler how to find these functions (Sio_xxx ()).

So when we use Pcommpro to develop a serial program in Borlandc++builder, we must do the following two points:

If your pcommpro is installed in the C:programfiles (default installation directory) directory, add the Pcommb.lib file under C:Program filespcommprolib to the C++builder View menu project Manager's project, making it a unit of the project.

Include the #include "c:programfilespcommprolibpcomm.h" in your Comm.cpp.

The form and the setting of the property

Main interface to add controls, and set the name and caption properties of each control according to the settings in table 1.

The communication program works by means of interruption, that is, when there is data in the input cache, it triggers the Pcomm interrupt function Sio_cnt_irq (port,*cntirq,count), which starts the Interrupt Service Program CNTIRQ (), and then the data receives the function Sio_ Read (Port,ibuf,len) to receive the data and do the other corresponding processing, as the function open (), Sent (), close () is the Open button, the Send button, the Click event function to turn off the button; SendData, Receivedata A string variable that corresponds to the Send data edit box and the Receive data edit box, respectively.

Third, the preparation of the main program

Double-clicking the button control on the form produces an event, such as an open () event function, if you double-click the On button. Adding code to these functions, as well as Pcommpro serial control functions, can achieve the processing of serial events. One of the issues to note is that both SendData and Receivedata are ansistring strings, and the strings that are required to be sent and received by Pcommpro functions are char, so use the Pcomm function correctly, and note the string conversions. Ansistring strings can be converted to char by the C_STR () function, while char row strings are simpler to convert to ansistring. You can use Ansistring (char) to cast the char type to the ansistring type. The main program code looks like this:

Void__fastcalltcomm::open (Tobject*sender)
//serial port Open function
{
Inti;
Sio_open (port);//Open serial port
Sio_ IOCTL (port,b2400,p_none| bit_7| STOP_1);
//Set serial port parameters
//include baud rate, data bit, stop bit, parity
Void__stdcall (*p) (int);
P=cntirq
I=sio_cnt_irq (port,*p,1); Set Interrupt function
}
//-----------------
void__fastcalltcomm::sent (tobject*sender)
//Data send function
{
CHAR*SENDDATA=NEWCHAR[20];
Senddata=sentedit->text.c_str ();
//Converts the Ansistring type character
string in Sendedit to char
Sio_write (port,senddata,20);//Send Data
}
//-----------------
Void__fastcalltcomm::close (Tobject*sender)
//serial close function
{
Sio_close (port);//close serial port
}
// -----------------
Void__stdcallcntirq (intport)
//Interrupt Service function (manual generation function)
{
charibuf[20];
ANSISTRINGRECEIVEDATA[20];
Sio_read (port,ibuf,20);//Receive Data
Receivedata=ansistring (IBUF);
//char string Conversion ansistring-type string
receiveedit->text=receivedata;
//Show received string
}

In the program, we use some sio_xxx () type functions, they are pcommpro with the serial communication function (the specific use of the function can refer to Pcommpro help), through these functions, we can set the serial port.

Sio_open (port) and Sio_close (port) to open the serial port and turn off the serial functions, parameter port can be set up the specific operation of the serial port, Sio_ioctl (Intport,intbaud,intmode) for the serial control function, can set the baud rate of the serial port, data bit, stop bit, parity check; As for Sio_write (port) and Sio_read (port), it is read serial port and write serial function; SIO_CNT_IRQ (Intport,void (Callback*func) ( Intport), intcount) is an interrupt function that triggers the function when the serial port has data, and then the function starts its interrupt service program void (CALLBACK *func) (Intport) (for a function pointer), which is called CNTIRQ () function to receive data, which requires manual generation by the programmer.

From this we can see that as long as we modify the program slightly, add some controls to the form so that the sio_xxx () parameters of these functions can be entered by the user interface, which can be done by the user selected for data transmission of the communication port, and set the relevant parameters of the port, including baud rate, data bits, stop bit, Parity check and flow control, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.