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);
}
}
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.
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)
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"};