AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

in Byte umrechnen und per string senden

Ein Thema von ljmarkus · begonnen am 2. Mai 2009 · letzter Beitrag vom 2. Mai 2009
Antwort Antwort
ljmarkus

Registriert seit: 14. Apr 2007
Ort: Göttingen
46 Beiträge
 
Delphi 7 Personal
 
#1

in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 10:32
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
  Mit Zitat antworten Zitat
Apollonius

Registriert seit: 16. Apr 2007
2.325 Beiträge
 
Turbo Delphi für Win32
 
#2

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 10:40
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.
Wer erweist der Welt einen Dienst und findet ein gutes Synonym für "Pointer"?
"An interface pointer is a pointer to a pointer. This pointer points to an array of pointers, each of which points to an interface function."
  Mit Zitat antworten Zitat
ljmarkus

Registriert seit: 14. Apr 2007
Ort: Göttingen
46 Beiträge
 
Delphi 7 Personal
 
#3

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 10:43
Hi.

komme jetzt nicht ganz mit wie das auszusehen hat.


Danke, Markus
  Mit Zitat antworten Zitat
Apollonius

Registriert seit: 16. Apr 2007
2.325 Beiträge
 
Turbo Delphi für Win32
 
#4

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 10:48
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.
Wer erweist der Welt einen Dienst und findet ein gutes Synonym für "Pointer"?
"An interface pointer is a pointer to a pointer. This pointer points to an array of pointers, each of which points to an interface function."
  Mit Zitat antworten Zitat
Reinhard Kern

Registriert seit: 22. Okt 2006
772 Beiträge
 
#5

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 11:07
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
  Mit Zitat antworten Zitat
ljmarkus

Registriert seit: 14. Apr 2007
Ort: Göttingen
46 Beiträge
 
Delphi 7 Personal
 
#6

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 11:08
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);
  Mit Zitat antworten Zitat
Apollonius

Registriert seit: 16. Apr 2007
2.325 Beiträge
 
Turbo Delphi für Win32
 
#7

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 11:13
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.
Wer erweist der Welt einen Dienst und findet ein gutes Synonym für "Pointer"?
"An interface pointer is a pointer to a pointer. This pointer points to an array of pointers, each of which points to an interface function."
  Mit Zitat antworten Zitat
ljmarkus

Registriert seit: 14. Apr 2007
Ort: Göttingen
46 Beiträge
 
Delphi 7 Personal
 
#8

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 11:19
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.....
  Mit Zitat antworten Zitat
Apollonius

Registriert seit: 16. Apr 2007
2.325 Beiträge
 
Turbo Delphi für Win32
 
#9

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 11:25
Ich glaube, dass du alle Bytes im String erst explizit auf #0 setzen musst.
Wer erweist der Welt einen Dienst und findet ein gutes Synonym für "Pointer"?
"An interface pointer is a pointer to a pointer. This pointer points to an array of pointers, each of which points to an interface function."
  Mit Zitat antworten Zitat
Muetze1
(Gast)

n/a Beiträge
 
#10

Re: in Byte umrechnen und per string senden

  Alt 2. Mai 2009, 12:21
@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.
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:14 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