mirror of https://github.com/FOME-Tech/openblt.git
Refs #247. Implemented exception catching and handling for low-level COM-port access in MicroBoot's UART communication interface DLL.
git-svn-id: https://svn.code.sf.net/p/openblt/code/trunk@206 5dc33758-31d5-4daf-9ae8-b24bf3d40d73
This commit is contained in:
parent
cbf012dedc
commit
66388e9e34
|
@ -79,10 +79,14 @@ begin
|
||||||
// call inherited constructor
|
// call inherited constructor
|
||||||
inherited Create;
|
inherited Create;
|
||||||
|
|
||||||
|
// reset packet length
|
||||||
|
packetLen := 0;
|
||||||
|
|
||||||
// create a sci driver instance
|
// create a sci driver instance
|
||||||
sciDriver := TComPort.Create(nil);
|
sciDriver := TComPort.Create(nil);
|
||||||
|
|
||||||
// init sci settings
|
// init sci settings
|
||||||
|
try
|
||||||
sciDriver.DataBits := dbEight;
|
sciDriver.DataBits := dbEight;
|
||||||
sciDriver.StopBits := sbOneStopBit;
|
sciDriver.StopBits := sbOneStopBit;
|
||||||
sciDriver.Parity.Bits := prNone;
|
sciDriver.Parity.Bits := prNone;
|
||||||
|
@ -90,9 +94,9 @@ begin
|
||||||
sciDriver.FlowControl.XonXoffIn := false;
|
sciDriver.FlowControl.XonXoffIn := false;
|
||||||
sciDriver.FlowControl.ControlRTS := rtsDisable;
|
sciDriver.FlowControl.ControlRTS := rtsDisable;
|
||||||
sciDriver.FlowControl.ControlDTR := dtrEnable;
|
sciDriver.FlowControl.ControlDTR := dtrEnable;
|
||||||
|
except
|
||||||
// reset packet length
|
Exit;
|
||||||
packetLen := 0;
|
end;
|
||||||
end; //*** end of Create ***
|
end; //*** end of Create ***
|
||||||
|
|
||||||
|
|
||||||
|
@ -107,7 +111,6 @@ destructor TXcpTransport.Destroy;
|
||||||
begin
|
begin
|
||||||
// release sci driver instance
|
// release sci driver instance
|
||||||
sciDriver.Free;
|
sciDriver.Free;
|
||||||
|
|
||||||
// call inherited destructor
|
// call inherited destructor
|
||||||
inherited;
|
inherited;
|
||||||
end; //*** end of Destroy ***
|
end; //*** end of Destroy ***
|
||||||
|
@ -124,6 +127,7 @@ procedure TXcpTransport.Configure(iniFile : string);
|
||||||
var
|
var
|
||||||
settingsIni : TIniFile;
|
settingsIni : TIniFile;
|
||||||
configIndex : integer;
|
configIndex : integer;
|
||||||
|
baudrateValue: TBaudRate;
|
||||||
begin
|
begin
|
||||||
// read XCP configuration from INI
|
// read XCP configuration from INI
|
||||||
if FileExists(iniFile) then
|
if FileExists(iniFile) then
|
||||||
|
@ -131,30 +135,38 @@ begin
|
||||||
// create ini file object
|
// create ini file object
|
||||||
settingsIni := TIniFile.Create(iniFile);
|
settingsIni := TIniFile.Create(iniFile);
|
||||||
|
|
||||||
// configure baudrate
|
// read baudrate
|
||||||
configIndex := settingsIni.ReadInteger('sci', 'baudrate', 6);
|
configIndex := settingsIni.ReadInteger('sci', 'baudrate', 6);
|
||||||
sciDriver.BaudRate := br38400; // init to default value
|
// init to default baudrate value
|
||||||
|
baudrateValue := br38400;
|
||||||
case configIndex of
|
case configIndex of
|
||||||
0 : sciDriver.BaudRate := br1200;
|
0 : baudrateValue := br1200;
|
||||||
1 : sciDriver.BaudRate := br2400;
|
1 : baudrateValue := br2400;
|
||||||
2 : sciDriver.BaudRate := br4800;
|
2 : baudrateValue := br4800;
|
||||||
3 : sciDriver.BaudRate := br9600;
|
3 : baudrateValue := br9600;
|
||||||
4 : sciDriver.BaudRate := br14400;
|
4 : baudrateValue := br14400;
|
||||||
5 : sciDriver.BaudRate := br19200;
|
5 : baudrateValue := br19200;
|
||||||
6 : sciDriver.BaudRate := br38400;
|
6 : baudrateValue := br38400;
|
||||||
7 : sciDriver.BaudRate := br56000;
|
7 : baudrateValue := br56000;
|
||||||
8 : sciDriver.BaudRate := br57600;
|
8 : baudrateValue := br57600;
|
||||||
9 : sciDriver.BaudRate := br115200;
|
9 : baudrateValue := br115200;
|
||||||
10: sciDriver.BaudRate := br128000;
|
10: baudrateValue := br128000;
|
||||||
11: sciDriver.BaudRate := br256000;
|
11: baudrateValue := br256000;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// configure port
|
// read port
|
||||||
configIndex := settingsIni.ReadInteger('sci', 'port', 0);
|
configIndex := settingsIni.ReadInteger('sci', 'port', 0);
|
||||||
sciDriver.Port := Format( 'COM%d', [ord(configIndex + 1)] );
|
|
||||||
|
|
||||||
// release ini file object
|
// release ini file object
|
||||||
settingsIni.Free;
|
settingsIni.Free;
|
||||||
|
|
||||||
|
// set the port and the baudrate
|
||||||
|
try
|
||||||
|
sciDriver.Port := Format( 'COM%d', [ord(configIndex + 1)] );
|
||||||
|
sciDriver.BaudRate := baudrateValue;
|
||||||
|
except
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
end;
|
end;
|
||||||
end; //*** end of Configure ***
|
end; //*** end of Configure ***
|
||||||
|
|
||||||
|
@ -168,8 +180,12 @@ end; //*** end of Configure ***
|
||||||
//***************************************************************************************
|
//***************************************************************************************
|
||||||
function TXcpTransport.Connect : Boolean;
|
function TXcpTransport.Connect : Boolean;
|
||||||
begin
|
begin
|
||||||
|
try
|
||||||
sciDriver.Open;
|
sciDriver.Open;
|
||||||
result := sciDriver.Connected;
|
result := sciDriver.Connected;
|
||||||
|
except
|
||||||
|
result := False;
|
||||||
|
end;
|
||||||
end; //*** end of Connect ***
|
end; //*** end of Connect ***
|
||||||
|
|
||||||
|
|
||||||
|
@ -201,6 +217,7 @@ var
|
||||||
cnt : byte;
|
cnt : byte;
|
||||||
rxCnt : byte;
|
rxCnt : byte;
|
||||||
dwEnd : DWord;
|
dwEnd : DWord;
|
||||||
|
bytesRead : integer;
|
||||||
begin
|
begin
|
||||||
// init the return value
|
// init the return value
|
||||||
result := false;
|
result := false;
|
||||||
|
@ -224,8 +241,12 @@ begin
|
||||||
end;
|
end;
|
||||||
|
|
||||||
// configure transmit timeout. timeout = (MULTIPLIER) * number_of_bytes + CONSTANT
|
// configure transmit timeout. timeout = (MULTIPLIER) * number_of_bytes + CONSTANT
|
||||||
|
try
|
||||||
sciDriver.Timeouts.WriteTotalConstant := 0;
|
sciDriver.Timeouts.WriteTotalConstant := 0;
|
||||||
sciDriver.Timeouts.WriteTotalMultiplier := timeOutms div (packetLen+1);
|
sciDriver.Timeouts.WriteTotalMultiplier := timeOutms div (packetLen+1);
|
||||||
|
except
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
// submit the packet transmission request
|
// submit the packet transmission request
|
||||||
if sciDriver.Write(msgData[0], packetLen+1) <> (packetLen+1) then
|
if sciDriver.Write(msgData[0], packetLen+1) <> (packetLen+1) then
|
||||||
|
@ -238,14 +259,24 @@ begin
|
||||||
Application.ProcessMessages;
|
Application.ProcessMessages;
|
||||||
|
|
||||||
// confgure the reception timeout. timeout = (MULTIPLIER) * number_of_bytes + CONSTANT
|
// confgure the reception timeout. timeout = (MULTIPLIER) * number_of_bytes + CONSTANT
|
||||||
|
try
|
||||||
sciDriver.Timeouts.ReadTotalConstant := timeOutms;
|
sciDriver.Timeouts.ReadTotalConstant := timeOutms;
|
||||||
sciDriver.Timeouts.ReadTotalMultiplier := 0;
|
sciDriver.Timeouts.ReadTotalMultiplier := 0;
|
||||||
|
except
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
// compute timeout time for receiving the response
|
// compute timeout time for receiving the response
|
||||||
dwEnd := GetTickCount + timeOutms;
|
dwEnd := GetTickCount + timeOutms;
|
||||||
|
|
||||||
// receive the first byte which should hold the packet length
|
// receive the first byte which should hold the packet length
|
||||||
if sciDriver.Read(resLen, 1) = 1 then
|
try
|
||||||
|
bytesRead := sciDriver.Read(resLen, 1);
|
||||||
|
except
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
if bytesRead = 1 then
|
||||||
begin
|
begin
|
||||||
// init the number of received bytes to 0
|
// init the number of received bytes to 0
|
||||||
rxCnt := 0;
|
rxCnt := 0;
|
||||||
|
@ -256,14 +287,24 @@ begin
|
||||||
begin
|
begin
|
||||||
// re-confgure the reception timeout now that the total packet length is known.
|
// re-confgure the reception timeout now that the total packet length is known.
|
||||||
// timeout = (MULTIPLIER) * number_of_bytes + CONSTANT
|
// timeout = (MULTIPLIER) * number_of_bytes + CONSTANT
|
||||||
|
try
|
||||||
sciDriver.Timeouts.ReadTotalConstant := 0;
|
sciDriver.Timeouts.ReadTotalConstant := 0;
|
||||||
sciDriver.Timeouts.ReadTotalMultiplier := timeOutms div resLen;
|
sciDriver.Timeouts.ReadTotalMultiplier := timeOutms div resLen;
|
||||||
|
except
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
// attempt to receive the bytes of the response packet one by one
|
// attempt to receive the bytes of the response packet one by one
|
||||||
while (rxCnt < resLen) and (GetTickCount < dwEnd) do
|
while (rxCnt < resLen) and (GetTickCount < dwEnd) do
|
||||||
begin
|
begin
|
||||||
// receive the next byte
|
// receive the next byte
|
||||||
if sciDriver.Read(packetData[rxCnt], 1) = 1 then
|
try
|
||||||
|
bytesRead := sciDriver.Read(packetData[rxCnt], 1);
|
||||||
|
except
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
|
|
||||||
|
if bytesRead = 1 then
|
||||||
begin
|
begin
|
||||||
// increment counter
|
// increment counter
|
||||||
rxCnt := rxCnt + 1;
|
rxCnt := rxCnt + 1;
|
||||||
|
@ -291,7 +332,11 @@ end; //*** end of SendPacket ***
|
||||||
//***************************************************************************************
|
//***************************************************************************************
|
||||||
procedure TXcpTransport.Disconnect;
|
procedure TXcpTransport.Disconnect;
|
||||||
begin
|
begin
|
||||||
|
try
|
||||||
sciDriver.Close;
|
sciDriver.Close;
|
||||||
|
except
|
||||||
|
Exit;
|
||||||
|
end;
|
||||||
end; //*** end of Disconnect ***
|
end; //*** end of Disconnect ***
|
||||||
|
|
||||||
|
|
||||||
|
|
Binary file not shown.
Loading…
Reference in New Issue