read through a text and store ascii bytes

Hi all,

i´ve searched here to find something which lets me "pull-out" individual ascii-bytes out of a text.

like: serial.print ("hello world");

does it.

But i need to do it manually. I have an old printer which need hadshakening for every letter.
Therefore i need to convert every letter into a single byte and store it temporarily in a variable print it out add the handshake and go on to the next letter.

I´m not very familiar with these char, ascii and string things, i also looked here:

especially the string.getBytes(buf, len) - but that´s not what i need and also may to complex for my rather little problem....

like this:

pseudo code:

pointer = 0;
byte = 0;

array string = "hello word.";

run until "." detected 

{
pointer at string and read letter; 

convert it into hex ascii and store in byte;

handshake; // i know how to do this;)

myPrintRoutine (byte); // i done this with hardcoded "t" by sending 0x74 works fine, but that´s boring;)

handshake; // simply wait until printer is ready again by testing a pin..

pionter++;
}

/pseudo code

Any help on how to get these ascii bytes out of a given "text" one by one would be nice.

thanx,
Tubical

Encapsulate the handshake with a "write character with handshake" function.

Then (untested):

char testString = "hello world";
printWithHandshake(testString);

printWithHandshake(char* charString)
{
  char *ptr = charString;

  while (*ptr != '\0')
  {
    writeWithHandshake(*ptr);
  }
}

writeWithHandshake(char c)
{
  // do your thing with c
}

Hi aarg,

Many thanx for your fast reply :slight_smile:

This looks like what I need to do in order to get my printer going...!

I'll try this within the next days (sparetime-tinkerer).

Cheers,
Tubical

Hi aarg,

i tested your code, with minor changes, as c&p into

void loop ()

{

your code

}
didn´t work, instead i got many many errors, like

writeWithHandshake is not decared

i done this

 /*
 PCF8574 ChipTest with the old Printer attached
*/

#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // Sainsmart keypad LCD

#define PCF B0111001  // Address of my PCF8574

int strobe = 3;
int i = 1;

char testString = "hello world";

void printWithHandshake(char* charString)
{
  char *ptr = charString;

  while (*ptr != '\0')
  {
    writeWithHandshake(*ptr);
  }
}



void writeWithHandshake(char c)
 {

  Wire.beginTransmission(PCF);
  digitalWrite(strobe, LOW);
  delay (2);
  Wire.write(c);
  lcd.print(c);
  delay (2);
  digitalWrite(strobe,HIGH);
  Wire.endTransmission();
  delay (500);
 }

void setup()
 
{
 
 Serial.begin(9600);
  lcd.begin(16, 2);
  lcd.print("hello, world!");

  pinMode(strobe, OUTPUT);
  digitalWrite(strobe, HIGH);


}

void loop() {

 Wire.begin();
 
 delay(100);

 lcd.setCursor(0, 1);
 lcd.print ("test");
 lcd.print (" ");
 lcd.print (i);
 lcd.print (" ");


printWithHandshake(testString);


 delay (1000);

i++;
 
}

And it doesn´t compile as well....
i got these errors:

TextPrint:14: error: invalid conversion from 'const char*' to 'char' [-fpermissive]

char testString = "hello world";

^

C:\Users\mywork\TextPrint.ino: In function 'void loop()':

TextPrint:69: error: invalid conversion from 'char' to 'char*' [-fpermissive]

printWithHandshake(testString);

^

TextPrint:16: error: initializing argument 1 of 'void printWithHandshake(char*)' [-fpermissive]

void printWithHandshake(char* charString)

^

if i change:

char testString = "hello world";

to

char* testString = "hello world";

to have it declared as an array of an array, my printer prints out infinite: hhh(...)h

So it somehow works, but not as intended...

any ideas? :wink:

printWithHandshake() needs to increment ptr in the while loop, too.

I guess @aarg left that out.

"Hello World" needs to be initialized as a char array.

Hi BL,

hmm not sure how...
may like this:

I really need to catch up on these pointers, any nice online doc on this, you know of?

void printWithHandshake(char* charString)
{
  char *ptr = charString;

  while (*ptr != '\0')
  {
    writeWithHandshake(*ptr);

    ptr++; // or ptr = ptr +1 ??
  }
}
void printWithHandshake(char* charString)
{
  char *ptr = charString;

Why bother copying the pointer?

i don´t know,

but if it´s needed to copy the pointer or not
doesn´t help me on my current issue....

doesn´t help me on my current issue

Which is . . ?

...which is....

how do i increase the *ptr...(see post #5 :wink:

i´m not very familiar with these pointers, sorry and thanx :slight_smile:

What happened when you tried the code in reply #5?

i used

ptr++;

as i assumed it does behave like variables, but it does the same thing (writing lots of h...)

is there a difference between pointers and usual variables like:

int i = 0;

(..)

i++;

it´s the first time for me dealing with pointers....

If you increment a simple integer variable with ++, it increments the value by one.
If you increment a pointer with ++, it increments the value of the pointer by the size of the thing that pointer points to.

dammit!

i should typed ptr..instead of prt...:wink:

now it does the job.

whoo-hoo...

THANX! :smiley: