|
|
@@ -115,7 +115,7 @@ type
|
|
|
FStatus_RxEvent, FStatus_TxEvent, FUART_RxEvent, FUART_TxEvent,
|
|
|
FUnpluggedEvent: TEvent;
|
|
|
|
|
|
- //FLastUsbError: integer;
|
|
|
+ FHidErrorStr: string;
|
|
|
|
|
|
FUART_RxBuffer, FUART_TxBuffer, FStatus_RxBuffer,
|
|
|
FStatus_TxBuffer: TSPSCRingBuffer;
|
|
|
@@ -151,6 +151,9 @@ type
|
|
|
|
|
|
implementation
|
|
|
|
|
|
+type
|
|
|
+ TUSBPacketBuffer = array[0..8] of byte;
|
|
|
+
|
|
|
{ TUSBasp }
|
|
|
|
|
|
constructor TUSBasp.Create;
|
|
|
@@ -176,8 +179,7 @@ begin
|
|
|
FUnpluggedEvent := TEvent.Create(nil, False, False, 'UNPLD_' +
|
|
|
IntToStr(Random(999)));
|
|
|
|
|
|
- if HidInit = 0 then
|
|
|
- HIDEnumerate;
|
|
|
+ HidInit();
|
|
|
|
|
|
end;
|
|
|
|
|
|
@@ -207,7 +209,15 @@ begin
|
|
|
if not FConnected then
|
|
|
begin
|
|
|
|
|
|
+ if not HIDEnumerate(ASerialNumber) then
|
|
|
+ Exit;
|
|
|
+
|
|
|
FUART_HIDDevice := THidDevice.OpenPath(FUART_HIDDevicePath);
|
|
|
+ FHidErrorStr := FUART_HIDDevice^.GetError;
|
|
|
+
|
|
|
+ if FHidErrorStr <> 'Success' then
|
|
|
+ Exit;
|
|
|
+
|
|
|
FUART_HIDDevice^.SetNonBlocking(0);
|
|
|
|
|
|
FUART_RxThread := TThread_HID_UART_Rx.Create(
|
|
|
@@ -218,12 +228,18 @@ begin
|
|
|
if FHasStatus then
|
|
|
begin
|
|
|
FStatus_HIDDevice := THidDevice.OpenPath(FStatus_HIDDevicePath);
|
|
|
- FStatus_HIDDevice^.SetNonBlocking(0);
|
|
|
+ FHidErrorStr := FStatus_HIDDevice^.GetError;
|
|
|
+
|
|
|
+ if FHidErrorStr = 'Success' then
|
|
|
+ begin
|
|
|
+ FStatus_HIDDevice^.SetNonBlocking(0);
|
|
|
|
|
|
- FStatus_RxThread := TThread_HID_Status_Rx.Create(FStatus_HIDDevice,
|
|
|
- FStatus_RxBuffer, FStatus_RxEvent, FUnpluggedEvent);
|
|
|
- FStatus_TxThread := TThread_HID_Status_Tx.Create(FStatus_HIDDevice,
|
|
|
- FStatus_TxBuffer, FStatus_TxEvent, FUnpluggedEvent);
|
|
|
+ FStatus_RxThread := TThread_HID_Status_Rx.Create(FStatus_HIDDevice,
|
|
|
+ FStatus_RxBuffer, FStatus_RxEvent, FUnpluggedEvent);
|
|
|
+
|
|
|
+ FStatus_TxThread := TThread_HID_Status_Tx.Create(FStatus_HIDDevice,
|
|
|
+ FStatus_TxBuffer, FStatus_TxEvent, FUnpluggedEvent);
|
|
|
+ end;
|
|
|
end;
|
|
|
|
|
|
FConnected := True;
|
|
|
@@ -273,7 +289,7 @@ end;
|
|
|
function TUSBasp.UARTOpen(const ABaudRate, ADataBits, AParity,
|
|
|
AStopBits: integer): boolean;
|
|
|
var
|
|
|
- Buffer: array[0..8] of byte = (0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
+ Buffer: TUSBPacketBuffer;
|
|
|
Prescaler: word;
|
|
|
begin
|
|
|
if FConnected and not FUARTOpened then
|
|
|
@@ -294,7 +310,7 @@ end;
|
|
|
|
|
|
function TUSBasp.UARTClose: boolean;
|
|
|
var
|
|
|
- Buffer: array[0..8] of byte = (0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
+ Buffer: TUSBPacketBuffer;
|
|
|
begin
|
|
|
if FConnected and FUARTOpened then
|
|
|
begin
|
|
|
@@ -316,7 +332,7 @@ function TUSBasp.HIDEnumerate(const ASerialNumber: string): boolean;
|
|
|
var
|
|
|
HidItemRoot, HidItem: PHidDeviceInfo;
|
|
|
HIDDeviceMatch: boolean;
|
|
|
- FeatureReportBuffer: array[0..8] of byte = (0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
+ FeatureReportBuffer: TUSBPacketBuffer;
|
|
|
begin
|
|
|
Result := False;
|
|
|
|
|
|
@@ -347,7 +363,6 @@ begin
|
|
|
2: begin
|
|
|
FStatus_HIDDevicePath := HidItem^.Path;
|
|
|
FHasStatus := True;
|
|
|
- Break;
|
|
|
end;
|
|
|
end;
|
|
|
|
|
|
@@ -391,7 +406,7 @@ end;
|
|
|
|
|
|
function TUSBasp.ChangeSerialNumber(const ASerialNumber: string): integer;
|
|
|
var
|
|
|
- Buffer: array[0..8] of byte = (0, 0, 0, 0, 0, 0, 0, 0, 0);
|
|
|
+ Buffer: TUSBPacketBuffer;
|
|
|
ErrorCode: integer;
|
|
|
SerNumValue: word;
|
|
|
begin
|