- .
. , . : , . , .
, 2.1.
. 2.1 ³
1) __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner) { }.
2) 2,3,4,5,6,7 void __fastcall TForm1::Button N Click(TObject *Sender) N .
COM-. 4, . ϳ 5. ϳ , . 2.1.
. 2.1 COM-
-. 1, (. 2.1 2, . 2.2, 2.6) . , (. 2.5) .
, -. , (. 3.1 3, . 3.7), . , (. 2.5) . , , . .
, ( 2.4). .
. 2.2 -
. 2.3 -
. 2.4 .
. 2.5 ϳ COM-
. 2.6 ³ .
. 2.7 ³ .
- Windows Borland Builder C++. Project1.exe.
- COM USB, . Borland Builder, ++. C++.
/ COM-.
|
|
IBM PC, , :
1. 286DX/66MHz ;
2. 16 ;
3. 3,19 ;
4. Windows 95/98/NT/ME/2000/XP/Vista/Win 7/ Win 8;
5. ;
6. ³ .
˲
1. http://cisco.opennet.ru/docs/RUS/serial_guide/index.html#2_2 RS-232;
2. http://www.softelectro.ru/rs232.html - RS232, COM ;
3. http://www.denvo.ru/pub/hardware/rs-232.html - RS-232;
4. 4 http://www.realcoding.net/articles/programmirovanie-com-portov.html- -
COM ;
5. http://carradio.narod.ru/03.html - /;
6. http://www.comprog.ru/Assembler/article_267.htm - COM ;
7. http://www.alterbit.ru/glossary121.html - ;
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE hComPort, hComPortW, hComPortR;
//
int status = 0; //
int thStW = 0; //
DWORD bytes; // -
char *szComPort; //
char buf[4]; //
char *buf_out = "1"; //
char *buf_in = new char[strlen(buf_out)]; //
int a = 0;
HANDLE hThreadR, hThreadW;
//
DWORD thIDR, thIDW; //
DWORD WINAPI read(LPVOID IpParam);
//
DWORD WINAPI write(LPVOID IpParam);
//
__fastcall TForm1::TForm1(TComponent* Owner) //
: TForm(Owner)
{ }
void __fastcall TForm1::Button1Click(TObject *Sender) //
{
if (status == 1) { //
// CloseHandle(hComPort); //
SuspendThread(hThreadR); //
SuspendThread(hThreadW); //
Shape1->Brush->Color=clInactiveCaption; //
Button1->Caption="ϳ"; //
status = 0; //
}else{ //
hComPort = CreateFileA(COM1->Text.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); //
COMMTIMEOUTS ComTimeouts={100,100,150,100,150}; // ,
SetCommTimeouts(hComPort, &ComTimeouts); //
if (hComPort == INVALID_HANDLE_VALUE) { //
MessageBox(NULL, " ", "", MB_OK); //
}else{ //
CloseHandle(hComPort);
Button1->Caption="³"; //
szComPort = COM1->Text.c_str(); //
|
|
hThreadR = CreateThread(NULL,0,read,0,0,&thIDR);
//
status = 1; //
if(thStW == 1){
//
ResumeThread(hThreadW); //
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
//
{
if(Shape1->Brush->Color == clLime){ //
SuspendThread(hThreadW); //
Shape1->Brush->Color=clInactiveCaption; //
Button2->Caption=" ";
// " "
}else{
Shape1->Brush->Color=clLime; //
Button2->Caption=" ";
// " "
ResumeThread(hThreadW); //
if(thStW == 0){ //
hThreadW = CreateThread(NULL,0,write,0,0,&thIDW); //
thStW = 1; //
}else{
ResumeThread(hThreadW); //
}
}
}
//---------------------------------------------------------------------------
DWORD WINAPI write(LPVOID IpParam){ //
while(1){
WriteFile(hComPort, buf_out, strlen(buf_out), &bytes, NULL); //
}
}
DWORD WINAPI read(LPVOID IpParam){ //
while(1){
hComPort = CreateFileA(Form1->COM1->Text.c_str(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); //
COMMTIMEOUTS ComTimeouts={100,100,150,100,150}; // ,
SetCommTimeouts(hComPort, &ComTimeouts); //
ReadFile(hComPort, buf_in, 1, &bytes, NULL);
CloseHandle(hComPort); //
if(bytes == 1){ //
Form1->Shape2->Brush->Color=clRed; //
}
else{
Form1->Shape2->Brush->Color=clInactiveCaption; //
}
}
}
void __fastcall TForm1::Button3Click(TObject *Sender) //
{
MessageBox(NULL, " - ' .. - ..", "", MB_OK); //
}
//---------------------------------------------------------------------------