# Native Code > برنامه نویسی با C > برنامه نویسی با Borland C++‎ Builder >  serial port

## misytaz

می خوام به پورت serial اطلاعات بفرستم و بگیرم . چی کار کنم
 (کامل بگیدا)

----------


## mzjahromi

این کد توی دلفی هست
https://barnamenevis.org/showth...highlight=hcom
تبدیلش به builder هم که بلدی

----------


## misytaz

نه داداش شرمندتم بلد نیستم
می شه بگی 
چاکرتم

----------


## rezam123

سلام
دقیقا از سایت http://www.bcbdev.com/ کپی کردم.
Q: Send and receive data from a serial port (RS-232) 

Answer
DOS programmers may be familiar with how to send and receive serial data by reading and writing directly from the port's hardware. In windows, you do not interface with the hardware directly. Instead, you use the CreateFile, ReadFile, and WriteFile API functions to send and receive data. The following code snippet demonstrates how to open a serial port and send the message "hello world". 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    HANDLE hCom = CreateFile("COM1",
                             GENERIC_READ | GENERIC_WRITE,
                             0,
                             NULL,
                             OPEN_EXISTING,
                             0,
                             NULL );
    if(hCom)
    {
        DCB dcb;
        ZeroMemory(&dcb, sizeof(dcb));
        dcb.DCBlength = sizeof(dcb);
        dcb.BaudRate = 57600;
        dcb.ByteSize = 8;
        dcb.Parity   = NOPARITY;    // NOPARITY and friends are
        dcb.StopBits = ONESTOPBIT;  // #defined in windows.h
        // Set the port properties and write the string out the port.
        if(SetCommState(hCom,&dcb))
        {
            DWORD ByteCount;
            const char *msg = "Hello world!";
            WriteFile(hCom,msg, strlen(msg),&ByteCount,NULL);
        }
        CloseHandle(hCom);
    }
}
This code example demonstrates how to get started with serial port programming in windows, but it is far from being complete. For more information, read the help section entitled "Communications" in the win32.hlp help file. The API provides a host of functions for configuring the port. 
Note: The communications API functions are not that easy to work with from code. You may want to explore using a class or component that encapsulates the serial port. Turbopower sells a product called AsyncPro that can be used for serial communication. There are also some freeware alternatives out there. I also have a a class that encapsulates the serial port. You can download it from the table below. My class is very simple, and probably not quite as powerful as some of the third party products that you can buy. But it does make it a lot easier to send and receive data when compared with the raw api calls. Note that my class is just that, a class. It is not a graphical component. 

موفق باشید

----------

