Einzelnen Beitrag anzeigen

Benutzerbild von Sharky
Sharky

Registriert seit: 29. Mai 2002
Ort: Frankfurt
8.251 Beiträge
 
Delphi 2006 Professional
 
#13

Re: Relaiskarte K8056 von Velleman seriell ansteuern

  Alt 24. Mai 2006, 12:04
Hai HaGü,

ich würde das ganze so aufbauen das man auch erkennt was, wo im Code passiert
Delphi-Quellcode:
type
  TCommandSet = array[1..4] of Byte;

const
  Cmd_Start = 13;
  Cmd_Nothalt = Ord('E'); //: Nothalt
  Cmd_DisplayAdress = Ord('D'); //: Display-Adresse
  Cmd_SetRelay = Ord('S'); //: Relais einstellen
  Cmd_ClearRelay = Ord('C'); //: Relais löschen
  Cmd_ToggleRelay = Ord('T'); //: Toggle-Relais
  Cmd_ChangeCardAdress = Ord('A'); //: Adresse einer Karte wechseln
  Cmd_SetAllCarsToOne = Ord('F'); //: Alle Karten auf Adresse 1
  Cmd_SendByte = Ord('B'); //: Byte senden

  Relay_1 = Ord('1');
  Relay_2 = Ord('2');
  Relay_3 = Ord('3');
  Relay_4 = Ord('4');
  Relay_5 = Ord('5');
  Relay_6 = Ord('6');
  Relay_7 = Ord('7');
  Relay_8 = Ord('8');
  AllRealy = Ord('9'); // Nur für Command S und C

function Zweierkomplement(aValue: Integer): Integer;
var
  neg: Boolean;
  iDez: Integer;
begin
  iDez := aValue;
  neg := iDez < 0;
  if (neg) then
    iDez := iDez * -1;
  iDez := iDez xor $FFFF;
  Inc(iDez);
  result := iDez;
end;

function CalcCheckSum(aValue: TCommandSet): Integer;
var
  summ: Integer;
  ndx: Integer;
begin
  summ := 0;
  for ndx := Low(aValue) to High(aValue) do
  begin
    summ := summ + aValue[ndx];
  end;
  result := Zweierkomplement(summ) + 1;
end;

procedure TDemoForm.btn_testClick(Sender: TObject);
var
  CommandSet: TCommandSet;
  CheckSum: Integer;
begin
  CommandSet[1] := Cmd_Start;
  CommandSet[2] := 1; // Karte 1
  CommandSet[3] := Cmd_ToggleRelay;
  CommandSet[4] := Relay_1;
  CheckSum := CalcCheckSum(CommandSet);
  // Und hier jetzt der Code um die 4 Byte des CommandSets und die Prüfsumme zu senden
end;
Was mir noch nicht klar ist: Die Prüfsumme kann ja kein Byte sein. Da ja die Summe von 4 Byte nicht in ein Byte passt. Andererseits wird aber ein Byte an die Karte gesendet.
Stephan B.
  Mit Zitat antworten Zitat