Re: Serial Port
Ben Hague <[email protected]> Mon, 24 Mar 2003 08:44:16 +0000
| Newsgroups | gmane.comp.lang.delphi.kylix |
|---|---|
| Organization | Ravenfield Designs |
| Message-ID | <03032408441600.01089@linux> |
The async professional components have been released under an open source
license, and can be found here http://sourceforge.net/projects/tpaproclx.
If you just want a simple component then this one has worked for me.
{***************************************************************************
comport.pas - simple rs232 communication component for borland kylix 1.0
--------------------------------------------------------------------------
begin : Wed June 13 2001
copyright : (C) 2001 by martin mittelmann
email : [email protected]
***************************************************************************/
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************}
// Note: this component is far from be complete.
// it's very basic and very simple but it suits my needs.
// If you improve this component please let me know.
unit comport;
interface
uses
SysUtils, Types, Classes,libc,KernelIoctl;
type tcom = (ttyS0, ttyS1, ttyS2, ttyS3, ttyp0, ttyp1);
type tBitrate = (br1200, br2400, br4800, br9600, br19200, br38400,br57600,
br115200);
type tparity = (pnone, peven, podd);
type tdatabits = (db8, db7, db6, db5);
type tstopbits = (sb1, sb2);
type tflowcontrol = (hfcNONE, hfcRTSCTS);
type
tcomport = class(TComponent)
private
fstopbits : tstopbits;
fbitrate : tbitrate;
fcomport : tcom;
fopen : boolean;
ComHandle : integer;
fparity : tparity;
fdatabits : tdatabits;
fflow : tflowcontrol;
restBuffer : string;
ComBuffer : array[1..4096] of byte;
ftimeout : boolean;
procedure setopen(b:boolean);
protected
public
function write(s:string):integer;
function writeln(s:string):integer;
function read:string;
function readln:string;
function setRTS(b:boolean):boolean;
published
property BitRate : tbitrate read fbitrate write fbitrate;
property StopBits : tstopbits read fstopbits write fstopbits;
property DataBits : tdatabits read fdatabits write fdatabits;
property Parity : tparity read fparity write fparity;
property ComPort : tcom read fcomport write fcomport;
property Open : boolean read fopen write setopen;
property FlowControl : tflowcontrol read fflow write fflow;
property timeout : boolean read ftimeout;
end;
procedure Register;
implementation
procedure tcomport.setopen(b:boolean);
var ios: termios;
portname : string;
bits,
speed : cardinal;
begin
if b <> fopen then begin
fopen := b;
if fopen then begin //öffnen
case comport of
ttyS0: portname := '/dev/ttyS0';
ttyS1: portname := '/dev/ttyS1';
ttyS2: portname := '/dev/ttyS2';
ttyS3: portname := '/dev/ttyS3';
ttyp0: portname := '/dev/ttyp0';
ttyp1: portname := '/dev/ttyp1';
end;
speed := b9600;
case bitrate of
br1200: speed := B1200;
br2400: speed := B2400;
br4800: SPEED := B4800;
br9600: speed := B9600;
br19200: speed := B19200;
br57600: speed := B57600;
br115200: speed := B115200;
end;
bits := CS8;
case databits of
db5: bits := CS5;
db6: bits := CS6;
db7: bits := CS7;
db8: bits := CS8;
end;
ComHandle:= LibC.Open (pchar(portname),fmOpenReadWrite or o_nonblock);
tcgetattr (ComHandle,ios);
cfmakeraw (ios);
case parity of
pnone : ios.c_cflag := ios.c_cflag and (not PARENB);
peven : begin
ios.c_cflag := ios.c_cflag or PARENB;
ios.c_cflag := ios.c_cflag and (not PARODD);
end;
podd : begin
ios.c_cflag := ios.c_cflag or PARENB;
ios.c_cflag := ios.c_cflag or PARODD;
end;
end;
if FlowControl <> hfcNONE then
ios.c_cflag := ios.c_cflag or CRTSCTS
else
ios.c_cflag := ios.c_cflag and ( not CRTSCTS);
if stopbits = sb1 then ios.c_cflag := ios.c_cflag and ( not CSTOPB);
ios.c_cflag := ios.c_cflag and ( not CSIZE);
ios.c_cflag := ios.c_cflag or bits;
cfsetospeed (ios, speed);
cfsetispeed (ios, speed);
tcsetattr (ComHandle,tcsanow,ios);
end else begin
fileclose(ComHandle);
end;
end;
ftimeout := false;
restBuffer := '';
end;
function tcomport.write(s:string):integer;
begin
result := FileWrite (ComHandle, s[1], Length(s));
end;
function tcomport.writeln(s:string):integer;
begin
s := s + #13;
result := FileWrite (ComHandle, s[1], Length(s));
end;
function tcomport.SetRTS(b:boolean):boolean;
var tbool:integer;
tint:integer;
begin
tint:=TIOCM_RTS;
if b then
tbool:=ioctl(ComHandle,TIOCMBIS,@tint)
else
tbool:=ioctl(ComHandle,TIOCMBIC,@tint);
if tbool<>-1 then
result:=true
else
result:=false;
end;
function tcomport.read:string;
var cnt, BufferPosition: integer;
begin
ftimeout := false;
cnt:= FileRead (ComHandle, ComBuffer, sizeof (ComBuffer));
if cnt > 0 then begin
for BufferPosition:= 1 to cnt do begin
restbuffer:= restbuffer + char(ComBuffer [BufferPosition]);
end;
end;
result := restbuffer;
restbuffer := '';
end;
function tcomport.readln:string;
var nPos,cnt, BufferPosition: integer;
c : char;
begin
ftimeout := false;
cnt:= FileRead (ComHandle, ComBuffer, sizeof (ComBuffer));
if cnt > 0 then begin
for BufferPosition:= 1 to cnt do begin
c := char(ComBuffer [BufferPosition]);
RestBuffer := Restbuffer + c;
end;
end;
result := '';
nPos := pos(#13,restBuffer);
if nPos > 0 then begin
result := copy(restbuffer,1,nPos);
restbuffer := copy(restbuffer,nPos+1,length(restbuffer));
end;
end;
procedure Register;
begin
RegisterComponents('Examples', [tcomport]);
end;
end.
On Friday 21 March 2003 9:51 pm, you wrote:
> I need information to use and set the serial port under Kylix.
>
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/xaxhjB/hdqFAA/xGHJAA/i7folB/TM
---------------------------------------------------------------------~->
-------------------------------------------------------
Forum / mailing list for Borland Kylix programmers
Our web page: http://groups.yahoo.com/group/kylixgroup
To join the forum: [email protected]
To unsubscribe: [email protected]
To post your messages: [email protected]
To contact the moderator: [email protected]
-------------------------------------------------------
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/