Переглянути джерело

IMplementing the new program writing protokoll

Wilfried Klaas 6 роки тому
батько
коміт
24f6927eb2
5 змінених файлів з 65 додано та 68 видалено
  1. 2 1
      Beispiele/SimpleServo.tps
  2. 1 1
      ugui.lfm
  3. 53 64
      ugui.pas
  4. 3 2
      uselectcom.lfm
  5. 6 0
      uselectcom.pas

+ 2 - 1
Beispiele/SimpleServo.tps

@@ -1,8 +1,9 @@
 #TPS:Willies SPS Arduino
 0x00,F,E,""
-0x01,2,7,""
+0x01,2,8,""
 0x02,F,D,""
 0x03,2,7,""
 0x04,3,4,""
 0x05,0,0,""
 0x06,0,0,""
+0x07,0,0,""

+ 1 - 1
ugui.lfm

@@ -51907,7 +51907,7 @@ object Form1: TForm1
   end
   object SdpoSerial1: TSdpoSerial
     Active = False
-    BaudRate = br_57600
+    BaudRate = br__9600
     DataBits = db8bits
     Parity = pNone
     FlowControl = fcNone

+ 53 - 64
ugui.pas

@@ -177,6 +177,7 @@ type
     stopit: boolean;
     activeFile: string;
     dirty: boolean;
+    function readString(var line: string): boolean;
     procedure saveSection(filename: string; key: string);
     procedure loadSection(filename: string; key: string);
 
@@ -749,63 +750,10 @@ end;
 
 function TForm1.serialUpload: string;
 var
-  hexFileSource: TStringList;
-  hexFile, tmp, line: string;
-  pos, x: integer;
+  hexFile: string;
 begin
-  pos := 0;
-  line := '';
   hexFile := MCSIO.CreateUniqueFile(MCSIO.GetTempDir, 'TPS', '.hex');
   makeHexFile(hexFile);
-
-{  hexFileSource := TStringList.Create;
-
-  for x := 1 to StringGrid1.RowCount - 1 do
-  begin
-    if (pos = 0) then
-    begin
-      line := line + ':';
-    end;
-    tmp := StringGrid1.Cells[1, x];
-    line := line + tmp;
-
-    tmp := StringGrid1.Cells[2, x];
-    line := line + tmp;
-
-    Inc(pos);
-    if (pos = 8) then
-    begin
-      hexFileSource.Add(line);
-      line := '';
-      pos := 0;
-    end
-    else
-    begin
-      line := line + ',';
-    end;
-  end;
-  if (pos = 0) then
-  begin
-    line := hexFileSource.Strings[hexFileSource.Count - 1];
-    hexFileSource.Delete(hexFileSource.Count - 1);
-    pos := 8;
-  end;
-  if (pos <= 7) then
-  begin
-    for x := pos to 7 do
-    begin
-      line := line + '00';
-      if (x < 7) then
-        line := line + ',';
-    end;
-  end;
-  line := line + '*';
-  hexFileSource.Add(line);
-
-  MCSIO.StrToFile(hexFile, hexFileSource.Text);
-
-  hexFileSource.Free;
-}
   Result := hexFile;
 end;
 
@@ -827,8 +775,11 @@ var
   KeyName, StringValue: string;
   Res: WideString;
   Lines: TStringList;
+  TimeOut: integer;
+  error: boolean;
 
 begin
+  error := False;
   line := GetSerialPortNames;
   //  if (line <> '') then
   begin
@@ -853,24 +804,62 @@ begin
       SdpoSerial1.Device := comService;
       SdpoSerial1.Active := True;
 
-      Lines := TStringList.Create;
-      Lines.LoadFromFile(hexFile);
-      for x := 0 to Lines.Count - 1 do
+      if (not readString(line)) then
+      begin
+        error := True;
+      end;
+      if (not error) then
+      begin
+        SdpoSerial1.WriteData('w');
+        if (readString(line)) then
+        begin
+          if (pos('ready', line) > 0) then
+          begin
+            Lines := TStringList.Create;
+            Lines.LoadFromFile(hexFile);
+            for x := 0 to Lines.Count - 1 do
+            begin
+              SdpoSerial1.WriteData(Lines.Strings[x]);
+              SdpoSerial1.WriteData(CRLF);
+              Sleep(250);
+            end;
+            SdpoSerial1.WriteData('e');
+          end
+          else
+          begin
+            error := True;
+          end;
+        end;
+      end;
+
+      if (error) then
       begin
-        SdpoSerial1.WriteData(Lines.Strings[x]);
-        SdpoSerial1.WriteData(CRLF);
+        Application.MessageBox('Arduino antwortet nicht. Evtl. Arduino nicht angeschlossen oder falsche Firmware?',
+          'Keine Antwort',
+          MB_OK + MB_ICONEXCLAMATION);
       end;
       SdpoSerial1.Active := False;
       DeleteFile(hexFile);
     end;
   end;
-{  else
+end;
+
+function TForm1.readString(var line: string): boolean;
+var
+  TimeOut: integer;
+begin
+  Result := False;
+  TimeOut := 10;
+  while ((not SdpoSerial1.DataAvailable) and (TimeOut > 0)) do
+  begin
+    Dec(TimeOut);
+    Sleep(1000);
+  end;
+  if (Timeout > 0) then
   begin
-    Application.MessageBox('Kein Comport vorhanden. EVt. Arduino nicht angeschlossen?',
-      'Kein Comport',
-      MB_OK + MB_ICONEXCLAMATION);
+    line := SdpoSerial1.ReadData;
+    Result := True;
   end;
-  }
 end;
 
 procedure TForm1.setDirty(Value: boolean);

+ 3 - 2
uselectcom.lfm

@@ -1,7 +1,7 @@
 object frmSelectCom: TfrmSelectCom
-  Left = 344
+  Left = 375
   Height = 172
-  Top = 152
+  Top = 161
   Width = 401
   Caption = 'Schnittstelle auswählen'
   ClientHeight = 172
@@ -24,6 +24,7 @@ object frmSelectCom: TfrmSelectCom
     CancelButton.Name = 'CancelButton'
     CancelButton.DefaultCaption = True
     TabOrder = 0
+    OnClick = ButtonPanel1Click
     ShowButtons = [pbOK, pbCancel]
   end
   object Label1: TLabel

+ 6 - 0
uselectcom.pas

@@ -17,6 +17,7 @@ type
     cbServices: TComboBox;
     Label1: TLabel;
     Label2: TLabel;
+    procedure ButtonPanel1Click(Sender: TObject);
     procedure FormCreate(Sender: TObject);
     procedure FormShow(Sender: TObject);
   private
@@ -40,6 +41,11 @@ begin
   MCSLSU.MakeForm('frmSelectCom', 'ID_', frmSelectCom );
 end;
 
+procedure TfrmSelectCom.ButtonPanel1Click(Sender: TObject);
+begin
+
+end;
+
 procedure TfrmSelectCom.FormShow(Sender: TObject);
 begin
   MCSLSU.MakeForm('frmSelectCom', 'ID_', frmSelectCom );