I have been working on a script to interface my arduino to a serial device sending TAP (Telocator Alphanumeric Protocol). I am testing my code using a terminal program that can send scripts. I am able to send and recieve strings, but I'm hitting a road block trying to figure out how to parse a string coming from the termial program. Example TAP string might look like 101TEST PAGE>32> and I would like to parse the 101 or TEST PAGE and based on that send out another string from arduino, possibly . My code looks like this so far. I am also attaching a screen shot of what the debug window looks like after sending a script from the terminal program. The following link is to a previous post that helped get me started with the code. Greatly appreciate help or comments on the code. http://arduino.cc/forum/index.php/topic,99198.0/topicseen.html
/*===========================
Modified original code by Fayaz Kadir http://fayazkadir.com/blog/?p=2291
DESCRIPTION: If you send a string starting with "CR" and ending with "ESC",
the Arduino responds back with "$Y" for valid data string, or $N for invalid data string.
============================*/
#define NUL 0
#define SOH 1
#define STX 2
#define ETX 3
#define EOT 4
#define ACK 6
#define CR 13
#define ESC 27
#define ID "ID="
#define SP 32
#include <SoftwareSerial.h>
// software serial #1: TX = digital pin 2, RX = digital pin 3
SoftwareSerial portOne(2, 3);
char string[128];
char string1[6]= {0x0D,0x49,0x44,0x3D,'\n'}; //[CR][ID=]
char string2[] ={0x02,0x31,0x30,0x31,0x0D,0x54,0x45,0x53,0x54,0x20,0x50,0x41,0x47,0x45,
0x0D,0x03,0x33,0x32,0x0D}; //[STX][101][CR][TEST][SP][CR][ETX][32][CR]
int i=0;
int string_len;
char inByte;
int last_inByte;
void setup() {
portOne.begin(9600); //INTIALISING THE SERIAL PORT
Serial.begin(9600);
}
void loop() // LOOP FUNCTION
{
inByte = portOne.read();
string_len=0;
if (inByte == 13) read_serial(); // If Start of line (CR) is found, call read_serial() function
}
void read_serial() // FUNCTION FOR READING THE SERIAL MESSAGE
{
Serial.println ("CR FOUND and reading"); // THE SOL (Start of Line) found notice.
Serial.print("READ :CR\n"); //Saying that it has read "!" on the serial port
portOne.print("\n");
portOne.write(ID);
portOne.write(CR);
while (inByte!= (ESC)) // As long as EOL not found, keep reading
if (portOne.available() > 0) // if new data is available
{inByte = portOne.read(); // Read new byte
Serial.print("READ : "); // Display the new byte
string[string_len] = inByte; // Save the data in a character array
Serial.println(string[string_len]); // Print the characters that was recieved
string_len++;}
else if (inByte== (SP))
{Serial.println("EOL not available, data string invalid"); // If EOL not in the string
Serial.println("$N"); //FAIL SIGNATURE
break;
}
if (inByte == (ESC))
{
Serial.println ("eol FOUND, and full string was "); // Echoes Success Message
Serial.println ("$Y"); //SUCCESS SIGNATURE
for (i=0;i<(string_len-1);i++) Serial.print(string[i]);
portOne.write(string1);
portOne.write(string2);
Serial.print("\n");
}
}
Moderator edit: [code] ... [/code] tags added. (Nick Gammon)
Doc1.docx (91.5 KB)