PDA

View Full Version : serial port



misytaz
سه شنبه 23 مرداد 1386, 17:40 عصر
می خوام به پورت serial اطلاعات بفرستم و بگیرم . چی کار کنم
(کامل بگیدا)

mzjahromi
سه شنبه 23 مرداد 1386, 18:23 عصر
این کد توی دلفی هست
http://barnamenevis.org/forum/showthread.php?t=39287&highlight=hcom
تبدیلش به builder هم که بلدی

misytaz
سه شنبه 23 مرداد 1386, 18:29 عصر
نه داداش شرمندتم بلد نیستم
می شه بگی
چاکرتم

rezam123
یک شنبه 04 آذر 1386, 12:39 عصر
سلام
دقیقا از سایت 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.

موفق باشید