Harald wrote:
> Hej
>
> Nogen der har et eks. på hvordan man åbner en com port (RS232) og derfter
Jeg har selv arbejdet lidt på en komponent der mangler lidt,
men det fungere da. Der er den ulempe at man ikke kan læse mere end
der er i bufferen. Hvis der er en løsning på det høre jeg det gerne.
Mvh.
Carsten
-------------
unit ComPort;
interface
Uses Windows,SysUtils;
type
TMessagePopUp=procedure(msg:string);
TRS232 = class
private {Private declarations }
{comSecurity:psecurityAttributes;}
comTimeOuts:tCommtimeOuts;
overlapped:tOverlapped;
ComConfig:TCommConfig;
ComHandle:Integer;
MessagePopUp:TMessagePopUp;
WindowsHandle:integer;
protected {Protected declarations }
public {Public declarations }
constructor Create(MessagePopUp_:TMessagePopUp);
function CreateRS232Port(portNo,BoadRate:integer):boolean;
function IsPortFree(portNo:Integer):boolean;
function PortClose:boolean;
function PortSendStr(s:ShortString):integer;
function PortReciveStr(var s:shortString):integer;
Function CommConfig(portNo:Integer):boolean;
published {Published declarations }
End;
implementation
const
comInitCaption_t= 'Comport valg og opsÁtning';
AvanceretIndstillinger_t= 'Avanceret indstillinger';
ValgAfComPort_t= 'Valg af COM port';
BoadRate_t= 'Boad rate:';
openComBool:boolean=false;
constructor TRS232.Create(MessagePopUp_:TMessagePopUp);
Begin
MessagePopUp:=MessagePopUp_;
ComTimeOuts.ReadIntervalTimeOut:=100;
ComTimeOuts.ReadTotalTimeOutMultiplier:=20;
ComTimeOuts.ReadTotalTimeOutConstant:=100;
ComTimeOuts.WriteTotalTimeOutMultiplier:=20;
ComTimeOuts.WriteTotalTimeOutConstant:=100;
overlapped.offset:=0; {Overlapped data}
overlapped.OffsetHigh:=0; {Overlapped data}
overlapped.hEvent:=0; {Overlapped data}
comConfig.Dwsize:=sizeOf(tCommConfig); {St°relse pi array}
comConfig.wVersion:=1; {Driver version
for Win95}
comConfig.dcb.dcbLength:=sizeOf(tDcb); {St°relse pi DCB felt}
End;
Function TRS232.CreateRS232Port(portNo,BoadRate:integer):boolean;
var
ComPortName:array[0..5] of char;
s:String[5];
Begin
s:='COM'+IntToStr(portNo);
StrPCopy(ComPortName,s);
CreateRS232Port:=false;
Comhandle:=CreateFile(ComPortName, {pointer to name of the file}
generic_read+generic_write, {access (read-write) mode}
0, {share mode}
nil, {pointer to security attributes}
open_existing, {how to create}
file_flag_overlapped+ {file attributes}
FILE_ATTRIBUTE_NORMAL,
0); {handle to file with attributes to copy}
if Comhandle<0 then
begin
MessagePopUp('CreateFile:'+SysErrorMessage(getLastError));
exit;
end;
if not GetCommTimeOuts(Comhandle,comTimeOuts) then
begin
MessagePopUp('Get Comm TimeOut:'+SysErrorMessage(getLastError));
exit;
end;
if not setCommTimeOuts(Comhandle,comTimeOuts) then
begin
MessagePopUp('Set Comm TimeOut:'+SysErrorMessage(getLastError));
exit;
end;
if not GetCommConfig(comHandle,comConfig,comConfig.DwSize) then
begin
MessagePopUp('Get Comm Comfig:'+SysErrorMessage(getLastError));
exit;
end;
comConfig.dcb.BaudRate:=boadRate;
if not setCommConfig(comHandle,comConfig,sizeOf(comConfig)) then
begin
MessagePopUp('Set Comm Comfig:'+SysErrorMessage(getLastError));
exit;
end;
CreateRS232Port:=true;
openComBool:=true;
End;
Function TRS232.IsPortFree(portNo:Integer):boolean;
var
ComPortName:array[0..5] of char;
s:String[6];
h:integer;
Begin
s:='COM'+IntToStr(portNo);
StrPCopy(ComPortName,s);
h:=CreateFile(ComPortName, {pointer to name of the file}
generic_read+generic_write, {access (read-write) mode}
0, {share mode}
nil, {pointer to security attributes}
open_existing, {how to create}
file_flag_overlapped+ {file attributes}
FILE_ATTRIBUTE_NORMAL,
0); {handle to file with attributes to copy}
IsPortFree:=h>=0;
CloseHandle(h);
End;
function TRS232.PortClose:boolean;
var
e:LongBool;
Begin
e:=false;
if openComBool then
begin
e:=CloseHandle(comHandle);
if not e then
MessagePopUp('Com Close error:'+SysErrorMessage(getLastError));
end else MessagePopUp('Com Close: File not open');
PortClose:=e;
End;
function TRS232.PortSendStr(s:ShortString):integer;
var
m1:Cardinal;
Begin
writeFile(ComHandle,s[1],ord(s[0]),m1,@overlapped);
PortSendStr:=m1;
End;
function TRS232.PortReciveStr(var s:shortString):integer;
var
m1:Cardinal;
Begin
readFile(ComHandle,s[1],2,m1,@overlapped);
PortReciveStr:=m1;
s[0]:=chr(m1);
End;
Function TRS232.CommConfig(portNo:Integer):boolean;
var
ComPortName:array[0..5] of char;
s:String[5];
Begin
s:='COM'+IntToStr(portNo);
StrPCopy(ComPortName,s);
CommConfig:=true;
if CommConfigDialog(ComPortName,WindowsHandle,comConfig) and
openComBool then
begin
if not setCommConfig(comHandle,comConfig,sizeOf(comConfig)) then
begin
MessagePopUp('Set Comm Comfig:'+SysErrorMessage(getLastError));
CommConfig:=false;
end;
end;
End;
end.
|