Serial Port Communications? 问 I want to build a simple electrical controller which receives input from a sensor through a comm port and either turns a power source(s) on or off based upon this signal. I want this controller to be software in nature. How do I communicate through the port and is it possible to discern changes in voltage. If not, what kind of signal must be input. 答 When you want to write and read only binary signals you can use the PRinter parallel port. For that purpose the Port command is useful. In the below an example of some D1 code used for bidirectional 2 wire bus communication (I2C). BaseAddress is $278, $378 or $3BC, depending on the LPT port used for communication. There is a 'but'. In D1 the port function was available but not documented. In D2 and D3 it seems to have disappeared entirely (Please somebody correct me if this is wrong). We are using the parallel printer port with attached a small interface card with some I/O buffering for control of RF modules. Could somebody indicate whether the Port function still exist or what the alternative could be ? regards, Hans Brekelmans PROCEDURE SetIICline(Terminal: IICterminalTypes; High: Boolean); Var Count : Word; CtrlAddress: word; Begin { set iic line } CtrlAddress:=BaseAddress+2; Case Terminal of SCL : if High then Port[CtrlAddress]:=$08 else Port[CtrlAddress]:=$00; SDA : if NOT High then Port[BaseAddress]:=$80 else Port[BaseAddress]:=$00; END; For Count := 1 to ClockDelay do; End; {SetIICline} FUNCTION GetIICline(Terminal: IICterminalTypes): Boolean; const SDA_IN=$80; { SDA: 25 pin #11, status, NOT BUSY, bit 7 } SCL_IN=$08; { SCL: 25 pin #15, status, NOT Error, bit 3 } var Count : Word; ReadAddress: word; Begin ReadAddress:=BaseAddress+1; CASE Terminal OF SCL: GetIICline:=((Port[ReadAddress] AND SCL_IN) = SCL_IN); SDA: GetIICline:=((Port[ReadAddress] AND SDA_IN) = SDA_IN); { read sda pin } END; For Count := 1 to ClockDelay do; End;
得到本机ip地址? How about using winsockets? This code is untested and ugly. program get_ip; uses winsock,sysutils; VAR ch : ARRAY[1..32] OF Char; i : Integer; WSData: TWSAData; MyHost: PHostEnt; begin IF WSAstartup(2,wsdata)<>0 THEN BEGIN Writeln('can't start Winsock: Error ',WSAGetLastError); Halt(2); END; try IF getHostName(@ch[1],32)<>0 THEN BEGIN Writeln('getHostName failed'); Halt(3); END; except Writeln('getHostName failed'); halt(3); end; MyHost:=GetHostByName(@ch[1]); IF MyHost=NIL THEN BEGIN Writeln(GetHostName('+StrPas(@ch[1])+') failed : Error '+IntToStr(WSAGetLastError)); Halt(4); END ELSE BEGIN Write('address '); FOR i:=1 TO 4 DO BEGIN Write(Ord(MyHost.h_addr^[i-1])); IF i<4 THEN write('.') ELSE writeln; END; END; end.