RS232 transcoding

Hi

I have an Uno - connected with an RS232 TTL converter shield.

I would like to recieve an ASCII RS232 code - like "A11" on Rx

And send out something like "A112345" instead - on Tx

The codes come in, and should leave on the same baud rate...

How do I store the incoming codes, and compare them to a library - and send out new codes ?

I got this small program running:

/* Use a variable called byteRead to temporarily store
the data coming from the computer */
byte byteRead;

void setup() {
// Turn the Serial Protocol ON
Serial.begin(9600);
}

void loop() {
/* check if data has been sent from the computer: /
if (Serial.available()) {
/
read the most recent byte */
byteRead = Serial.read();
/*ECHO the value that was read, back to the serial port. */
Serial.write(byteRead);
}
}

How do I store the incoming codes,

Do not take them out of the serial buffer until you need to.
Take them out one command at a time. There should be a code delimiter so you can tell.

compare them to a library

Not sure what you mean by a library but you need two look up table, a string array (not a String array) one for the input and the other for the output. Search until you find a match and then you have the index number for the output string.

and send out new codes

Serial write one byte at a time.

Hi Mike

Thanks for your time.

Should I make a table with the codes I am waiting for ?

And then upon recieving an string in the table - send out the new string...

char init_str[] = {A,1,1,2,3,4,5};
writeBytes(init_str,r);

I am not sure how to build the table - could you help my with this ?

I have comed this far - with code from another RFID project:

//Reconized codes
char tag1[13] = "A01"; //

//Codes to transmit
char tag11[13] = "A1111"; //

void setup(){
Serial.begin(9600);

}

void loop(){

char tagString[13];
int index = 0;
boolean reading = false;

while(Serial.available()){

int readByte = Serial.read(); //read next available byte

if(reading && readByte != 2 && readByte != 10 && readByte != 13){
//store the tag
tagString[index] = readByte;
index ++;
}
}

checkTag(tagString); //Check if it is a match
clearTag(tagString); //Clear the char of all value
}

void checkTag(char tag[]){
///////////////////////////////////
//Check the read tag against known tags
///////////////////////////////////

if(strlen(tag) == 0) return; //empty, no need to contunue

if(compareTag(tag, tag1))
{ // if matched tag1, do this {
Serial.write(tag11);
delay(10);

}

// else if(compareTag(tag, tag2))
// { //if matched tag2, do this
// delay(10);
// }

// else
// {
// Serial.write(tag); //read out any unknown tag
// }

}

void clearTag(char one[]){
///////////////////////////////////
//clear the char array by filling with null - ASCII 0
//Will think same tag has been read otherwise
///////////////////////////////////
for(int i = 0; i < strlen(one); i++){
one = 0;

  • }*
    }
    boolean compareTag(char one[], char two[]){
    ///////////////////////////////////
    //compare two value to see if same,
    //strcmp not working 100% so we do this
    ///////////////////////////////////
  • if(strlen(one) == 0) return false; //empty*
  • for(int i = 0; i < 12; i++){*
    if(one != two*) return false;*
    * }*
    * return true; //no mismatches*
    }
    Could someone please help me to goal :0)

Have scaled this down alot...

How do I react on a specific string, instead of an character ?

char tag1[13] = "A1111";
char tag2[13] = "A0000";

void setup()
{
Serial.begin(9600);

}

void loop()
{
if(Serial.available() > 0)
{
char letter = Serial.read();

if(letter == '1')
{
Serial.write(tag1);
}
else if(letter == '0')
{
Serial.write(tag2);
}
}
}

You need an array of strings

char* inputStrings[]={"A11", "This is string 2", "This is string 3",
"This is string 4", "This is string 5","This is string 6"};

char* outputStrings[]={"A112345", "This is string 2", "This is string 3",
"This is string 4", "This is string 5","This is string 6"};

How do I react on a specific string,

from
https://www.inkling.com/read/arduino-cookbook-michael-margolis-2nd/chapter-2/recipe-2-18

char string1[ ] = "left";
  char string2[ ] = "right";

  if(strcmp(string1, string2) == 0)
     Serial.print("strings are equal)