ميتونيد اونا رو مثل يك فايل باز كنيد و توشون بنويسيد يا ازشون بخونيد
اين كد باز كردن پورت
کد:
procedure TMainForm.OpenPort(i:Integer);
{}
Procedure InitSerial;
Var
DCB: TDCB;
Config : String;
CommTimeouts : TCommTimeouts;
begin
if not SetupComm(hCom, RxBufferSize, TxBufferSize) then
showMessage('CanNot Setup Com Port');

if not GetCommState(hCom, DCB) then
showmessage('can not read com state')
Else
Begin
Config :=Pchar('baud=19200 parity=n data=8 stop=1'+#0);

if not BuildCommDCB(@Config[1], DCB) then
ShowMessage('Can Not build com dcb')
else
if not SetCommState(hCom, DCB) then
ShowMessage('Can Not set com state');
End;

with CommTimeouts do
begin
ReadIntervalTimeout := 0;
ReadTotalTimeoutMultiplier := 0;
ReadTotalTimeoutConstant := 1000;
WriteTotalTimeoutMultiplier := 0;
WriteTotalTimeoutConstant := 1000;
end;

if not SetCommTimeouts(hCom, CommTimeouts) then
showMessage('Can not set com timeout');
End;
begin
CPN:=i; //initialize serial Port to Boud=9600 Parity=none startbit=1
hCom := CreateFile(PChar(ComPort),
Generic_Read,// Or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if hCom = INVALID_HANDLE_VALUE then
showMessage('Error Opening File')
else
Begin
InitSerial;
End;
end;

اينهم كد خواندن

کد:
function TMainForm.read1byteFromPort:byte;
Var
d: array[1..1] of byte;
s: String;
BytesRead, i: cardinal;
Begin
if not ReadFile (hCom, d, sizeof(d), BytesRead, Nil) then
read1byteFromPort:=0
Else
read1byteFromPort:=d[1];
end;