12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339 |
- {$IFDEF WIN32}
- {$IFNDEF MSWINDOWS}
- {$DEFINE MSWINDOWS}
- {$ENDIF}
- {$ENDIF}
- {$IFDEF LINUX}
- {$IFNDEF UNIX}
- {$DEFINE UNIX}
- {$ENDIF}
- {$ENDIF}
- {$IFDEF FPC}
- {$MODE DELPHI}
- {$IFDEF MSWINDOWS}
- {$ASMMODE intel}
- {$ENDIF}
-
- {$DEFINE NO_LIBC}
- {$ENDIF}
- {$Q-}
- {$H+}
- {$M+}
- unit synaser;
- interface
- uses
- {$IFNDEF MSWINDOWS}
- {$IFNDEF NO_LIBC}
- Libc,
- KernelIoctl,
- {$ELSE}
- termio, baseunix, unix,
- {$ENDIF}
- {$IFNDEF FPC}
- Types,
- {$ENDIF}
- {$ELSE}
- Windows, registry,
- {$IFDEF FPC}
- winver,
- {$ENDIF}
- {$ENDIF}
- synafpc,
- Classes, SysUtils, synautil;
- const
- CR = #$0d;
- LF = #$0a;
- CRLF = CR + LF;
- cSerialChunk = 8192;
- LockfileDirectory = '/var/lock';
- PortIsClosed = -1;
- ErrAlreadyOwned = 9991;
- ErrAlreadyInUse = 9992;
- ErrWrongParameter = 9993;
- ErrPortNotOpen = 9994;
- ErrNoDeviceAnswer = 9995;
- ErrMaxBuffer = 9996;
- ErrTimeout = 9997;
- ErrNotRead = 9998;
- ErrFrame = 9999;
- ErrOverrun = 10000;
- ErrRxOver = 10001;
- ErrRxParity = 10002;
- ErrTxFull = 10003;
- dcb_Binary = $00000001;
- dcb_ParityCheck = $00000002;
- dcb_OutxCtsFlow = $00000004;
- dcb_OutxDsrFlow = $00000008;
- dcb_DtrControlMask = $00000030;
- dcb_DtrControlDisable = $00000000;
- dcb_DtrControlEnable = $00000010;
- dcb_DtrControlHandshake = $00000020;
- dcb_DsrSensivity = $00000040;
- dcb_TXContinueOnXoff = $00000080;
- dcb_OutX = $00000100;
- dcb_InX = $00000200;
- dcb_ErrorChar = $00000400;
- dcb_NullStrip = $00000800;
- dcb_RtsControlMask = $00003000;
- dcb_RtsControlDisable = $00000000;
- dcb_RtsControlEnable = $00001000;
- dcb_RtsControlHandshake = $00002000;
- dcb_RtsControlToggle = $00003000;
- dcb_AbortOnError = $00004000;
- dcb_Reserveds = $FFFF8000;
-
- SB1 = 0;
-
- SB1andHalf = 1;
-
- SB2 = 2;
- {$IFNDEF MSWINDOWS}
- const
- INVALID_HANDLE_VALUE = THandle(-1);
- CS7fix = $0000020;
- type
- TDCB = record
- DCBlength: DWORD;
- BaudRate: DWORD;
- Flags: Longint;
- wReserved: Word;
- XonLim: Word;
- XoffLim: Word;
- ByteSize: Byte;
- Parity: Byte;
- StopBits: Byte;
- XonChar: CHAR;
- XoffChar: CHAR;
- ErrorChar: CHAR;
- EofChar: CHAR;
- EvtChar: CHAR;
- wReserved1: Word;
- end;
- PDCB = ^TDCB;
- const
- {$IFDEF UNIX}
- {$IFDEF DARWIN}
- MaxRates = 18;
- {$ELSE}
- MaxRates = 30;
- {$ENDIF}
- {$ELSE}
- MaxRates = 19;
- {$ENDIF}
- Rates: array[0..MaxRates, 0..1] of cardinal =
- (
- (0, B0),
- (50, B50),
- (75, B75),
- (110, B110),
- (134, B134),
- (150, B150),
- (200, B200),
- (300, B300),
- (600, B600),
- (1200, B1200),
- (1800, B1800),
- (2400, B2400),
- (4800, B4800),
- (9600, B9600),
- (19200, B19200),
- (38400, B38400),
- (57600, B57600),
- (115200, B115200),
- (230400, B230400)
- {$IFNDEF DARWIN}
- ,(460800, B460800)
- {$IFDEF UNIX}
- ,(500000, B500000),
- (576000, B576000),
- (921600, B921600),
- (1000000, B1000000),
- (1152000, B1152000),
- (1500000, B1500000),
- (2000000, B2000000),
- (2500000, B2500000),
- (3000000, B3000000),
- (3500000, B3500000),
- (4000000, B4000000)
- {$ENDIF}
- {$ENDIF}
- );
- {$ENDIF}
- {$IFDEF DARWIN}
- const
- O_SYNC = $0080;
- {$ENDIF}
- const
- sOK = 0;
- sErr = integer(-1);
- type
-
- THookSerialReason = (
- HR_SerialClose,
- HR_Connect,
- HR_CanRead,
- HR_CanWrite,
- HR_ReadCount,
- HR_WriteCount,
- HR_Wait
- );
-
- THookSerialStatus = procedure(Sender: TObject; Reason: THookSerialReason;
- const Value: string) of object;
-
- ESynaSerError = class(Exception)
- public
- ErrorCode: integer;
- ErrorMessage: string;
- end;
-
- TBlockSerial = class(TObject)
- protected
- FOnStatus: THookSerialStatus;
- Fhandle: THandle;
- FTag: integer;
- FDevice: string;
- FLastError: integer;
- FLastErrorDesc: string;
- FBuffer: AnsiString;
- FRaiseExcept: boolean;
- FRecvBuffer: integer;
- FSendBuffer: integer;
- FModemWord: integer;
- FRTSToggle: Boolean;
- FDeadlockTimeout: integer;
- FInstanceActive: boolean;
- FTestDSR: Boolean;
- FTestCTS: Boolean;
- FLastCR: Boolean;
- FLastLF: Boolean;
- FMaxLineLength: Integer;
- FLinuxLock: Boolean;
- FMaxSendBandwidth: Integer;
- FNextSend: LongWord;
- FMaxRecvBandwidth: Integer;
- FNextRecv: LongWord;
- FConvertLineEnd: Boolean;
- FATResult: Boolean;
- FAtTimeout: integer;
- FInterPacketTimeout: Boolean;
- FComNr: integer;
- {$IFDEF MSWINDOWS}
- FPortAddr: Word;
- function CanEvent(Event: dword; Timeout: integer): boolean;
- procedure DecodeCommError(Error: DWord); virtual;
- function GetPortAddr: Word; virtual;
- function ReadTxEmpty(PortAddr: Word): Boolean; virtual;
- {$ENDIF}
- procedure SetSizeRecvBuffer(size: integer); virtual;
- function GetDSR: Boolean; virtual;
- procedure SetDTRF(Value: Boolean); virtual;
- function GetCTS: Boolean; virtual;
- procedure SetRTSF(Value: Boolean); virtual;
- function GetCarrier: Boolean; virtual;
- function GetRing: Boolean; virtual;
- procedure DoStatus(Reason: THookSerialReason; const Value: string); virtual;
- procedure GetComNr(Value: string); virtual;
- function PreTestFailing: boolean; virtual;
- function TestCtrlLine: Boolean; virtual;
- {$IFDEF UNIX}
- procedure DcbToTermios(const dcb: TDCB; var term: termios); virtual;
- procedure TermiosToDcb(const term: termios; var dcb: TDCB); virtual;
- function ReadLockfile: integer; virtual;
- function LockfileName: String; virtual;
- procedure CreateLockfile(PidNr: integer); virtual;
- {$ENDIF}
- procedure LimitBandwidth(Length: Integer; MaxB: integer; var Next: LongWord); virtual;
- procedure SetBandwidth(Value: Integer); virtual;
- public
-
- DCB: Tdcb;
- {$IFDEF UNIX}
- TermiosStruc: termios;
- {$ENDIF}
-
- constructor Create;
-
- destructor Destroy; override;
-
- class function GetVersion: string; virtual;
-
- procedure CloseSocket; virtual;
-
- procedure Config(baud, bits: integer; parity: char; stop: integer;
- softflow, hardflow: boolean); virtual;
-
- procedure Connect(comport: string); virtual;
-
- procedure SetCommState; virtual;
-
- procedure GetCommState; virtual;
-
- function SendBuffer(buffer: pointer; length: integer): integer; virtual;
-
- procedure SendByte(data: byte); virtual;
-
- procedure SendString(data: AnsiString); virtual;
-
- procedure SendInteger(Data: integer); virtual;
-
- procedure SendBlock(const Data: AnsiString); virtual;
-
- procedure SendStreamRaw(const Stream: TStream); virtual;
-
- procedure SendStream(const Stream: TStream); virtual;
-
- procedure SendStreamIndy(const Stream: TStream); virtual;
-
- function RecvBuffer(buffer: pointer; length: integer): integer; virtual;
-
- function RecvBufferEx(buffer: pointer; length: integer; timeout: integer): integer; virtual;
-
- function RecvBufferStr(Length: Integer; Timeout: Integer): AnsiString; virtual;
-
- function RecvPacket(Timeout: Integer): AnsiString; virtual;
-
- function RecvByte(timeout: integer): byte; virtual;
-
- function RecvTerminated(Timeout: Integer; const Terminator: AnsiString): AnsiString; virtual;
-
- function Recvstring(timeout: integer): AnsiString; virtual;
-
- function RecvInteger(Timeout: Integer): Integer; virtual;
-
- function RecvBlock(Timeout: Integer): AnsiString; virtual;
-
- procedure RecvStreamRaw(const Stream: TStream; Timeout: Integer); virtual;
-
- procedure RecvStreamSize(const Stream: TStream; Timeout: Integer; Size: Integer); virtual;
-
- procedure RecvStream(const Stream: TStream; Timeout: Integer); virtual;
-
- procedure RecvStreamIndy(const Stream: TStream; Timeout: Integer); virtual;
-
- function WaitingData: integer; virtual;
-
- function WaitingDataEx: integer; virtual;
-
- function SendingData: integer; virtual;
-
- procedure EnableRTSToggle(value: boolean); virtual;
-
- procedure Flush; virtual;
-
- procedure Purge; virtual;
-
- function CanRead(Timeout: integer): boolean; virtual;
-
- function CanWrite(Timeout: integer): boolean; virtual;
-
- function CanReadEx(Timeout: integer): boolean; virtual;
-
- function ModemStatus: integer; virtual;
-
- procedure SetBreak(Duration: integer); virtual;
-
- function ATCommand(value: AnsiString): AnsiString; virtual;
-
- function ATConnect(value: AnsiString): AnsiString; virtual;
-
- function SerialCheck(SerialResult: integer): integer; virtual;
-
- procedure ExceptCheck; virtual;
-
- procedure SetSynaError(ErrNumber: integer); virtual;
-
- procedure RaiseSynaError(ErrNumber: integer); virtual;
- {$IFDEF UNIX}
- function cpomComportAccessible: boolean; virtual;
- procedure cpomReleaseComport; virtual;
- {$ENDIF}
-
- property Device: string read FDevice;
-
- property LastError: integer read FLastError;
-
- property LastErrorDesc: string read FLastErrorDesc;
-
- property ATResult: Boolean read FATResult;
-
- property RTS: Boolean write SetRTSF;
-
- property CTS: boolean read GetCTS;
-
- property DTR: Boolean write SetDTRF;
-
- property DSR: boolean read GetDSR;
-
- property Carrier: boolean read GetCarrier;
-
- property Ring: boolean read GetRing;
-
- property InstanceActive: boolean read FInstanceActive;
-
- property MaxSendBandwidth: Integer read FMaxSendBandwidth Write FMaxSendBandwidth;
-
- property MaxRecvBandwidth: Integer read FMaxRecvBandwidth Write FMaxRecvBandwidth;
-
- property MaxBandwidth: Integer Write SetBandwidth;
-
- property SizeRecvBuffer: integer read FRecvBuffer write SetSizeRecvBuffer;
- published
-
- class function GetErrorDesc(ErrorCode: integer): string;
-
- property Tag: integer read FTag write FTag;
-
- property Handle: THandle read Fhandle write FHandle;
-
- property LineBuffer: AnsiString read FBuffer write FBuffer;
-
- property RaiseExcept: boolean read FRaiseExcept write FRaiseExcept;
-
- property OnStatus: THookSerialStatus read FOnStatus write FOnStatus;
-
- property TestDSR: boolean read FTestDSR write FTestDSR;
-
- property TestCTS: boolean read FTestCTS write FTestCTS;
-
- property MaxLineLength: Integer read FMaxLineLength Write FMaxLineLength;
-
- property DeadlockTimeout: Integer read FDeadlockTimeout Write FDeadlockTimeout;
-
- property LinuxLock: Boolean read FLinuxLock write FLinuxLock;
-
- property ConvertLineEnd: Boolean read FConvertLineEnd Write FConvertLineEnd;
-
- property AtTimeout: integer read FAtTimeout Write FAtTimeout;
-
- property InterPacketTimeout: Boolean read FInterPacketTimeout Write FInterPacketTimeout;
- end;
- function GetSerialPortNames: string;
- implementation
- constructor TBlockSerial.Create;
- begin
- inherited create;
- FRaiseExcept := false;
- FHandle := INVALID_HANDLE_VALUE;
- FDevice := '';
- FComNr:= PortIsClosed;
- FInstanceActive:= false;
- Fbuffer := '';
- FRTSToggle := False;
- FMaxLineLength := 0;
- FTestDSR := False;
- FTestCTS := False;
- FDeadlockTimeout := 30000;
- FLinuxLock := True;
- FMaxSendBandwidth := 0;
- FNextSend := 0;
- FMaxRecvBandwidth := 0;
- FNextRecv := 0;
- FConvertLineEnd := False;
- SetSynaError(sOK);
- FRecvBuffer := 4096;
- FLastCR := False;
- FLastLF := False;
- FAtTimeout := 1000;
- FInterPacketTimeout := True;
- end;
- destructor TBlockSerial.Destroy;
- begin
- CloseSocket;
- inherited destroy;
- end;
- class function TBlockSerial.GetVersion: string;
- begin
- Result := 'SynaSer 7.5.0';
- end;
- procedure TBlockSerial.CloseSocket;
- begin
- if Fhandle <> INVALID_HANDLE_VALUE then
- begin
- Purge;
- RTS := False;
- DTR := False;
- FileClose(FHandle);
- end;
- if InstanceActive then
- begin
- {$IFDEF UNIX}
- if FLinuxLock then
- cpomReleaseComport;
- {$ENDIF}
- FInstanceActive:= false
- end;
- Fhandle := INVALID_HANDLE_VALUE;
- FComNr:= PortIsClosed;
- SetSynaError(sOK);
- DoStatus(HR_SerialClose, FDevice);
- end;
- {$IFDEF MSWINDOWS}
- function TBlockSerial.GetPortAddr: Word;
- begin
- Result := 0;
- if Win32Platform <> VER_PLATFORM_WIN32_NT then
- begin
- EscapeCommFunction(FHandle, 10);
- asm
- MOV @Result, DX;
- end;
- end;
- end;
- function TBlockSerial.ReadTxEmpty(PortAddr: Word): Boolean;
- begin
- Result := True;
- if Win32Platform <> VER_PLATFORM_WIN32_NT then
- begin
- asm
- MOV DX, PortAddr;
- ADD DX, 5;
- IN AL, DX;
- AND AL, $40;
- JZ @K;
- MOV AL,1;
- @K: MOV @Result, AL;
- end;
- end;
- end;
- {$ENDIF}
- procedure TBlockSerial.GetComNr(Value: string);
- begin
- FComNr := PortIsClosed;
- if pos('COM', uppercase(Value)) = 1 then
- FComNr := StrToIntdef(copy(Value, 4, Length(Value) - 3), PortIsClosed + 1) - 1;
- if pos('/DEV/TTYS', uppercase(Value)) = 1 then
- FComNr := StrToIntdef(copy(Value, 10, Length(Value) - 9), PortIsClosed - 1);
- end;
- procedure TBlockSerial.SetBandwidth(Value: Integer);
- begin
- MaxSendBandwidth := Value;
- MaxRecvBandwidth := Value;
- end;
- procedure TBlockSerial.LimitBandwidth(Length: Integer; MaxB: integer; var Next: LongWord);
- var
- x: LongWord;
- y: LongWord;
- begin
- if MaxB > 0 then
- begin
- y := GetTick;
- if Next > y then
- begin
- x := Next - y;
- if x > 0 then
- begin
- DoStatus(HR_Wait, IntToStr(x));
- sleep(x);
- end;
- end;
- Next := GetTick + Trunc((Length / MaxB) * 1000);
- end;
- end;
- procedure TBlockSerial.Config(baud, bits: integer; parity: char; stop: integer;
- softflow, hardflow: boolean);
- begin
- FillChar(dcb, SizeOf(dcb), 0);
- GetCommState;
- dcb.DCBlength := SizeOf(dcb);
- dcb.BaudRate := baud;
- dcb.ByteSize := bits;
- case parity of
- 'N', 'n': dcb.parity := 0;
- 'O', 'o': dcb.parity := 1;
- 'E', 'e': dcb.parity := 2;
- 'M', 'm': dcb.parity := 3;
- 'S', 's': dcb.parity := 4;
- end;
- dcb.StopBits := stop;
- dcb.XonChar := #17;
- dcb.XoffChar := #19;
- dcb.XonLim := FRecvBuffer div 4;
- dcb.XoffLim := FRecvBuffer div 4;
- dcb.Flags := dcb_Binary;
- if softflow then
- dcb.Flags := dcb.Flags or dcb_OutX or dcb_InX;
- if hardflow then
- dcb.Flags := dcb.Flags or dcb_OutxCtsFlow or dcb_RtsControlHandshake
- else
- dcb.Flags := dcb.Flags or dcb_RtsControlEnable;
- dcb.Flags := dcb.Flags or dcb_DtrControlEnable;
- if dcb.Parity > 0 then
- dcb.Flags := dcb.Flags or dcb_ParityCheck;
- SetCommState;
- end;
- procedure TBlockSerial.Connect(comport: string);
- {$IFDEF MSWINDOWS}
- var
- CommTimeouts: TCommTimeouts;
- {$ENDIF}
- begin
-
- if InstanceActive then
- begin
- RaiseSynaError(ErrAlreadyInUse);
- Exit;
- end;
- FBuffer := '';
- FDevice := comport;
- GetComNr(comport);
- {$IFDEF MSWINDOWS}
- SetLastError (sOK);
- {$ELSE}
- {$IFNDEF FPC}
- SetLastError (sOK);
- {$ELSE}
- fpSetErrno(sOK);
- {$ENDIF}
- {$ENDIF}
- {$IFNDEF MSWINDOWS}
- if FComNr <> PortIsClosed then
- FDevice := '/dev/ttyS' + IntToStr(FComNr);
-
- if FLinuxLock then
- if not cpomComportAccessible then
- begin
- RaiseSynaError(ErrAlreadyOwned);
- Exit;
- end;
- {$IFNDEF FPC}
- FHandle := THandle(Libc.open(pchar(FDevice), O_RDWR or O_SYNC));
- {$ELSE}
- FHandle := THandle(fpOpen(FDevice, O_RDWR or O_SYNC));
- {$ENDIF}
- if FHandle = INVALID_HANDLE_VALUE then
- SerialCheck(-1)
- else
- SerialCheck(0);
- {$IFDEF UNIX}
- if FLastError <> sOK then
- if FLinuxLock then
- cpomReleaseComport;
- {$ENDIF}
- ExceptCheck;
- if FLastError <> sOK then
- Exit;
- {$ELSE}
- if FComNr <> PortIsClosed then
- FDevice := '\\.\COM' + IntToStr(FComNr + 1);
- FHandle := THandle(CreateFile(PChar(FDevice), GENERIC_READ or GENERIC_WRITE,
- 0, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED, 0));
- if FHandle = INVALID_HANDLE_VALUE then
- SerialCheck(-1)
- else
- SerialCheck(0);
- ExceptCheck;
- if FLastError <> sOK then
- Exit;
- SetCommMask(FHandle, 0);
- SetupComm(Fhandle, FRecvBuffer, 0);
- CommTimeOuts.ReadIntervalTimeout := MAXWORD;
- CommTimeOuts.ReadTotalTimeoutMultiplier := 0;
- CommTimeOuts.ReadTotalTimeoutConstant := 0;
- CommTimeOuts.WriteTotalTimeoutMultiplier := 0;
- CommTimeOuts.WriteTotalTimeoutConstant := 0;
- SetCommTimeOuts(FHandle, CommTimeOuts);
- FPortAddr := GetPortAddr;
- {$ENDIF}
- SetSynaError(sOK);
- if not TestCtrlLine then
- begin
- SetSynaError(ErrNoDeviceAnswer);
- FileClose(FHandle);
- {$IFDEF UNIX}
- if FLinuxLock then
- cpomReleaseComport;
- {$ENDIF}
- Fhandle := INVALID_HANDLE_VALUE;
- FComNr:= PortIsClosed;
- end
- else
- begin
- FInstanceActive:= True;
- RTS := True;
- DTR := True;
- Purge;
- end;
- ExceptCheck;
- DoStatus(HR_Connect, FDevice);
- end;
- function TBlockSerial.SendBuffer(buffer: pointer; length: integer): integer;
- {$IFDEF MSWINDOWS}
- var
- Overlapped: TOverlapped;
- x, y, Err: DWord;
- {$ENDIF}
- begin
- Result := 0;
- if PreTestFailing then
- Exit;
- LimitBandwidth(Length, FMaxSendBandwidth, FNextsend);
- if FRTSToggle then
- begin
- Flush;
- RTS := True;
- end;
- {$IFNDEF MSWINDOWS}
- result := FileWrite(Fhandle, Buffer^, Length);
- serialcheck(result);
- {$ELSE}
- FillChar(Overlapped, Sizeof(Overlapped), 0);
- SetSynaError(sOK);
- y := 0;
- if not WriteFile(FHandle, Buffer^, Length, DWord(Result), @Overlapped) then
- y := GetLastError;
- if y = ERROR_IO_PENDING then
- begin
- x := WaitForSingleObject(FHandle, FDeadlockTimeout);
- if x = WAIT_TIMEOUT then
- begin
- PurgeComm(FHandle, PURGE_TXABORT);
- SetSynaError(ErrTimeout);
- end;
- GetOverlappedResult(FHandle, Overlapped, Dword(Result), False);
- end
- else
- SetSynaError(y);
- ClearCommError(FHandle, err, nil);
- if err <> 0 then
- DecodeCommError(err);
- {$ENDIF}
- if FRTSToggle then
- begin
- Flush;
- CanWrite(255);
- RTS := False;
- end;
- ExceptCheck;
- DoStatus(HR_WriteCount, IntToStr(Result));
- end;
- procedure TBlockSerial.SendByte(data: byte);
- begin
- SendBuffer(@Data, 1);
- end;
- procedure TBlockSerial.SendString(data: AnsiString);
- begin
- SendBuffer(Pointer(Data), Length(Data));
- end;
- procedure TBlockSerial.SendInteger(Data: integer);
- begin
- SendBuffer(@data, SizeOf(Data));
- end;
- procedure TBlockSerial.SendBlock(const Data: AnsiString);
- begin
- SendInteger(Length(data));
- SendString(Data);
- end;
- procedure TBlockSerial.SendStreamRaw(const Stream: TStream);
- var
- si: integer;
- x, y, yr: integer;
- s: AnsiString;
- begin
- si := Stream.Size - Stream.Position;
- x := 0;
- while x < si do
- begin
- y := si - x;
- if y > cSerialChunk then
- y := cSerialChunk;
- Setlength(s, y);
- yr := Stream.read(PAnsiChar(s)^, y);
- if yr > 0 then
- begin
- SetLength(s, yr);
- SendString(s);
- Inc(x, yr);
- end
- else
- break;
- end;
- end;
- procedure TBlockSerial.SendStreamIndy(const Stream: TStream);
- var
- si: integer;
- begin
- si := Stream.Size - Stream.Position;
- si := Swapbytes(si);
- SendInteger(si);
- SendStreamRaw(Stream);
- end;
- procedure TBlockSerial.SendStream(const Stream: TStream);
- var
- si: integer;
- begin
- si := Stream.Size - Stream.Position;
- SendInteger(si);
- SendStreamRaw(Stream);
- end;
- function TBlockSerial.RecvBuffer(buffer: pointer; length: integer): integer;
- {$IFNDEF MSWINDOWS}
- begin
- Result := 0;
- if PreTestFailing then
- Exit;
- LimitBandwidth(Length, FMaxRecvBandwidth, FNextRecv);
- result := FileRead(FHandle, Buffer^, length);
- serialcheck(result);
- {$ELSE}
- var
- Overlapped: TOverlapped;
- x, y, Err: DWord;
- begin
- Result := 0;
- if PreTestFailing then
- Exit;
- LimitBandwidth(Length, FMaxRecvBandwidth, FNextRecv);
- FillChar(Overlapped, Sizeof(Overlapped), 0);
- SetSynaError(sOK);
- y := 0;
- if not ReadFile(FHandle, Buffer^, length, Dword(Result), @Overlapped) then
- y := GetLastError;
- if y = ERROR_IO_PENDING then
- begin
- x := WaitForSingleObject(FHandle, FDeadlockTimeout);
- if x = WAIT_TIMEOUT then
- begin
- PurgeComm(FHandle, PURGE_RXABORT);
- SetSynaError(ErrTimeout);
- end;
- GetOverlappedResult(FHandle, Overlapped, Dword(Result), False);
- end
- else
- SetSynaError(y);
- ClearCommError(FHandle, err, nil);
- if err <> 0 then
- DecodeCommError(err);
- {$ENDIF}
- ExceptCheck;
- DoStatus(HR_ReadCount, IntToStr(Result));
- end;
- function TBlockSerial.RecvBufferEx(buffer: pointer; length: integer; timeout: integer): integer;
- var
- s: AnsiString;
- rl, l: integer;
- ti: LongWord;
- begin
- Result := 0;
- if PreTestFailing then
- Exit;
- SetSynaError(sOK);
- rl := 0;
- repeat
- ti := GetTick;
- s := RecvPacket(Timeout);
- l := System.Length(s);
- if (rl + l) > Length then
- l := Length - rl;
- Move(Pointer(s)^, IncPoint(Buffer, rl)^, l);
- rl := rl + l;
- if FLastError <> sOK then
- Break;
- if rl >= Length then
- Break;
- if not FInterPacketTimeout then
- begin
- Timeout := Timeout - integer(TickDelta(ti, GetTick));
- if Timeout <= 0 then
- begin
- SetSynaError(ErrTimeout);
- Break;
- end;
- end;
- until False;
- delete(s, 1, l);
- FBuffer := s;
- Result := rl;
- end;
- function TBlockSerial.RecvBufferStr(Length: Integer; Timeout: Integer): AnsiString;
- var
- x: integer;
- begin
- Result := '';
- if PreTestFailing then
- Exit;
- SetSynaError(sOK);
- if Length > 0 then
- begin
- Setlength(Result, Length);
- x := RecvBufferEx(PAnsiChar(Result), Length , Timeout);
- if FLastError = sOK then
- SetLength(Result, x)
- else
- Result := '';
- end;
- end;
- function TBlockSerial.RecvPacket(Timeout: Integer): AnsiString;
- var
- x: integer;
- begin
- Result := '';
- if PreTestFailing then
- Exit;
- SetSynaError(sOK);
- if FBuffer <> '' then
- begin
- Result := FBuffer;
- FBuffer := '';
- end
- else
- begin
-
- Sleep(0);
- x := WaitingData;
- if x > 0 then
- begin
- SetLength(Result, x);
- x := RecvBuffer(Pointer(Result), x);
- if x >= 0 then
- SetLength(Result, x);
- end
- else
- begin
- if CanRead(Timeout) then
- begin
- x := WaitingData;
- if x = 0 then
- SetSynaError(ErrTimeout);
- if x > 0 then
- begin
- SetLength(Result, x);
- x := RecvBuffer(Pointer(Result), x);
- if x >= 0 then
- SetLength(Result, x);
- end;
- end
- else
- SetSynaError(ErrTimeout);
- end;
- end;
- ExceptCheck;
- end;
- function TBlockSerial.RecvByte(timeout: integer): byte;
- begin
- Result := 0;
- if PreTestFailing then
- Exit;
- SetSynaError(sOK);
- if FBuffer = '' then
- FBuffer := RecvPacket(Timeout);
- if (FLastError = sOK) and (FBuffer <> '') then
- begin
- Result := Ord(FBuffer[1]);
- System.Delete(FBuffer, 1, 1);
- end;
- ExceptCheck;
- end;
- function TBlockSerial.RecvTerminated(Timeout: Integer; const Terminator: AnsiString): AnsiString;
- var
- x: Integer;
- s: AnsiString;
- l: Integer;
- CorCRLF: Boolean;
- t: ansistring;
- tl: integer;
- ti: LongWord;
- begin
- Result := '';
- if PreTestFailing then
- Exit;
- SetSynaError(sOK);
- l := system.Length(Terminator);
- if l = 0 then
- Exit;
- tl := l;
- CorCRLF := FConvertLineEnd and (Terminator = CRLF);
- s := '';
- x := 0;
- repeat
- ti := GetTick;
-
- s := s + RecvPacket(Timeout);
- if FLastError <> sOK then
- Break;
- x := 0;
- if Length(s) > 0 then
- if CorCRLF then
- begin
- if FLastCR and (s[1] = LF) then
- Delete(s, 1, 1);
- if FLastLF and (s[1] = CR) then
- Delete(s, 1, 1);
- FLastCR := False;
- FLastLF := False;
- t := '';
- x := PosCRLF(s, t);
- tl := system.Length(t);
- if t = CR then
- FLastCR := True;
- if t = LF then
- FLastLF := True;
- end
- else
- begin
- x := pos(Terminator, s);
- tl := l;
- end;
- if (FMaxLineLength <> 0) and (system.Length(s) > FMaxLineLength) then
- begin
- SetSynaError(ErrMaxBuffer);
- Break;
- end;
- if x > 0 then
- Break;
- if not FInterPacketTimeout then
- begin
- Timeout := Timeout - integer(TickDelta(ti, GetTick));
- if Timeout <= 0 then
- begin
- SetSynaError(ErrTimeout);
- Break;
- end;
- end;
- until False;
- if x > 0 then
- begin
- Result := Copy(s, 1, x - 1);
- System.Delete(s, 1, x + tl - 1);
- end;
- FBuffer := s;
- ExceptCheck;
- end;
- function TBlockSerial.RecvString(Timeout: Integer): AnsiString;
- var
- s: AnsiString;
- begin
- Result := '';
- s := RecvTerminated(Timeout, #13 + #10);
- if FLastError = sOK then
- Result := s;
- end;
- function TBlockSerial.RecvInteger(Timeout: Integer): Integer;
- var
- s: AnsiString;
- begin
- Result := 0;
- s := RecvBufferStr(4, Timeout);
- if FLastError = 0 then
- Result := (ord(s[1]) + ord(s[2]) * 256) + (ord(s[3]) + ord(s[4]) * 256) * 65536;
- end;
- function TBlockSerial.RecvBlock(Timeout: Integer): AnsiString;
- var
- x: integer;
- begin
- Result := '';
- x := RecvInteger(Timeout);
- if FLastError = 0 then
- Result := RecvBufferStr(x, Timeout);
- end;
- procedure TBlockSerial.RecvStreamRaw(const Stream: TStream; Timeout: Integer);
- var
- s: AnsiString;
- begin
- repeat
- s := RecvPacket(Timeout);
- if FLastError = 0 then
- WriteStrToStream(Stream, s);
- until FLastError <> 0;
- end;
- procedure TBlockSerial.RecvStreamSize(const Stream: TStream; Timeout: Integer; Size: Integer);
- var
- s: AnsiString;
- n: integer;
- begin
- for n := 1 to (Size div cSerialChunk) do
- begin
- s := RecvBufferStr(cSerialChunk, Timeout);
- if FLastError <> 0 then
- Exit;
- Stream.Write(PAnsichar(s)^, cSerialChunk);
- end;
- n := Size mod cSerialChunk;
- if n > 0 then
- begin
- s := RecvBufferStr(n, Timeout);
- if FLastError <> 0 then
- Exit;
- Stream.Write(PAnsichar(s)^, n);
- end;
- end;
- procedure TBlockSerial.RecvStreamIndy(const Stream: TStream; Timeout: Integer);
- var
- x: integer;
- begin
- x := RecvInteger(Timeout);
- x := SwapBytes(x);
- if FLastError = 0 then
- RecvStreamSize(Stream, Timeout, x);
- end;
- procedure TBlockSerial.RecvStream(const Stream: TStream; Timeout: Integer);
- var
- x: integer;
- begin
- x := RecvInteger(Timeout);
- if FLastError = 0 then
- RecvStreamSize(Stream, Timeout, x);
- end;
- {$IFNDEF MSWINDOWS}
- function TBlockSerial.WaitingData: integer;
- begin
- {$IFNDEF FPC}
- serialcheck(ioctl(FHandle, FIONREAD, @result));
- {$ELSE}
- serialcheck(fpIoctl(FHandle, FIONREAD, @result));
- {$ENDIF}
- if FLastError <> 0 then
- Result := 0;
- ExceptCheck;
- end;
- {$ELSE}
- function TBlockSerial.WaitingData: integer;
- var
- stat: TComStat;
- err: DWORD;
- begin
- if ClearCommError(FHandle, err, @stat) then
- begin
- SetSynaError(sOK);
- Result := stat.cbInQue;
- end
- else
- begin
- SerialCheck(sErr);
- Result := 0;
- end;
- ExceptCheck;
- end;
- {$ENDIF}
- function TBlockSerial.WaitingDataEx: integer;
- begin
- if FBuffer <> '' then
- Result := Length(FBuffer)
- else
- Result := Waitingdata;
- end;
- {$IFNDEF MSWINDOWS}
- function TBlockSerial.SendingData: integer;
- begin
- SetSynaError(sOK);
- Result := 0;
- end;
- {$ELSE}
- function TBlockSerial.SendingData: integer;
- var
- stat: TComStat;
- err: DWORD;
- begin
- SetSynaError(sOK);
- if not ClearCommError(FHandle, err, @stat) then
- serialcheck(sErr);
- ExceptCheck;
- result := stat.cbOutQue;
- end;
- {$ENDIF}
- {$IFNDEF MSWINDOWS}
- procedure TBlockSerial.DcbToTermios(const dcb: TDCB; var term: termios);
- var
- n: integer;
- x: cardinal;
- begin
-
- cfmakeraw(term);
- term.c_cflag := term.c_cflag or CREAD;
- term.c_cflag := term.c_cflag or CLOCAL;
- term.c_cflag := term.c_cflag or HUPCL;
-
- if (dcb.flags and dcb_RtsControlHandshake) > 0 then
- term.c_cflag := term.c_cflag or CRTSCTS
- else
- term.c_cflag := term.c_cflag and (not CRTSCTS);
-
- if (dcb.flags and dcb_OutX) > 0 then
- term.c_iflag := term.c_iflag or IXON or IXOFF or IXANY
- else
- term.c_iflag := term.c_iflag and (not (IXON or IXOFF or IXANY));
-
- term.c_cflag := term.c_cflag and (not CSIZE);
- case dcb.bytesize of
- 5:
- term.c_cflag := term.c_cflag or CS5;
- 6:
- term.c_cflag := term.c_cflag or CS6;
- 7:
- {$IFDEF FPC}
- term.c_cflag := term.c_cflag or CS7;
- {$ELSE}
- term.c_cflag := term.c_cflag or CS7fix;
- {$ENDIF}
- 8:
- term.c_cflag := term.c_cflag or CS8;
- end;
-
- if (dcb.flags and dcb_ParityCheck) > 0 then
- term.c_cflag := term.c_cflag or PARENB
- else
- term.c_cflag := term.c_cflag and (not PARENB);
- case dcb.parity of
- 1:
- term.c_cflag := term.c_cflag or PARODD;
- 2:
- term.c_cflag := term.c_cflag and (not PARODD);
- end;
-
- if dcb.stopbits > 0 then
- term.c_cflag := term.c_cflag or CSTOPB
- else
- term.c_cflag := term.c_cflag and (not CSTOPB);
-
- x := 0;
- for n := 0 to Maxrates do
- if rates[n, 0] = dcb.BaudRate then
- begin
- x := rates[n, 1];
- break;
- end;
- cfsetospeed(term, x);
- cfsetispeed(term, x);
- end;
- procedure TBlockSerial.TermiosToDcb(const term: termios; var dcb: TDCB);
- var
- n: integer;
- x: cardinal;
- begin
-
- dcb.baudrate := 0;
- {$IFDEF FPC}
-
- x := term.c_oflag and $0F;
- {$ELSE}
- x := cfgetospeed(term);
- {$ENDIF}
- for n := 0 to Maxrates do
- if rates[n, 1] = x then
- begin
- dcb.baudrate := rates[n, 0];
- break;
- end;
-
- if (term.c_cflag and CRTSCTS) > 0 then
- dcb.flags := dcb.flags or dcb_RtsControlHandshake or dcb_OutxCtsFlow
- else
- dcb.flags := dcb.flags and (not (dcb_RtsControlHandshake or dcb_OutxCtsFlow));
-
- if (term.c_cflag and IXOFF) > 0 then
- dcb.flags := dcb.flags or dcb_OutX or dcb_InX
- else
- dcb.flags := dcb.flags and (not (dcb_OutX or dcb_InX));
-
- case term.c_cflag and CSIZE of
- CS5:
- dcb.bytesize := 5;
- CS6:
- dcb.bytesize := 6;
- CS7fix:
- dcb.bytesize := 7;
- CS8:
- dcb.bytesize := 8;
- end;
-
- if (term.c_cflag and PARENB) > 0 then
- dcb.flags := dcb.flags or dcb_ParityCheck
- else
- dcb.flags := dcb.flags and (not dcb_ParityCheck);
- dcb.parity := 0;
- if (term.c_cflag and PARODD) > 0 then
- dcb.parity := 1
- else
- dcb.parity := 2;
-
- if (term.c_cflag and CSTOPB) > 0 then
- dcb.stopbits := 2
- else
- dcb.stopbits := 0;
- end;
- {$ENDIF}
- {$IFNDEF MSWINDOWS}
- procedure TBlockSerial.SetCommState;
- begin
- DcbToTermios(dcb, termiosstruc);
- SerialCheck(tcsetattr(FHandle, TCSANOW, termiosstruc));
- ExceptCheck;
- end;
- {$ELSE}
- procedure TBlockSerial.SetCommState;
- begin
- SetSynaError(sOK);
- if not windows.SetCommState(Fhandle, dcb) then
- SerialCheck(sErr);
- ExceptCheck;
- end;
- {$ENDIF}
- {$IFNDEF MSWINDOWS}
- procedure TBlockSerial.GetCommState;
- begin
- SerialCheck(tcgetattr(FHandle, termiosstruc));
- ExceptCheck;
- TermiostoDCB(termiosstruc, dcb);
- end;
- {$ELSE}
- procedure TBlockSerial.GetCommState;
- begin
- SetSynaError(sOK);
- if not windows.GetCommState(Fhandle, dcb) then
- SerialCheck(sErr);
- ExceptCheck;
- end;
- {$ENDIF}
- procedure TBlockSerial.SetSizeRecvBuffer(size: integer);
- begin
- {$IFDEF MSWINDOWS}
- SetupComm(Fhandle, size, 0);
- GetCommState;
- dcb.XonLim := size div 4;
- dcb.XoffLim := size div 4;
- SetCommState;
- {$ENDIF}
- FRecvBuffer := size;
- end;
- function TBlockSerial.GetDSR: Boolean;
- begin
- ModemStatus;
- {$IFNDEF MSWINDOWS}
- Result := (FModemWord and TIOCM_DSR) > 0;
- {$ELSE}
- Result := (FModemWord and MS_DSR_ON) > 0;
- {$ENDIF}
- end;
- procedure TBlockSerial.SetDTRF(Value: Boolean);
- begin
- {$IFNDEF MSWINDOWS}
- ModemStatus;
- if Value then
- FModemWord := FModemWord or TIOCM_DTR
- else
- FModemWord := FModemWord and not TIOCM_DTR;
- {$IFNDEF FPC}
- ioctl(FHandle, TIOCMSET, @FModemWord);
- {$ELSE}
- fpioctl(FHandle, TIOCMSET, @FModemWord);
- {$ENDIF}
- {$ELSE}
- if Value then
- EscapeCommFunction(FHandle, SETDTR)
- else
- EscapeCommFunction(FHandle, CLRDTR);
- {$ENDIF}
- end;
- function TBlockSerial.GetCTS: Boolean;
- begin
- ModemStatus;
- {$IFNDEF MSWINDOWS}
- Result := (FModemWord and TIOCM_CTS) > 0;
- {$ELSE}
- Result := (FModemWord and MS_CTS_ON) > 0;
- {$ENDIF}
- end;
- procedure TBlockSerial.SetRTSF(Value: Boolean);
- begin
- {$IFNDEF MSWINDOWS}
- ModemStatus;
- if Value then
- FModemWord := FModemWord or TIOCM_RTS
- else
- FModemWord := FModemWord and not TIOCM_RTS;
- {$IFNDEF FPC}
- ioctl(FHandle, TIOCMSET, @FModemWord);
- {$ELSE}
- fpioctl(FHandle, TIOCMSET, @FModemWord);
- {$ENDIF}
- {$ELSE}
- if Value then
- EscapeCommFunction(FHandle, SETRTS)
- else
- EscapeCommFunction(FHandle, CLRRTS);
- {$ENDIF}
- end;
- function TBlockSerial.GetCarrier: Boolean;
- begin
- ModemStatus;
- {$IFNDEF MSWINDOWS}
- Result := (FModemWord and TIOCM_CAR) > 0;
- {$ELSE}
- Result := (FModemWord and MS_RLSD_ON) > 0;
- {$ENDIF}
- end;
- function TBlockSerial.GetRing: Boolean;
- begin
- ModemStatus;
- {$IFNDEF MSWINDOWS}
- Result := (FModemWord and TIOCM_RNG) > 0;
- {$ELSE}
- Result := (FModemWord and MS_RING_ON) > 0;
- {$ENDIF}
- end;
- {$IFDEF MSWINDOWS}
- function TBlockSerial.CanEvent(Event: dword; Timeout: integer): boolean;
- var
- ex: DWord;
- y: Integer;
- Overlapped: TOverlapped;
- begin
- FillChar(Overlapped, Sizeof(Overlapped), 0);
- Overlapped.hEvent := CreateEvent(nil, True, False, nil);
- try
- SetCommMask(FHandle, Event);
- SetSynaError(sOK);
- if (Event = EV_RXCHAR) and (Waitingdata > 0) then
- Result := True
- else
- begin
- y := 0;
- if not WaitCommEvent(FHandle, ex, @Overlapped) then
- y := GetLastError;
- if y = ERROR_IO_PENDING then
- begin
-
- WaitForSingleObject(Overlapped.hEvent, Timeout);
- SetCommMask(FHandle, 0);
- GetOverlappedResult(FHandle, Overlapped, DWord(y), True);
- end;
- Result := (ex and Event) = Event;
- end;
- finally
- SetCommMask(FHandle, 0);
- CloseHandle(Overlapped.hEvent);
- end;
- end;
- {$ENDIF}
- {$IFNDEF MSWINDOWS}
- function TBlockSerial.CanRead(Timeout: integer): boolean;
- var
- FDSet: TFDSet;
- TimeVal: PTimeVal;
- TimeV: TTimeVal;
- x: Integer;
- begin
- TimeV.tv_usec := (Timeout mod 1000) * 1000;
- TimeV.tv_sec := Timeout div 1000;
- TimeVal := @TimeV;
- if Timeout = -1 then
- TimeVal := nil;
- {$IFNDEF FPC}
- FD_ZERO(FDSet);
- FD_SET(FHandle, FDSet);
- x := Select(FHandle + 1, @FDSet, nil, nil, TimeVal);
- {$ELSE}
- fpFD_ZERO(FDSet);
- fpFD_SET(FHandle, FDSet);
- x := fpSelect(FHandle + 1, @FDSet, nil, nil, TimeVal);
- {$ENDIF}
- SerialCheck(x);
- if FLastError <> sOK then
- x := 0;
- Result := x > 0;
- ExceptCheck;
- if Result then
- DoStatus(HR_CanRead, '');
- end;
- {$ELSE}
- function TBlockSerial.CanRead(Timeout: integer): boolean;
- begin
- Result := WaitingData > 0;
- if not Result then
- Result := CanEvent(EV_RXCHAR, Timeout) or (WaitingData > 0);
-
- if Result then
- DoStatus(HR_CanRead, '');
- end;
- {$ENDIF}
- {$IFNDEF MSWINDOWS}
- function TBlockSerial.CanWrite(Timeout: integer): boolean;
- var
- FDSet: TFDSet;
- TimeVal: PTimeVal;
- TimeV: TTimeVal;
- x: Integer;
- begin
- TimeV.tv_usec := (Timeout mod 1000) * 1000;
- TimeV.tv_sec := Timeout div 1000;
- TimeVal := @TimeV;
- if Timeout = -1 then
- TimeVal := nil;
- {$IFNDEF FPC}
- FD_ZERO(FDSet);
- FD_SET(FHandle, FDSet);
- x := Select(FHandle + 1, nil, @FDSet, nil, TimeVal);
- {$ELSE}
- fpFD_ZERO(FDSet);
- fpFD_SET(FHandle, FDSet);
- x := fpSelect(FHandle + 1, nil, @FDSet, nil, TimeVal);
- {$ENDIF}
- SerialCheck(x);
- if FLastError <> sOK then
- x := 0;
- Result := x > 0;
- ExceptCheck;
- if Result then
- DoStatus(HR_CanWrite, '');
- end;
- {$ELSE}
- function TBlockSerial.CanWrite(Timeout: integer): boolean;
- var
- t: LongWord;
- begin
- Result := SendingData = 0;
- if not Result then
- Result := CanEvent(EV_TXEMPTY, Timeout);
- if Result and (Win32Platform <> VER_PLATFORM_WIN32_NT) then
- begin
- t := GetTick;
- while not ReadTxEmpty(FPortAddr) do
- begin
- if TickDelta(t, GetTick) > 255 then
- Break;
- Sleep(0);
- end;
- end;
- if Result then
- DoStatus(HR_CanWrite, '');
- end;
- {$ENDIF}
- function TBlockSerial.CanReadEx(Timeout: integer): boolean;
- begin
- if Fbuffer <> '' then
- Result := True
- else
- Result := CanRead(Timeout);
- end;
- procedure TBlockSerial.EnableRTSToggle(Value: boolean);
- begin
- SetSynaError(sOK);
- {$IFNDEF MSWINDOWS}
- FRTSToggle := Value;
- if Value then
- RTS:=False;
- {$ELSE}
- if Win32Platform = VER_PLATFORM_WIN32_NT then
- begin
- GetCommState;
- if value then
- dcb.Flags := dcb.Flags or dcb_RtsControlToggle
- else
- dcb.flags := dcb.flags and (not dcb_RtsControlToggle);
- SetCommState;
- end
- else
- begin
- FRTSToggle := Value;
- if Value then
- RTS:=False;
- end;
- {$ENDIF}
- end;
- procedure TBlockSerial.Flush;
- begin
- {$IFNDEF MSWINDOWS}
- SerialCheck(tcdrain(FHandle));
- {$ELSE}
- SetSynaError(sOK);
- if not Flushfilebuffers(FHandle) then
- SerialCheck(sErr);
- {$ENDIF}
- ExceptCheck;
- end;
- {$IFNDEF MSWINDOWS}
- procedure TBlockSerial.Purge;
- begin
- {$IFNDEF FPC}
- SerialCheck(ioctl(FHandle, TCFLSH, TCIOFLUSH));
- {$ELSE}
- {$IFDEF DARWIN}
- SerialCheck(fpioctl(FHandle, TCIOflush, TCIOFLUSH));
- {$ELSE}
- SerialCheck(fpioctl(FHandle, TCFLSH, Pointer(PtrInt(TCIOFLUSH))));
- {$ENDIF}
- {$ENDIF}
- FBuffer := '';
- ExceptCheck;
- end;
- {$ELSE}
- procedure TBlockSerial.Purge;
- var
- x: integer;
- begin
- SetSynaError(sOK);
- x := PURGE_TXABORT or PURGE_TXCLEAR or PURGE_RXABORT or PURGE_RXCLEAR;
- if not PurgeComm(FHandle, x) then
- SerialCheck(sErr);
- FBuffer := '';
- ExceptCheck;
- end;
- {$ENDIF}
- function TBlockSerial.ModemStatus: integer;
- begin
- Result := 0;
- {$IFNDEF MSWINDOWS}
- {$IFNDEF FPC}
- SerialCheck(ioctl(FHandle, TIOCMGET, @Result));
- {$ELSE}
- SerialCheck(fpioctl(FHandle, TIOCMGET, @Result));
- {$ENDIF}
- {$ELSE}
- SetSynaError(sOK);
- if not GetCommModemStatus(FHandle, dword(Result)) then
- SerialCheck(sErr);
- {$ENDIF}
- ExceptCheck;
- FModemWord := Result;
- end;
- procedure TBlockSerial.SetBreak(Duration: integer);
- begin
- {$IFNDEF MSWINDOWS}
- SerialCheck(tcsendbreak(FHandle, Duration));
- {$ELSE}
- SetCommBreak(FHandle);
- Sleep(Duration);
- SetSynaError(sOK);
- if not ClearCommBreak(FHandle) then
- SerialCheck(sErr);
- {$ENDIF}
- end;
- {$IFDEF MSWINDOWS}
- procedure TBlockSerial.DecodeCommError(Error: DWord);
- begin
- if (Error and DWord(CE_FRAME)) > 1 then
- FLastError := ErrFrame;
- if (Error and DWord(CE_OVERRUN)) > 1 then
- FLastError := ErrOverrun;
- if (Error and DWord(CE_RXOVER)) > 1 then
- FLastError := ErrRxOver;
- if (Error and DWord(CE_RXPARITY)) > 1 then
- FLastError := ErrRxParity;
- if (Error and DWord(CE_TXFULL)) > 1 then
- FLastError := ErrTxFull;
- end;
- {$ENDIF}
- function TBlockSerial.PreTestFailing: Boolean;
- begin
- if not FInstanceActive then
- begin
- RaiseSynaError(ErrPortNotOpen);
- result:= true;
- Exit;
- end;
- Result := not TestCtrlLine;
- if result then
- RaiseSynaError(ErrNoDeviceAnswer)
- end;
- function TBlockSerial.TestCtrlLine: Boolean;
- begin
- result := ((not FTestDSR) or DSR) and ((not FTestCTS) or CTS);
- end;
- function TBlockSerial.ATCommand(value: AnsiString): AnsiString;
- var
- s: AnsiString;
- ConvSave: Boolean;
- begin
- result := '';
- FAtResult := False;
- ConvSave := FConvertLineEnd;
- try
- FConvertLineEnd := True;
- SendString(value + #$0D);
- repeat
- s := RecvString(FAtTimeout);
- if s <> Value then
- result := result + s + CRLF;
- if s = 'OK' then
- begin
- FAtResult := True;
- break;
- end;
- if s = 'ERROR' then
- break;
- until FLastError <> sOK;
- finally
- FConvertLineEnd := Convsave;
- end;
- end;
- function TBlockSerial.ATConnect(value: AnsiString): AnsiString;
- var
- s: AnsiString;
- ConvSave: Boolean;
- begin
- result := '';
- FAtResult := False;
- ConvSave := FConvertLineEnd;
- try
- FConvertLineEnd := True;
- SendString(value + #$0D);
- repeat
- s := RecvString(90 * FAtTimeout);
- if s <> Value then
- result := result + s + CRLF;
- if s = 'NO CARRIER' then
- break;
- if s = 'ERROR' then
- break;
- if s = 'BUSY' then
- break;
- if s = 'NO DIALTONE' then
- break;
- if Pos('CONNECT', s) = 1 then
- begin
- FAtResult := True;
- break;
- end;
- until FLastError <> sOK;
- finally
- FConvertLineEnd := Convsave;
- end;
- end;
- function TBlockSerial.SerialCheck(SerialResult: integer): integer;
- begin
- if SerialResult = integer(INVALID_HANDLE_VALUE) then
- {$IFDEF MSWINDOWS}
- result := GetLastError
- {$ELSE}
- {$IFNDEF FPC}
- result := GetLastError
- {$ELSE}
- result := fpGetErrno
- {$ENDIF}
- {$ENDIF}
- else
- result := sOK;
- FLastError := result;
- FLastErrorDesc := GetErrorDesc(FLastError);
- end;
- procedure TBlockSerial.ExceptCheck;
- var
- e: ESynaSerError;
- s: string;
- begin
- if FRaiseExcept and (FLastError <> sOK) then
- begin
- s := GetErrorDesc(FLastError);
- e := ESynaSerError.CreateFmt('Communication error %d: %s', [FLastError, s]);
- e.ErrorCode := FLastError;
- e.ErrorMessage := s;
- raise e;
- end;
- end;
- procedure TBlockSerial.SetSynaError(ErrNumber: integer);
- begin
- FLastError := ErrNumber;
- FLastErrorDesc := GetErrorDesc(FLastError);
- end;
- procedure TBlockSerial.RaiseSynaError(ErrNumber: integer);
- begin
- SetSynaError(ErrNumber);
- ExceptCheck;
- end;
- procedure TBlockSerial.DoStatus(Reason: THookSerialReason; const Value: string);
- begin
- if assigned(OnStatus) then
- OnStatus(Self, Reason, Value);
- end;
- class function TBlockSerial.GetErrorDesc(ErrorCode: integer): string;
- begin
- Result:= '';
- case ErrorCode of
- sOK: Result := 'OK';
- ErrAlreadyOwned: Result := 'Port owned by other process';
- ErrAlreadyInUse: Result := 'Instance already in use';
- ErrWrongParameter: Result := 'Wrong parameter at call';
- ErrPortNotOpen: Result := 'Instance not yet connected';
- ErrNoDeviceAnswer: Result := 'No device answer detected';
- ErrMaxBuffer: Result := 'Maximal buffer length exceeded';
- ErrTimeout: Result := 'Timeout during operation';
- ErrNotRead: Result := 'Reading of data failed';
- ErrFrame: Result := 'Receive framing error';
- ErrOverrun: Result := 'Receive Overrun Error';
- ErrRxOver: Result := 'Receive Queue overflow';
- ErrRxParity: Result := 'Receive Parity Error';
- ErrTxFull: Result := 'Tranceive Queue is full';
- end;
- if Result = '' then
- begin
- Result := SysErrorMessage(ErrorCode);
- end;
- end;
- {$IFDEF UNIX}
- function TBlockSerial.LockfileName: String;
- var
- s: string;
- begin
- s := SeparateRight(FDevice, '/dev/');
- result := LockfileDirectory + '/LCK..' + s;
- end;
- procedure TBlockSerial.CreateLockfile(PidNr: integer);
- var
- f: TextFile;
- s: string;
- begin
-
- s := IntToStr(PidNr);
- while length(s) < 10 do
- s := ' ' + s;
-
- try
- AssignFile(f, LockfileName);
- try
- Rewrite(f);
- writeln(f, s);
- finally
- CloseFile(f);
- end;
-
- s := 'chmod a+rw ' + LockfileName;
- {$IFNDEF FPC}
- FileSetReadOnly( LockfileName, False ) ;
-
- {$ELSE}
- fpSystem(s);
- {$ENDIF}
- except
-
- on Exception do
- ;
- end;
- end;
- function TBlockSerial.ReadLockfile: integer;
- var
- f: TextFile;
- s: string;
- begin
- AssignFile(f, LockfileName);
- Reset(f);
- try
- readln(f, s);
- finally
- CloseFile(f);
- end;
- Result := StrToIntDef(s, -1)
- end;
- function TBlockSerial.cpomComportAccessible: boolean;
- var
- MyPid: integer;
- Filename: string;
- begin
- Filename := LockfileName;
- {$IFNDEF FPC}
- MyPid := Libc.getpid;
- {$ELSE}
- MyPid := fpGetPid;
- {$ENDIF}
-
- if not DirectoryExists(LockfileDirectory) then
- CreateDir(LockfileDirectory);
-
- if not FileExists (Filename) then
- begin
- CreateLockfile(MyPid);
- result := true;
- exit;
- end;
-
-
- {$IFNDEF FPC}
- if Libc.getsid(ReadLockfile) = -1 then
- begin
- DeleteFile(Filename);
- CreateLockfile(MyPid);
- result := true;
- exit;
- end;
- {$ENDIF}
- result := false
- end;
- procedure TBlockSerial.cpomReleaseComport;
- begin
- DeleteFile(LockfileName);
- end;
- {$ENDIF}
- {$IFDEF MSWINDOWS}
- function GetSerialPortNames: string;
- var
- reg: TRegistry;
- l, v: TStringList;
- n: integer;
- begin
- l := TStringList.Create;
- v := TStringList.Create;
- reg := TRegistry.Create;
- try
- {$IFNDEF VER100}
- {$IFNDEF VER120}
- reg.Access := KEY_READ;
- {$ENDIF}
- {$ENDIF}
- reg.RootKey := HKEY_LOCAL_MACHINE;
- reg.OpenKey('\HARDWARE\DEVICEMAP\SERIALCOMM', false);
- reg.GetValueNames(l);
- for n := 0 to l.Count - 1 do
- v.Add(reg.ReadString(l[n]));
- Result := v.CommaText;
- finally
- reg.Free;
- l.Free;
- v.Free;
- end;
- end;
- {$ENDIF}
- {$IFNDEF MSWINDOWS}
- function GetSerialPortNames: string;
- var
- Index: Integer;
- Data: string;
- TmpPorts: String;
- sr : TSearchRec;
- begin
- try
- TmpPorts := '';
- if FindFirst('/dev/ttyS*', $FFFFFFFF, sr) = 0 then
- begin
- repeat
- if (sr.Attr and $FFFFFFFF) = Sr.Attr then
- begin
- data := sr.Name;
- index := length(data);
- while (index > 1) and (data[index] <> '/') do
- index := index - 1;
- TmpPorts := TmpPorts + ' ' + copy(data, 1, index + 1);
- end;
- until FindNext(sr) <> 0;
- end;
- FindClose(sr);
- finally
- Result:=TmpPorts;
- end;
- end;
- {$ENDIF}
- end.
|