papalive Δημοσ. 16 Μαρτίου 2010 Δημοσ. 16 Μαρτίου 2010 Εκανα ενα κώδικα για επικοινωνία με τα ports και θα ήθελα τον σχολιασμό σας. .cpp file //--------------------------------------------------------------------------- #include <vcl.h> #pragma hdrstop #include "PortProject.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { comb->Items->Add("COM1"); comb->Items->Add("COM2"); comb->Items->Add("COM3"); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Form1->Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::CreateFileButtonClick(TObject *Sender) { hPort=CreateFile(lpszPortName=comb->Text.t_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(hPort==INVALID_HANDLE_VALUE) { Memo->Text="Port is closed"; ResetButton->Click(); //return EC_FOPEN; } else { Memo->Lines->Add("Port is Opened"); BaudRateEdit->Text=9600; } } //--------------------------------------------------------------------------- void __fastcall TForm1::combChange(TObject *Sender) { ResetButton->Click(); Memo->Clear(); } //--------------------------------------------------------------------------- void __fastcall TForm1::BuildCommPortClick(TObject *Sender) { dcbCommPort.DCBlength=sizeof(DCB); GetCommState(hPort,&dcbCommPort); dcbCommPort.BaudRate=BaudRateEdit->Text.ToInt(); dcbCommPort.fBinary=TRUE; dcbCommPort.fParity=TRUE; dcbCommPort.fOutxCtsFlow=FALSE; dcbCommPort.fOutxDsrFlow=false; dcbCommPort.fDtrControl=DTR_CONTROL_ENABLE; dcbCommPort.fDsrSensitivity=FALSE; dcbCommPort.fTXContinueOnXoff=TRUE; dcbCommPort.fOutX=FALSE; dcbCommPort.fInX=FALSE; dcbCommPort.fErrorChar=FALSE; dcbCommPort.fNull=FALSE; dcbCommPort.fRtsControl=RTS_CONTROL_ENABLE; dcbCommPort.fAbortOnError=FALSE; dcbCommPort.ByteSize=8; dcbCommPort.Parity=NOPARITY; dcbCommPort.StopBits=ONESTOPBIT; Memo->Lines->Add("Baud Rate is:" + BaudRateEdit->Text); if(!SetCommState(hPort,&dcbCommPort)) { MessageBox(NULL,"Aδυναμία Ρύθμισης Port", "Msg", MB_OK | MB_ICONERROR); ResetButton->Click(); //return EC_INVAL_CONFIG; } GetCommTimeouts(hPort,&CommTimeouts); CommTimeouts.ReadIntervalTimeout=MAXDWORD; CommTimeouts.ReadTotalTimeoutConstant=0; CommTimeouts.ReadTotalTimeoutMultiplier=0; CommTimeouts.WriteTotalTimeoutConstant=100; CommTimeouts.WriteTotalTimeoutMultiplier=10; if(!SetCommTimeouts(hPort,&CommTimeouts)) { MessageBox(NULL,"Aδυναμία Ρύθμισης time-outs Παραμέτρων", "Msg", MB_OK | MB_ICONERROR); ResetButton->Click(); //return EC_INVAL_CONFIG; } } //--------------------------------------------------------------------------- void __fastcall TForm1::ResetButtonClick(TObject *Sender) { hPort=NULL; CloseHandle(hPort); LocalFree(lpMsgBuf); Memo->Clear(); } //--------------------------------------------------------------------------- .h αρχείο //--------------------------------------------------------------------------- #ifndef PortProjectH #define PortProjectH //--------------------------------------------------------------------------- #include <Classes.hpp> #include <Controls.hpp> #include <StdCtrls.hpp> #include <Forms.hpp> #include <windows.h> #include <iostream.h> //--------------------------------------------------------------------------- class TForm1 : public TForm { __published: // IDE-managed Components TComboBox *comb; TLabel *Label1; TButton *Button1; TButton *CreateFileButton; TMemo *Memo; TButton *BuildCommPort; TLabel *Label2; TEdit *BaudRateEdit; TButton *ResetButton; void __fastcall FormCreate(TObject *Sender); void __fastcall Button1Click(TObject *Sender); void __fastcall CreateFileButtonClick(TObject *Sender); void __fastcall combChange(TObject *Sender); void __fastcall BuildCommPortClick(TObject *Sender); void __fastcall ResetButtonClick(TObject *Sender); private: // User declarations HANDLE hPort; LPCSTR lpszPortName; HLOCAL lpMsgBuf; DCB dcbCommPort; COMMTIMEOUTS CommTimeouts; public: // User declarations __fastcall TForm1(TComponent* Owner); }; //--------------------------------------------------------------------------- extern PACKAGE TForm1 *Form1; //--------------------------------------------------------------------------- #endif Προσπάθησα να ανεβάσω και το GUI που εκανα και ενώ zipara τον φάκελο δεν μου το ανεβάζει!!! Οποιος ξέρει κατι ας ενημερώσει να το ανεβάσω.
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.