Convert passed string value to hex representation for serial.write


i have been working on project while , can't seem conversion of data-type.  sending data via serial control device.

currently output static uses format.  works fine way

serial.write(0x14);
serial.write(0x43);
serial.write(0x90);
serial.write(0x00);

however, wish dynamically fill in hex values above long string.  have string variable receives data external source mixed chars integers. 

string args = "f14439000"

i able parse individual components of string, struggling trying figure out how them serial.write command represented hex value.  i've tried , similar items stuck.

serial.write(args.substring(1,3),hex);
serial.write(args.substring(3,5),hex);
serial.write(args.substring(5,7),hex);
serial.write(args.substring(7,9),hex);


can please point me in right direction?  including program below, however, spark.io device there unique lines of code platform.

code: [select]

// #include statement automatically added spark ide
#include "liquidcrystal/liquidcrystal.h"
liquidcrystal lcd(6, 7, 5, 4, 3, 2);

int freq(string command);

void setup()
{
  lcd.begin(16,2);
  serial.begin(4800); // setup serial can monitor on screen
  serial1.begin(4800); // setup serial control device
  spark.function("urlcommand", docommand); // register spark function call via post
  delay(2000);
  lcd.clear();
  lcd.print(wifi.localip());
  delay(2000);
  lcd.clear();
}

void loop()
{
  lcd.setcursor(0,0);
  lcd.print(millis()/1000);
}


int docommand(string args)  // function automagically gets called upon matching post request.  args in post body param.
{
 
  //lcd.clear();
  int func = args.charat(0);
 
  if(func==102)  //f
  {
    lcd.setcursor(1,1);
    lcd.print(args.substring(1,4));
    lcd.print(".");
    lcd.print(args.substring(4,7));
    lcd.print(".");
    lcd.print(args.substring(7,10));
    lcd.print(" mhz");
    serialfreq(args);
    return 1;
  }
  else if(func==109) //m
  {
    lcd.setcursor(11,0);
    lcd.print(args.substring(1));
    return 1;
  }
  else return 0;
}

void serialfreq(string myvars){
  serial.print(myvars.substring(1,3));
  serial.print(".");
  serial.print(myvars.substring(3,5));
  serial.print(".");
  serial.print(myvars.substring(5,7));
  serial.print(".");
  serial.print(myvars.substring(7,9));
  serial.print("...... ");
 
  const byte = myvars.substring(1,3).toint();
  const byte b = myvars.substring(3,5).toint();
  const byte c = myvars.substring(5,7).toint();
  const byte d = myvars.substring(7,9).toint(); 
 
  serial1.write(a);
  serial1.write(b);
  serial1.write(c);
  serial1.write(d);
 
 
}



rich



















i must missing something....you have data character array (i.e. - "string"), in hex:

code: [select]

string args = "f14439000";


and want know how print serial port in hex?

what's wrong with:

code: [select]

serial.print(args);


regards,
ray l.


Arduino Forum > Using Arduino > Programming Questions > Convert passed string value to hex representation for serial.write


arduino

Comments

Popular posts from this blog

invalid use of void expresion in FlexiTimer2 library

error: a function-definition is not allowed here before '{' token

LED Strip Code