Autor Beitrag
Finn
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 43



BeitragVerfasst: So 27.02.05 13:32 
Hallo Delphi Freunde,

habe folgendes Problem:

Ich kann mit meinem Delphi-Programm entweder nur den rechnten Schrittmotor ansteuern oder gleich beide.

Befehl für rechten Motor:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button1Click(Sender: TObject);
begin
OpenCOM(1);
SetBaudRate(9600);
SendString(PChar('#rme'+chr(1)));
CloseCOM;
end;


Befehl für beide Motoren:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button2Click(Sender: TObject);
begin
OpenCOM(1);
SetBaudRate(9600);
SendString(PChar('#rme'+chr(2)));
CloseCOM;
end;


...die funktionieren aber der für den linken Motor

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure TForm1.Button3Click(Sender: TObject);
begin
OpenCOM(1);
SetBaudRate(9600);
SendString(PChar('#rme'+chr(0)));
CloseCOM;
end;


.. funktioniert nich.
Liegt das vielleicht and der Null. Mag Delphi die Null nicht ???

Den Quelltext zur Ansteuerung habe ich hier aus dem Forum kopiert (Danke für den Bericht).
Kann darin keinen Fehler sehen aber ich wäre euch dankbar wenn einer von euch den nochmal kurz durchschauen könnte.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

  // Benutzte GlobaleVariablen
  PortTimeout : _COMMTIMEOUTS;
  PortHandle : Integer;
  PortDCB : TDCB;
  PortNr  : Integer;
  PortState : Cardinal;
  WriteOverlapped,ReadOverlapped,StatusOs: TOverlapped;

implementation

// Testen ob Schnittstelle verfügbar ist
function ComAvailable(ComNr: byte): boolean; stdcall;
var
  TestHandle : integer;
begin
  TestHandle :=
  CreateFile(PChar('\\.\COM'+IntToStr(ComNr)),GENERIC_READ or GENERIC_WRITE,0,
             nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,LongInt(0));
  if (TestHandle <= 0then
    Result := false
  else
  begin
    Result := true;
    CloseHandle(TestHandle);
  end;
end;

// Ini Funktion für die Overlapped Strukturen
procedure InitOverlapped(var Overlapped : TOverlapped);
begin
  Overlapped.Offset := 0;
  Overlapped.OffsetHigh := 0;
  Overlapped.Internal := 0;
  Overlapped.InternalHigh := 0;
  Overlapped.hEvent := CreateEvent(nil,True,False,'');
end;

// Die Schnittstelle öffnen / schließen / testen
function OpenCOM(Port: byte): boolean; stdcall;
begin
  PortHandle :=
  CreateFile(PChar('\\.\COM'+IntToStr(Port)),GENERIC_READ or GENERIC_WRITE,0,
             nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,LongInt(0));

  if PortNr > 0 then
  begin                                     
    Result := true;
    InitOverlapped(WriteOverlapped);
    InitOverlapped(ReadOverlapped);
    InitOverlapped(StatusOs);
  end else Result := false;
end;

// Die Schnittstelle konfigurieren
function SetBaudRate(baud: cardinal): boolean; stdcall;
begin
  GetCommState(PortHandle,PortDCB);
  PortDCB.BaudRate := baud;
  Result := SetCommState(PortHandle,PortDCB);
end;

function SendString(Str: PChar): boolean; stdcall;
var
  written  : cardinal;
  tmpStr : string;
  i: LongBool;
begin
  if (PortHandle <> 0then
  begin
    tmpStr := string(Str);
    Result := not WriteFile(PortHandle,tmpStr[1],Length(tmpStr),written,@WriteOverlapped);
  end;
end;

// COM Schließen
procedure CloseCOM; stdcall;
begin
  PurgeComm(PortHandle, PURGE_RXABORT or PURGE_RXCLEAR or PURGE_TXABORT or PURGE_TXCLEAR);
  SetCommMask(PortHandle,0); //unterbricht WaitCommEvent im Polling thread
  CloseHandle(PortHandle);
  PortHandle := 0;
end;

{$R *.dfm}


Viele Grüße
Finn
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: So 27.02.05 13:37 
eine 0 ist wie ein abgeschlossener string
d.h. es wird nur
endString(PChar('#rme')) gesendet
da #0 das ende des strings zeigt
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 27.02.05 13:57 
Da wären wir wieder bei den Funktionen von SchelmVomElm. Sorry, aber die Funktionen sind echt eine komische Bastelei.
Schreib das Sendstring so um:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
function SendString(const Str: String): boolean;
var
  written  : cardinal;
begin
  if (PortHandle <> 0then
    Result := WriteFile(PortHandle,Str[1],Length(Str),written,@WriteOverlapped) else
    Result := False;
end;


Dann kannst du das PChar nämlich weglassen:
ausblenden Delphi-Quelltext
1:
SendString('#rme'+chr(0));					


// Edit1: Beispiel
// Edit2: else Result := False;
// Edit3: SchelmVomElm schreibt "dann eine DLL geschrieben. Aus der stammen die ganzen Funktionen. Daher übergebe ich auch PChars". Ok, er ist entschuldigt.


Zuletzt bearbeitet von delfiphan am So 27.02.05 14:07, insgesamt 3-mal bearbeitet
Finn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 43



BeitragVerfasst: So 27.02.05 13:57 
Hallo uall,

Danke für die schnelle Antwort. Ich habe das so ausprobiert aber es funktioniert leider nicht.
Habt ihr vielleicht noch eine Idee?

Viele Grüße
Finn
Finn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 43



BeitragVerfasst: So 27.02.05 14:02 
Titel: DANKE
DANKE LEUTE ES KLAPPT!!!!

IHR SEID SUPER !!!!!
uall@ogc
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1826
Erhaltene Danke: 11

Win 2000 & VMware
Delphi 3 Prof, Delphi 7 Prof
BeitragVerfasst: So 27.02.05 14:04 
die funktion von delfiphan sollte eigentlich funktionieren da ein string auch #0 beinhalten kann im gegensatz zu einem pchar
ausserdem gibt length(<string>) auch die richtige länge zurück ich hab das eben nochmal getestet

edit: dann is ja gut