Mein Beispiel für die Kommunikation zwischen Rasp und Arduino. Senden von vielen Bytes sind kein Problem, ich will aber mehr als die üblichen 2 Bytes empfangen.
4 Bytes schaffe ich jetzt. Ich würde aber lieber einen String empfangen, z.B. "200008001". Hat jemand eine Lösung ?
Das Testprogramm:
// Korrektur
Code: Alles auswählen
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, process, FileUtil, Forms, Controls, Graphics, Dialogs,
StdCtrls, Buttons,
BaseUnix;
const
I2C_SLAVE = 80;
type
{ TForm1 }
TForm1 = class(TForm)
eZiel: TEdit;
Label1: TLabel;
Label2: TLabel;
Memo1: TMemo;
Process1: TProcess;
sbInfo: TSpeedButton;
sb08Ein: TSpeedButton;
sb08Aus: TSpeedButton;
sbExit: TSpeedButton;
procedure sbInfoClick(Sender: TObject);
procedure sbExitClick(Sender: TObject);
procedure sb08EinClick(Sender: TObject);
procedure sb08AusClick(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.sbInfoClick(Sender: TObject);
var
AStringList : TStringList;
AProcess : TProcess;
begin
AProcess := TProcess.Create(nil);
AStringlist := TStringList.create;
AProcess.commandline := 'i2cdetect -y 1';
AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
Aprocess.Execute;
Astringlist.LoadFromStream(AProcess.Output);
Memo1.Clear;
Memo1.Lines.Add(' Aktive I2C-Adressen');
Memo1.Lines.Add('');
Memo1.Lines.Add(AStringlist.Text) ;
AStringList.Free;
AProcess.Free;
end;
procedure TForm1.sbExitClick(Sender: TObject);
begin
Form1.Close;
end;
procedure TForm1.sb08EinClick(Sender: TObject);
var
AStringList : TStringList;
AProcess : TProcess;
sDummy : string;
iDummy : integer;
begin
AProcess := TProcess.Create(nil);
AStringlist := TStringList.create;
// Set , in meinem Beispiel : Sende "200008001" an ...
AProcess.commandline := 'i2cset -y 1 0x' + eZiel.Text + ' 0x32 0x30 0x30 0x30 0x30 0x38 0x30 0x30 0x31 i';
AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
Aprocess.Execute;
// Get
AProcess.commandline := 'i2cget -y 1 0x' + eZiel.Text + ' 1 w';
AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
Aprocess.Execute;
Astringlist.LoadFromStream(AProcess.Output);
// Empfangene Daten umwandeln. Daten werden im Arduino generiert (0x00 bis 0xff) und (0x00 bis 0xff)
// also maximal 0xffff = 65535d
sDummy := AStringList.Text; // mit Steuerzeichen
sDummy := sDummy[1] + sDummy[2] + sDummy[3] + sDummy[4] + sDummy[5] + sDummy[6]; // ohne Steuerzeichen
iDummy := StrToInt(sDummy); // umwandeln in Integer
Memo1.Lines.Add(sDummy + ' ' + IntToStr(iDummy)); // Ausgabe in Memo
AStringList.Free;
AProcess.Free;
end;
procedure TForm1.sb08AusClick(Sender: TObject);
var
AStringList : TStringList;
AProcess : TProcess;
sDummy : string;
iDummy : integer;
begin
AProcess := TProcess.Create(nil);
AStringlist := TStringList.create;
// Set 2 0 0 0 0 8 0 0 0
AProcess.commandline := 'i2cset -y 1 0x' + eZiel.Text + ' 0x32 0x30 0x30 0x30 0x30 0x38 0x30 0x30 0x30 i';
AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
Aprocess.Execute;
// Get
AProcess.commandline := 'i2cget -y 1 0x' + eZiel.Text + ' 1 w';
AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
Aprocess.Execute;
Astringlist.LoadFromStream(AProcess.Output);
// Empfangene Daten umwandeln
sDummy := AStringList.Text;
sDummy := sDummy[1] + sDummy[2] + sDummy[3] + sDummy[4] + sDummy[5] + sDummy[6];
iDummy := StrToInt(sDummy);
Memo1.Lines.Add(sDummy + ' ' + IntToStr(iDummy));
AStringList.Free;
AProcess.Free;
end;
end.