[ Pobierz całość w formacie PDF ]
.Poniższy przykład ilustruje jedną z metod użycia w programie napisanym w Delphiprzedstawionych funkcji.procedure TForm1.FileListBox1Change(Sender: TObject);.beginhfile_s := _lopen(PChar(FileListBox1.FileName), OF_READ);if (hfile_s HFILE_ERROR) thenbegin._lread(hfile_s, @Buffer_O, cbOutQueue);_lclose(hfile_s);end;end;199Rysunek 5.17.Wygląd formularzaprojektup_RS_16.dprWydruk 5.14.Kod modułu RS_16.pasunit RS_16;interfaceusesWindows, Messages, SysUtils, Classes, Graphics, Controls, Forms,Dialogs, StdCtrls, ComCtrls, FileCtrl;typeTForm1 = class(TForm)CloseComm: TButton;CheckBox1: TCheckBox;OpenComm: TButton;CheckBox2: TCheckBox;CheckBox3: TCheckBox;CheckBox4: TCheckBox;SendFile: TButton;Receive: TButton;RichEdit1: TRichEdit;DriveComboBox1: TDriveComboBox;DirectoryListBox1: TDirectoryListBox;FileListBox1: TFileListBox;Edit1: TEdit;ProgressBar1: TProgressBar;procedure CloseCommClick(Sender: TObject);procedure OpenCommClick(Sender: TObject);procedure SendFileClick(Sender: TObject);procedure ReceiveClick(Sender: TObject);procedure FormCreate(Sender: TObject);200 procedure FileListBox1Change(Sender: TObject);private{ Private declarations }function Write_Comm(hCommDev: THANDLE; lpBuffer: PChar;nNumberOfBytesToWrite: DWORD): Integer;function Read_Comm(hCommDev: THANDLE; Buf_Size: DWORD): Integer;public{ Public declarations }end;varForm1: TForm1;implementation{$R *.DFM}const// -- wartości znaczników sterujących portu szeregowego --dcb_fBinary = $0001;dcb_fParity = $0002;dcb_fOutxCtsFlow = $0004;dcb_fOutxDsrFlow = $0008;// -- fDtrControl --DTR_CONTROL_ENABLE = $0010;DTR_CONTROL_HANDSHAKE = $0020;dcb_fDsrSensitivity = $0040;dcb_fTXContinueOnXoff = $0080;dcb_fOutX = $0100;dcb_fInX = $0200;dcb_fErrorChar = $0400;dcb_fNull = $0800;// -- fRtsControl --RTS_CONTROL_ENABLE = $1000;RTS_CONTROL_HANDSHAKE = $2000;RTS_CONTROL_TOGGLE = $3000;dcb_fAbortOnError = $4000;cbInQueue = 1024;cbOutQueue = 1024;varhfile_s : HFILE; // identyfikator pliku zródłowegoBuffer_O : ARRAY[0.cbOutQueue] of Char; // bufor wyjściowyBuffer_I : ARRAY[0.cbInQueue] of Char; // bufor wejściowyNumber_Bytes_Read : DWORD;hCommDev : THANDLE;lpFileName : LPCSTR;fdwEvtMask : DWORD;Stat : TCOMSTAT;Errors : DWORD;dcb : TDCB;//--------------------------------------------------------------------procedure TForm1.CloseCommClick(Sender: TObject);beginCloseHandle(hCommDev);201 Application.Terminate();end;//--------------------------------------------------------------------procedure TForm1.FormCreate(Sender: TObject);beginDirectoryListBox1.FileList := FileListBox1;DriveComboBox1.DirList := DirectoryListBox1;FileListBox1.FileEdit := Edit1;ProgressBar1.Step := 1;end;//--------------------------------------------------------------------procedure TForm1.FileListBox1Change(Sender: TObject);var i : Integer;beginfor i := 0 to cbOutQueue doBuffer_O[i] := #0; // czyści bufor wyjściowyhfile_s := _lopen(PChar(FileListBox1.FileName), OF_READ);if (hfile_s HFILE_ERROR) thenbegin_lread(hfile_s, @Buffer_O, cbOutQueue);RichEdit1.Text := Buffer_O;_lclose(hfile_s);end;end;//--------------------------------------------------------------------function TForm1.Write_Comm(hCommDev: THANDLE; lpBuffer: PChar;nNumberOfBytesToWrite: DWORD): Integer;varNumberOfBytesWritten : DWORD;begin// EscapeCommFunction(hCommDev, SETRTS);WriteFile(hCommDev, Buffer_O, nNumberOfBytesToWrite,NumberOfBytesWritten, NIL);if (WaitCommEvent(hCommDev, fdwEvtMask, NIL) = TRUE) thenbegin// EscapeCommFunction(hCommDev, CLRRTS);Write_Comm := 1endelseWrite_Comm := 0;end;//--------------------------------------------------------------------function TForm1.Read_Comm(hCommDev: THANDLE;Buf_Size: DWORD): Integer;varnNumberOfBytesToRead: DWORD;beginClearCommError(hCommDev, Errors, @Stat);if (Stat.cbInQue > 0) thenbeginif (Stat.cbInQue > Buf_Size) thennNumberOfBytesToRead := Buf_SizeelsenNumberOfBytesToRead := Stat.cbInQue;ReadFile(hCommDev, Buffer_I, nNumberOfBytesToRead,Number_Bytes_Read, NIL);Read_Comm := 1;202 endelsebeginNumber_Bytes_Read := 0;Read_Comm := 0;end;end;//--------------------------------------------------------------------procedure TForm1.OpenCommClick(Sender: TObject);beginif (CheckBox1.Checked = TRUE) thenlpFileName := 'COM2';if (CheckBox2.Checked = TRUE) thenlpFileName := 'COM1';hCommDev:= CreateFile(lpFileName, GENERIC_READ or GENERIC_WRITE, 0,NIL, OPEN_EXISTING, 0, 0);if (hCommDev INVALID_HANDLE_VALUE) thenbeginSetupComm(hCommDev, cbInQueue, cbOutQueue);dcb.DCBlength := sizeof(dcb);GetCommState(hCommDev, dcb);if (CheckBox3.Checked = TRUE) thendcb.BaudRate := CBR_1200;if (CheckBox4.Checked = TRUE) thendcb.BaudRate := CBR_9600;//-przykładowe ustawienia flag sterujących DCB-dcb.Flags := dcb_fParity;dcb.Parity := ODDPARITY;dcb.StopBits := TWOSTOPBITS;dcb.ByteSize := 8;SetCommState(hCommDev, dcb);GetCommMask(hCommDev, fdwEvtMask);SetCommMask(hCommDev, EV_TXEMPTY);endelsecase hCommDev ofIE_BADID: MessageDlg('Niewłaściwa nazwa portu '+lpFileName+' lub jest on aktywny ',mtError, [mbOk], 0);end;end;//-----------wysyłanie pliku--------------------------------------procedure TForm1.SendFileClick(Sender: TObject);vari : Integer;FileSizeHigh : DWORD;beginfor i := 0 to cbOutQueue doBuffer_O[i] := char(0); // czyści bufor wyjściowyProgressBar1.Max := 0;if (hCommDev > 0) thenbegin203 if((_lopen(PChar(FileListBox1.FileName), OF_READ))HFILE_ERROR) thenbeginhfile_s := _lopen(PChar(FileListBox1.FileName),OF_READ);ProgressBar1.Max := GetFileSize(hfile_s,@FileSizeHigh);while (_lread(hfile_s, @Buffer_O, 1) > 0) dobeginWrite_Comm(hCommDev, Buffer_O, 1); // 1 bajtProgressBar1.StepIt();end;_lclose(hfile_s);FlushFileBuffers(hCommDev);endelseApplication.MessageBox('Nie wybrano pliku do'+' transmisji ', 'Uwaga !',MB_OK);endelseApplication.MessageBox('Niewłaściwa nazwa portu lub jest on'+' aktywny ', 'Uwaga !',MB_OK);end;//------------odbiór danych-------------------------------------------procedure TForm1.ReceiveClick(Sender: TObject);beginif (Read_Comm(hCommDev, SizeOf(Buffer_I)) > 0) thenbeginRichEdit1.Text := Buffer_I;endelsebeginRichEdit1.Text := 'Brak danych do odebrania';Beep();end;end;//--------------------------------------------------------------------end
[ Pobierz całość w formacie PDF ]