Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi in Byte umrechnen und per string senden (https://www.delphipraxis.net/133461-byte-umrechnen-und-per-string-senden.html)

ljmarkus 2. Mai 2009 10:32


in Byte umrechnen und per string senden
 
Hallo.

ich lese die Paintbox aus ob Pixel gesetzt sind. Immer 8 Pixel um es per 1 Byte zusenden.

Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);

var timebuffer: String;

    MyTime: TDateTime;

    Data: array [0..55] of string;

    Bit0,Bit1,Bit2,Bit3,Bit4,Bit5,Bit6,Bit7: byte;

begin

if PaintBox1.Canvas.Pixels[0,0] = clBlack then bit0 := 1 else bit0 := 0;

if PaintBox1.Canvas.Pixels[1,0] = clBlack then bit1 := 1 else bit1 := 0;

if PaintBox1.Canvas.Pixels[2,0] = clBlack then bit2 := 1 else bit2 := 0;

if PaintBox1.Canvas.Pixels[3,0] = clBlack then bit3 := 1 else bit3 := 0;

if PaintBox1.Canvas.Pixels[4,0] = clBlack then bit4 := 1 else bit4 := 0;

if PaintBox1.Canvas.Pixels[5,0] = clBlack then bit5 := 1 else bit5 := 0;

if PaintBox1.Canvas.Pixels[6,0] = clBlack then bit6 := 1 else bit6 := 0;

if PaintBox1.Canvas.Pixels[7,0] = clBlack then bit7 := 1 else bit7 := 0;

if bit0 = 1 then Data[0] := (Data[0] + #$80);//128;

if bit1 = 1 then Data[0] := (Data[0] + #$40);//64;

if bit2 = 1 then Data[0] := (Data[0] + #$20);//32;

if bit3 = 1 then Data[0] := (Data[0] + #$10);//16;

if bit4 = 1 then Data[0] := (Data[0] + #$8);//8;

if bit5 = 1 then Data[0] := (Data[0] + #$4);//4;

if bit6 = 1 then Data[0] := (Data[0] + #$2);

if bit7 = 1 then Data[0] := (Data[0] + #$1);

ComPort1.WriteStr(Data[0]);
end;
leider werden aber mehr als ein Byte gesendet.
Wie und wo habe ich mich da jetzt verzettelt ?

z.b. Wenn alle Pixel gesetzt sind soll ein Hex FF gesendet werden.

Danke, Markus

Apollonius 2. Mai 2009 10:40

Re: in Byte umrechnen und per string senden
 
Du willst kein Array of String, du willst einen String. Und dann bist du über den +-Operator gestolpert, denn dieser fügt Strings zusammen. In Wirklichkeit willst du den aktuellen Buchstaben als Byte betrachten und dazu die Bitwerte addieren.
Wenn du mit Strings arbeitest, musst du aufpassen, dass sich die Comport-Komponente nicht an #0-Zeichen verschluckt. Wenn eine solche Möglichkeit zur Verfügung steht, solltest du auf Byte-Arrays umsteigen.

ljmarkus 2. Mai 2009 10:43

Re: in Byte umrechnen und per string senden
 
Hi.

komme jetzt nicht ganz mit wie das auszusehen hat.


Danke, Markus

Apollonius 2. Mai 2009 10:48

Re: in Byte umrechnen und per string senden
 
Delphi-Quellcode:
var Data: String;
//...
SetLength(Data, 1);

if bit0 = 1 then
  Byte(Data[1]) := Byte(Data[1]) + $80;
//...
ComPort1.WriteStr(Data);
Ich vermute mal, dass du später mehrere Bytes auf einmal senden willst. Dann musst du den zweiten Parameter von SetLength entsprechend anpassen und Data entsprechend indizieren.

Reinhard Kern 2. Mai 2009 11:07

Re: in Byte umrechnen und per string senden
 
Zitat:

Zitat von ljmarkus
Hallo.

ich lese die Paintbox aus ob Pixel gesetzt sind. Immer 8 Pixel um es per 1 Byte zusenden.
...

Hallo,

probiers mal damit:

Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);
var i : byte;
    Data : string[55];
begin
Data := #0;
with PaintBox1.Canvas do
  for i := 0 to 7 do
     if Pixels[7-i,0] = clBlack then Data[0] := chr (ord(Data[0]) + 1 SHL i);
ComPort1.WriteStr(Data);
end;
ungetestet!!

Gruss Reinhard

ljmarkus 2. Mai 2009 11:08

Re: in Byte umrechnen und per string senden
 
Delphi-Quellcode:
var Data: String;
//...
SetLength(Data, 1);

if bit0 = 1 then
  Byte(Data[1]) := Byte(Data[1]) + $80;
//...
ComPort1.WriteStr(Data);
danke, das funktioniert soweit, nur für das zweite Byte nicht.

Delphi-Quellcode:
var Data: String;
//...
SetLength(Data, 2);

// bit 0-7 auslesen
if bit0 = 1 then
  Byte(Data[1] := Byte(Data[1]) + $80;
//...
// bit 8-15 auslesen
if bit0 = 1 then
  Byte(Data[2] := Byte(Data[2]) + $80;
//...
ComPort1.WriteStr(Data);

Apollonius 2. Mai 2009 11:13

Re: in Byte umrechnen und per string senden
 
Das sieht eigentlich richtig aus. Kannst du mal den ganzen Code zeigen? Prinzipiell kannst du das ganze auch stark vereinfachen, wie Reinhard ja bereits angedeutet hat.

ljmarkus 2. Mai 2009 11:19

Re: in Byte umrechnen und per string senden
 
hier mal der Code
Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);

var timebuffer: String;

    MyTime: TDateTime;

    Data: string;

    Bit0,Bit1,Bit2,Bit3,Bit4,Bit5,Bit6,Bit7: byte;



begin

SetLength(Data, 2);





  if PaintBox1.Canvas.Pixels[0,0] = clBlack then bit0 := 1 else bit0 := 0;

  if PaintBox1.Canvas.Pixels[1,0] = clBlack then bit1 := 1 else bit1 := 0;

  if PaintBox1.Canvas.Pixels[2,0] = clBlack then bit2 := 1 else bit2 := 0;

  if PaintBox1.Canvas.Pixels[3,0] = clBlack then bit3 := 1 else bit3 := 0;

  if PaintBox1.Canvas.Pixels[4,0] = clBlack then bit4 := 1 else bit4 := 0;

  if PaintBox1.Canvas.Pixels[5,0] = clBlack then bit5 := 1 else bit5 := 0;

  if PaintBox1.Canvas.Pixels[6,0] = clBlack then bit6 := 1 else bit6 := 0;

  if PaintBox1.Canvas.Pixels[7,0] = clBlack then bit7 := 1 else bit7 := 0;

  if bit0 = 1 then Byte(Data[1]) := Byte(Data[1]) + $80;//128;

  if bit1 = 1 then Byte(Data[1]) := Byte(Data[1]) + $40;//64;

  if bit2 = 1 then Byte(Data[1]) := Byte(Data[1]) + $20;//32;

  if bit3 = 1 then Byte(Data[1]) := Byte(Data[1]) + $10;//16;

  if bit4 = 1 then Byte(Data[1]) := Byte(Data[1]) + $8;//8;

  if bit5 = 1 then Byte(Data[1]) := Byte(Data[1]) + $4;//4;

  if bit6 = 1 then Byte(Data[1]) := Byte(Data[1]) + $2;

  if bit7 = 1 then Byte(Data[1]) := Byte(Data[1]) + $1;



  if PaintBox1.Canvas.Pixels[8,0] = clBlack then bit0 := 1 else bit0 := 0;

  if PaintBox1.Canvas.Pixels[9,0] = clBlack then bit1 := 1 else bit1 := 0;

  if PaintBox1.Canvas.Pixels[10,0] = clBlack then bit2 := 1 else bit2 := 0;

  if PaintBox1.Canvas.Pixels[11,0] = clBlack then bit3 := 1 else bit3 := 0;

  if PaintBox1.Canvas.Pixels[12,0] = clBlack then bit4 := 1 else bit4 := 0;

  if PaintBox1.Canvas.Pixels[13,0] = clBlack then bit5 := 1 else bit5 := 0;

  if PaintBox1.Canvas.Pixels[14,0] = clBlack then bit6 := 1 else bit6 := 0;

  if PaintBox1.Canvas.Pixels[15,0] = clBlack then bit7 := 1 else bit7 := 0;

  if bit0 = 1 then Byte(Data[2]) := Byte(Data[2]) + $80;//128;

  if bit1 = 1 then Byte(Data[2]) := Byte(Data[2]) + $40;//64;

  if bit2 = 1 then Byte(Data[2]) := Byte(Data[2]) + $20;//32;

  if bit3 = 1 then Byte(Data[2]) := Byte(Data[2]) + $10;//16;

  if bit4 = 1 then Byte(Data[2]) := Byte(Data[2]) + $8;//8;

  if bit5 = 1 then Byte(Data[2]) := Byte(Data[2]) + $4;//4;

  if bit6 = 1 then Byte(Data[2]) := Byte(Data[2]) + $2;

  if bit7 = 1 then Byte(Data[2]) := Byte(Data[2]) + $1;



ComPort1.WriteStr(Data);



end;
für jede 8bit muss ein byte erzeugt werden. Byte1 ist für die ersten 8bit, Byte2 für die nächsten 8bit usw.....

Apollonius 2. Mai 2009 11:25

Re: in Byte umrechnen und per string senden
 
Ich glaube, dass du alle Bytes im String erst explizit auf #0 setzen musst.

Muetze1 2. Mai 2009 12:21

Re: in Byte umrechnen und per string senden
 
@Apollonius: Bitverknüpfungen immer mit OR und nicht mit + !

@ljmarkus: Ein kleiner Tipp: Wenn du dir den Inhalt der PaintBox auf ein TBitmap kopierst und dieses danach (oder auch davor) auf das PixelFormat von pf1Bit setzt, dann hast du mit der Funktion ScanLine[] direkt zugriff auf die Pixeldaten, die dann schon so abgelegt sind, dass ein Bit ein Pixel entspricht. Ist das Bit gesetzt, ist der Pixel schwarz (bei der Standardpalette schwarz/weiss, default). Damit kannst du dir den Aufwand hier deutlich verringern.


Alle Zeitangaben in WEZ +1. Es ist jetzt 13:35 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz