i have code work with rs232 in borland delphi. you can translate it to whatever you need.
CODE
unit Unit33;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
Timer1: TTimer;
Label2: TLabel;
Label1: TLabel;
Timer2: TTimer;
Image1: TImage;
procedure FormCreate(Sender: TObject);
procedure q(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hcom,houtfile: thandle;
dcb: tdcb;
stat: tcomstat;
lock: bool;
bytecount: dword;
databuf, x: byte;
const bufsize=sizeof(databuf);
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
x:=0;
stat.cbInQue:=0;
bytecount:=0;
lock:=true;
hcom:=createfile('com3',generic_read or generic_write,0,nil,open_existing,file_attribute_normal,0);
if hcom=invalid_handle_value then begin
showmessage('can''t open port');
application.Terminate;
end;
setupcomm(hcom,1600,1600);
with dcb do begin
baudrate:=57600;
bytesize:=8;
parity:=noparity;
stopbits:=onestopbit;
end;
if not setcommstate(hcom,dcb) then begin
showmessage('can''t set port');
application.Terminate;
end;
houtfile:=createfile('out.txt',generic_read or generic_write,0,nil,open_always,
file_attribute_normal,0);
if houtfile=invalid_handle_value then begin
showmessage('can''t open out file');
application.Terminate;
end;
lock:=false;
end;
procedure TForm1.q(Sender: TObject; var Action: TCloseAction);
begin
closehandle(houtfile);
closehandle(hcom);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var i,blocks,errors,bytes: dword;
begin
if not lock then begin
lock:=true;
clearcommerror(hcom,errors,@stat);
blocks:=stat.cbInQue div bufsize;
if blocks>0 then begin
for i:=1 to blocks do begin
if not readfile(hcom,databuf,bufsize,bytes,nil)
then beep;
writefile(houtfile,databuf,bufsize,bytes,nil);
inc(bytecount,bytes);
image1.Canvas.Pixels[x, 255-databuf]:=0;
inc(x);
if (x > 340) then x:=0;
end;
flushfilebuffers(houtfile);
end;
label1.Caption:='read '+inttostr(blocks)+
' blocks. write in file '+inttostr(bytecount)+' bytes';
form1.Caption:=datetimetostr(date+time);
lock:=false;
end;
end;
end.
Сообщение отредактировал IgorKossak - Apr 6 2011, 07:06
Причина редактирования: Use [codebox] tags