Changeset 410
- Timestamp:
- Aug 15, 2012, 10:45:44 AM (12 years ago)
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/TemplateGenerics/Additional/UBinarySerializer.pas
r407 r410 19 19 FEndianness: TEndianness; 20 20 SwapData: Boolean; 21 procedure SetList(const AValue: TListByte); 21 22 procedure SetEndianness(const AValue: TEndianness); 22 23 procedure ReverseByteOrder(var Buffer; Count: Integer); … … 59 60 destructor Destroy; override; 60 61 property Endianness: TEndianness read FEndianness write SetEndianness; 61 property List: TListByte read FList write FList;62 property List: TListByte read FList write SetList; 62 63 property Grow: Boolean read FGrow write FGrow; 63 64 end; … … 236 237 end; 237 238 239 procedure TBinarySerializer.SetList(const AValue: TListByte); 240 begin 241 if OwnsList then FList.Free; 242 FList := AValue; 243 end; 244 238 245 procedure TBinarySerializer.SetEndianness(const AValue: TEndianness); 239 246 begin -
ISPProgrammer/Dallas/UDallasProgrammer.pas
r409 r410 6 6 7 7 uses 8 Classes, SysUtils, USerialPort, UCommSerialPort, UCommPin, 8 Classes, SysUtils, USerialPort, UCommSerialPort, UCommPin, UCommMark, 9 9 UJobProgressView, SyncObjs, DateUtils, Dialogs, URegistry, 10 10 Forms, UISPProgrammer, Registry, UBinarySerializer, SpecializedList; 11 11 12 12 const 13 NewLine = #$0D#$0A;13 Mark = #13#10; 14 14 15 15 type … … 22 22 FOnLogData: TOnLogDataEvent; 23 23 Pin: TCommPin; 24 Response: TBinarySerializer; // should be thread safe 24 CommMark: TCommMark; 25 ResponseQueue: TListObject; 25 26 ResponseLock: TCriticalSection; 26 27 ResponseTemp: TBinarySerializer; 27 ReceiveEvent: TEvent;28 28 SerialPortBackup: TCommSerialPort; 29 29 SerialPortBackupPin: TCommPin; 30 30 HexData: TStringList; 31 31 Request: TBinarySerializer; 32 StartTime: TDateTime; 33 WaitResult: TWaitResult; 32 Mark: TListByte; 34 33 procedure ReceiveData(Sender: TCommPin; Stream: TListByte); 35 function WaitForString(EndString: string; Timeout: TDateTime): TWaitResult; 34 function ReadResponse: string; 35 function ResponseCount: Integer; 36 36 procedure ResponseClear; 37 37 procedure CheckErrorCode(Value: string); … … 70 70 SInvalidResponse = 'Invalid response'; 71 71 SUnknownProgrammerResponse = 'Unknown flash programmer response "%s"'; 72 SIdentification = ' Identification';72 SIdentification = 'Device identification'; 73 73 74 74 { TDallasProgrammer } … … 77 77 var 78 78 OldPosition: Integer; 79 NewList: TListByte; 79 80 begin 80 81 try 81 82 ResponseLock.Acquire; 82 OldPosition := Response.Position; 83 Response.Position := Response.List.Count; 84 Response.WriteList(Stream, 0, Stream.Count); 85 Response.Position := OldPosition; 83 NewList := TListByte.Create; 84 NewList.Assign(Stream); 85 ResponseQueue.Add(NewList); 86 86 finally 87 87 ResponseLock.Release; 88 88 end; 89 ReceiveEvent.SetEvent;90 end; 91 92 function TDallasProgrammer.WaitForString(EndString: string; Timeout: TDateTime): TWaitResult; 93 var 89 end; 90 91 function TDallasProgrammer.ReadResponse: string; 92 var 93 Serializer: TBinarySerializer; 94 94 StartTime: TDateTime; 95 TimeoutPart: TDateTime;96 OldPosition: Integer;97 95 ElapsedTime: TDateTime; 98 96 begin 99 97 StartTime := Now; 100 98 repeat 99 if ResponseCount > 0 then Break; 101 100 ElapsedTime := Now - StartTime; 102 TimeoutPart := Timeout - ElapsedTime; 103 if TimeoutPart < 0 then TimeoutPart := 0; 104 Result := ReceiveEvent.WaitFor(Round(TimeOutPart / OneMillisecond)); 105 try 106 ResponseLock.Acquire; 107 OldPosition := Response.Position; 108 Response.Position := 0; 109 Response.ReadStringTerminated(EndString); 110 if (Response.Position = 0) then begin 111 Result := wrTimeout; 112 end; 113 Response.Position := OldPosition; 114 finally 115 ResponseLock.Release; 116 end; 117 until (Result = wrSignaled) or (ElapsedTime > Timeout); 101 until (ElapsedTime > Timeout); 102 if ElapsedTime > Timeout then 103 raise Exception.Create(STimeout); 104 try 105 ResponseLock.Acquire; 106 Serializer := TBinarySerializer.Create; 107 Serializer.List := TListByte(ResponseQueue.First); 108 Result := Serializer.ReadString(Serializer.List.Count); 109 ResponseQueue.Delete(0); 110 finally 111 Serializer.Free; 112 ResponseLock.Release; 113 end; 114 end; 115 116 function TDallasProgrammer.ResponseCount: Integer; 117 begin 118 try 119 ResponseLock.Acquire; 120 Result := ResponseQueue.Count; 121 finally 122 ResponseLock.Release; 123 end; 118 124 end; 119 125 … … 122 128 try 123 129 ResponseLock.Acquire; 124 Response.Clear; 125 ReceiveEvent.ResetEvent; 130 ResponseQueue.Clear; 126 131 finally 127 132 ResponseLock.Release; … … 148 153 SerialPort.BaudRate := BaudRate; 149 154 SerialPort.DTR := True; 150 SerialPort.Pin.Connect( Pin);155 SerialPort.Pin.Connect(CommMark.PinRaw); 151 156 SerialPort.Flush; 152 157 SerialPort.Purge; 153 ResponseClear;154 158 SerialPort.Active := True; 155 159 if Assigned(FOnLogData) then 156 160 Pin.OnLogData := FOnLogData; 161 ResponseClear; 162 CommMark.Active := True; 157 163 158 164 ReadIdentification; 159 165 end else begin 166 CommMark.Active := False; 160 167 SerialPort.Active := False; 161 168 SerialPort.Assign(SerialPortBackup); … … 215 222 ResponseClear; 216 223 Request.WriteByte(Ord('D')); 217 Request.WriteByte($0D); 218 Pin.Send(Request.List); 219 if WaitForString(NewLine, Timeout) <> wrSignaled then begin 220 raise Exception.Create(STimeout); 221 end; 222 Response.ReadStringTerminated(NewLine); // D 223 try 224 ResponseLock.Acquire; 225 Response.List.DeleteItems(0, Response.Position); 226 Response.Position := 0; 227 finally 228 ResponseLock.Release; 229 end; 224 Pin.Send(Request.List); 225 Value := ReadResponse; 226 ReadResponse; // Empty line 230 227 231 228 //HexFile.SaveToStringList(HexData); … … 235 232 I := 0; 236 233 repeat 237 //Request.WriteString(HexData[I]); 238 //Request.WriteByte($0D); 239 //Pin.Send(Request); 240 if WaitForString(NewLine, Timeout) <> wrSignaled then 241 raise Exception.Create(STimeout); 242 243 //if ReceiveEvent.WaitFor(Round(Timeout / OneMillisecond)) <> wrSignaled then 244 // raise Exception.Create(STimeout); 245 try 246 ResponseLock.Acquire; 247 //Response.Position := 0; 248 //if Response.Size = 0 then 249 // raise Exception.Create(SEmptyBuffer); 250 Value := Response.ReadStringTerminated(NewLine); 251 if Value <> '' then begin 252 Response.List.DeleteItems(0, Response.Position); 253 Response.Position := 0; 254 //Log(Value); 255 HexData.Add(Value); 256 //Response.Size := 0; 257 if Value = ':00000001FF' then Break; 258 Inc(I); 259 end; 260 finally 261 ResponseLock.Release; 234 Value := ReadResponse; 235 if Value <> '' then begin 236 //Log(Value); 237 HexData.Add(Value); 238 if Value = ':00000001FF' then Break; 239 Inc(I); 262 240 end; 263 241 Job.Progress.Value := I; … … 280 258 ResponseClear; 281 259 Request.WriteByte(Ord('V')); 282 Request.WriteByte($0D); 283 Pin.Send(Request.List); 284 if WaitForString(NewLine, Timeout) <> wrSignaled then begin 285 Pin.Send(Request.List); 286 raise Exception.Create(STimeout); 287 end; 288 HexFile.SaveToStringList(HexData); 289 Job.Progress.Max := HexData.Count; 290 for I := 0 to HexData.Count - 1 do begin 291 Request.List.Count := 0; 292 ResponseClear; 293 Request.WriteString(HexData[I]); 294 Request.WriteByte($0D); 295 Pin.Send(Request.List); 296 if ReceiveEvent.WaitFor(Round(Timeout / OneMillisecond)) <> wrSignaled then 297 raise Exception.Create(STimeout); 298 try 299 ResponseLock.Acquire; 300 Response.Position := 0; 301 if Response.List.Count = 0 then 302 raise Exception.Create(SEmptyBuffer); 303 Value := Chr(Response.ReadByte); 304 finally 305 ResponseLock.Release; 306 end; 307 CheckErrorCode(Value); 308 Job.Progress.Value := I; 309 if Job.Terminate then Break; 260 Pin.Send(Request.List); 261 ReadResponse; 262 263 try 264 CommMark.Mark.Clear; 265 HexFile.SaveToStringList(HexData); 266 Job.Progress.Max := HexData.Count; 267 for I := 0 to HexData.Count - 1 do begin 268 Request.Clear; 269 ResponseClear; 270 Request.WriteString(HexData[I]); 271 Request.WriteList(Mark, 0, Mark.Count); 272 Pin.Send(Request.List); 273 Value := ReadResponse; 274 CheckErrorCode(Value); 275 Job.Progress.Value := I; 276 if Job.Terminate then Break; 277 end; 278 finally 279 CommMark.Mark.Assign(Mark); 310 280 end; 311 281 end; … … 320 290 ResponseClear; 321 291 Request.WriteByte(Ord('L')); 322 Request.WriteByte($0D); 323 Pin.Send(Request.List); 324 if WaitForString(NewLine, Timeout) <> wrSignaled then begin 325 Pin.Send(Request.List); 326 raise Exception.Create(STimeout); 327 end; 328 HexFile.SaveToStringList(HexData); 329 Job.Progress.Max := HexData.Count; 330 for I := 0 to HexData.Count - 1 do begin 331 Request.List.Count := 0; 332 ResponseClear; 333 Request.WriteString(HexData[I]); 334 Request.WriteByte($0D); 335 Pin.Send(Request.List); 336 if ReceiveEvent.WaitFor(Round(Timeout / OneMillisecond)) <> wrSignaled then 337 raise Exception.Create(STimeout); 338 try 339 ResponseLock.Acquire; 340 Response.Position := 0; 341 if Response.List.Count = 0 then 342 raise Exception.Create(SEmptyBuffer); 343 Value := Chr(Response.ReadByte); 344 finally 345 ResponseLock.Release; 346 end; 347 CheckErrorCode(Value); 348 Job.Progress.Value := I; 349 if Job.Terminate then Break; 292 Pin.Send(Request.List); 293 Value := ReadResponse; 294 295 try 296 CommMark.Mark.Clear; 297 HexFile.SaveToStringList(HexData); 298 Job.Progress.Max := HexData.Count; 299 for I := 0 to HexData.Count - 1 do begin 300 Request.Clear; 301 ResponseClear; 302 Request.WriteString(HexData[I]); 303 Request.WriteList(Mark, 0, Mark.Count); 304 Pin.Send(Request.List); 305 Value := ReadResponse; 306 CheckErrorCode(Value); 307 Job.Progress.Value := I; 308 if Job.Terminate then Break; 309 end; 310 finally 311 CommMark.Mark.Assign(Mark); 350 312 end; 351 313 end; … … 357 319 ResponseClear; 358 320 Request.WriteByte(Ord('K')); 359 Request.WriteByte($0D); 360 Pin.Send(Request.List); 361 if WaitForString('>', Timeout) <> wrSignaled then 362 raise Exception.Create(STimeout); 321 Pin.Send(Request.List); 322 ReadResponse; 363 323 end; 364 324 … … 370 330 var 371 331 InitTimeout: TDateTime; 332 Value: string; 372 333 begin 373 334 Result := ''; 374 335 InitTimeout := 6000 * OneMillisecond; 375 376 Active := True; 377 378 // Init and read identification 379 StartTime := Now; 380 repeat 381 ResponseClear; 382 Request.List.Count := 0; 383 Request.WriteByte($0D); 384 Pin.Send(Request.List); 385 WaitResult := WaitForString('>', Timeout); 386 until (WaitResult = wrSignaled) or ((Now - StartTime) > InitTimeout); 387 if WaitResult <> wrSignaled then 388 raise Exception.Create(STimeout); 389 try 390 ResponseLock.Acquire; 391 Response.Position := 0; 392 Response.ReadStringTerminated(NewLine); 393 Identification := Response.ReadStringTerminated(NewLine); 394 Result := Identification; 395 Log(SIdentification + ': ' + Identification); 396 finally 397 ResponseLock.Release; 398 end; 336 Active := True; 337 338 ResponseClear; 339 Request.Clear; 340 Pin.Send(Request.List); 341 ReadResponse; // Empty line 342 Identification := ReadResponse; 343 Result := Identification; 344 Log(SIdentification + ': ' + Identification); 399 345 end; 400 346 … … 404 350 Capabilities := [ipcErase, ipcRead, ipcWrite, ipcReset]; 405 351 Timeout := 3000 * OneMillisecond; 406 ReceiveEvent := TSimpleEvent.Create; 407 Response := TBinarySerializer.Create; 408 Response.List := TListByte.Create; 409 Response.OwnsList := True; 352 ResponseQueue := TListObject.Create; 410 353 ResponseLock := TCriticalSection.Create; 411 354 ResponseTemp := TBinarySerializer.Create; … … 414 357 Pin := TCommPin.Create; 415 358 Pin.OnReceive := ReceiveData; 359 Mark := TListByte.Create; 360 Mark.SetArray([13, 10]); 361 CommMark := TCommMark.Create; 362 CommMark.Mark.Assign(Mark); 363 CommMark.PinFrame.Connect(Pin); 416 364 BaudRate := 9600; 417 365 SerialPortBackup := TCommSerialPort.Create; … … 421 369 begin 422 370 Active := False; 371 Mark.Free; 372 CommMark.Free; 423 373 SerialPortBackup.Free; 424 374 Pin.Free; 425 Response .Free;375 ResponseQueue.Free; 426 376 ResponseLock.Free; 427 377 ResponseTemp.Free; 428 ReceiveEvent.Free;429 378 inherited Destroy; 430 379 end; -
PinConnection/PinConnection.lpk
r407 r410 22 22 <License Value="GNU/GPL"/> 23 23 <Version Minor="3"/> 24 <Files Count="1 3">24 <Files Count="14"> 25 25 <Item1> 26 26 <Filename Value="UCommFrame.pas"/> … … 75 75 <UnitName Value="UCommTelnetComPortOption"/> 76 76 </Item13> 77 <Item14> 78 <Filename Value="UCommMark.pas"/> 79 <UnitName Value="UCommMark"/> 80 </Item14> 77 81 </Files> 78 82 <i18n> -
PinConnection/PinConnection.pas
r407 r410 10 10 UCommFrame, UCommHub, UCommPin, UCommSerialPort, UCommThread, UPacketBurst, 11 11 USerialPort, UCommConcentrator, UCommDelay, UCommTCPClient, UCommTCPServer, 12 UCommTelnet, UCommTelnetComPortOption, LazarusPackageIntf;12 UCommTelnet, UCommTelnetComPortOption, UCommMark, LazarusPackageIntf; 13 13 14 14 implementation
Note:
See TracChangeset
for help on using the changeset viewer.