Pages: [1]   Go Down
Author Topic: Converting serial character string to new line or carriage return  (Read 1639 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a serial feed (ascii closed captioning text) transmitting to an arduino uno on pin 0 which works fine. The text file is then read by a sketch which looks for a new line or carriage return to  execute a command... The problem I'm facing is that the serial feed coming into the arduino uses 
Quote
¶&¶-¶p
in place of the new line or carriage return. How can I convert this to a new line or carriage return that will be recognized by the following code...

Code:
//Command line variables
String command; //String input from command promt
String temp1, temp2; //temporary strings
char inByte; //Byte input from command promt
char carray [6]; // character array for string to in // manipulation
int a,b,c;// temporary numbers
  //Controled Variables
 int Source1 =  11;
int Source2 =  12;
int StatusLED = 10;
int voidLED = 13;
int  incomingByte = 0;

unsigned long breakUntil;
boolean breakStarted = false;
long breakMax = 300000;
  
void setup () {
//initialize serial:
Serial.begin(1200, SERIAL_7O1);

   pinMode(Source1, OUTPUT);
   pinMode(Source2, OUTPUT);
   pinMode(StatusLED, OUTPUT);
   pinMode(voidLED, OUTPUT);
 
   Serial.println("Ready");
   digitalWrite(Source2,HIGH);
   digitalWrite(Source1,HIGH);
   delay(300);
   digitalWrite(Source1,LOW);
   delay(300);
   digitalWrite(Source1,HIGH);
}

void loop() {
  //input serial information:
  if (Serial.available () > 0){
    inByte = Serial.read();
    Serial.write(inByte);
      //only input if a letter, number or valid character are typed!
      if ((inByte >= 65 && inByte <= 90) || (inByte >= 97 && inByte <= 122) || (inByte >= 48 && inByte <= 57) || inByte == 61 || inByte == 63) {
        command.concat (inByte);
      }
  }//end serial.available
  
  //Process command when NL/CR are entered:
  if (inByte == 10 || inByte == 13 || inByte == 244){
        inByte = 0;

   if(command.equalsIgnoreCase("THE")){
    //Source 2 On, Source 1 Off
    breakSource();}
    
   else if(command.equalsIgnoreCase("BreakStop")){
    //Source 1 On, Source 2 Off
liveSource();}

//override
  else if(command.equalsIgnoreCase("Override")){
    //Source 1 On, Source 2 Off
voidSource();}
  
  command = "";}
  if ((breakStarted) && (millis() > breakUntil)) {
          voidBreak();
        }}

void breakSource() {
  Serial.println("\n \n Break Started \n");
  digitalWrite(Source2,LOW);
  delay(300);
  digitalWrite(Source2,HIGH);
  digitalWrite(StatusLED, HIGH);
  breakUntil = millis() + breakMax;
  breakStarted = true;
}

void StartBreak(){
breakSource();
}

void liveSource() {
  Serial.println("\n \n Break Stopped \n");
  digitalWrite(Source1,LOW);
  delay(300);
  digitalWrite(Source1,HIGH);
  digitalWrite(StatusLED,LOW);
  breakStarted = false;
}

void StopBreak(){
liveSource();
}

void voidSource() {
  Serial.println("\n \n Break Voided \n");
  digitalWrite(voidLED, HIGH);
  digitalWrite(Source1,LOW);
  delay(300);
  digitalWrite(Source1,HIGH);
  digitalWrite(StatusLED, LOW);
  delay(500);
  digitalWrite(voidLED, LOW);

  breakStarted = false;
}

void voidBreak(){
voidSource();
}

Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
¶&¶-¶p
Can you write that as hexadecimal numbers ?
I think it is very unlikely that a device is sending that. Is the data at other baudrate, other parity, other bit lenght, or inverted ?
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The byte sequence looks like 0xB6 0x26 0xB6 0x2D 0xB6 0x70.

Perhaps it's some sort of multi-byte character set, although it doesn't look like unicode. Anyway, if you know you get that at the end of each line you could just look for that byte sequence, and treat that as end-of-line.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

change this so it looks for your character rather than a newline:
Code:
//Process command when NL/CR are entered:
  if (inByte == 10 || inByte == 13 || inByte == 244){
        inByte = 0;
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In hexadecimal it looks like... 14 26 14 2D 14
Because the source is 608 closed captioning it uses modified ascii, but even on that table I couldn't find an equivalent for 14...

Is there a way to do string replacement where that particular string can be replaced with a newline?
Logged

Offline Offline
Edison Member
*
Karma: 58
Posts: 2078
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't make sense out of those numbers.
Perhaps they made a protocol with the 0x14 as escape character.
http://en.wikipedia.org/wiki/Escape_character

The translation can be done if the line of text is read into a buffer.

Is there a maximum for the length of a line ?
Does the line start with a special character ?
That would make it easier to start filling a buffer.

Rough layout:
If data is available, write it in the buffer.
If the last character is recognized (0x14 or 'p' 0x70 ?), test the last 5 or 6 bytes in that buffer.
If they match the trailing string, it is the end of a line.
Process the whole line.
« Last Edit: April 09, 2013, 01:01:25 pm by Erdin » Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7303
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I would do what WizenedEE suggested and just add "|| inByte == 20" to the if statement. 20 is the decimal value of Hex 14. There is no need to replace it with carriage return elsewhere.
Logged


UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rough layout:

I'd go for a state machine where the 'state' is the number of bytes in the incoming stream which have been matched against the end-of-line byte sequence.

Code:
if(inchar == eolSequence[eolState])
{
  eolState++;
  if(eolState >= strlen(eolSequence))
  {
    handleNewline()
    eolState = 0;
  }
}
else
{
  eolState = 0;
}
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

THANK YOU very much everyone for you help and input...
I finally realized that I was trying to put lipstick on a pig as they say by trying to make the output in the serial monitor "pretty" but it wasn't and isn't needed for the functionality of the project so in the end WizenedEE and  liudr's solution did the trick.
Thank you again everyone... final code is posted below.

Code:
//Command line variables
String command; //String input from command promt
String temp1, temp2; //temporary strings
char inByte; //Byte input from command promt
char carray [6]; // character array for string to in // manipulation
int a,b,c;// temporary numbers
  //Controled Variables
 int Source1 =  11;
int Source2 =  12;
int StatusLED = 10;
int voidLED = 13;
int  incomingByte = 0;

unsigned long breakUntil;
boolean breakStarted = false;
long breakMax = 300000;
 
void setup () {
//initialize serial:
Serial.begin(1200, SERIAL_7O1);

   pinMode(Source1, OUTPUT);
   pinMode(Source2, OUTPUT);
   pinMode(StatusLED, OUTPUT);
   pinMode(voidLED, OUTPUT);
 
   Serial.println("Ready");
   digitalWrite(Source2,HIGH);
   digitalWrite(Source1,HIGH);
   delay(300);
   digitalWrite(Source1,LOW);
   delay(300);
   digitalWrite(Source1,HIGH);
}

void loop() {
  //input serial information:
  if (Serial.available () > 0){
    inByte = Serial.read();
    Serial.write(inByte);
      //only input if a letter, number or valid character are typed!
      if ((inByte >= 65 && inByte <= 90) || (inByte >= 97 && inByte <= 122) || (inByte >= 48 && inByte <= 57) || inByte == 61 || inByte == 63) {
        command.concat (inByte);
      }
  }//end serial.available
 
  //Process command when NL/CR are entered:
  if (inByte == 10 || inByte == 13 || inByte == 20){
        inByte = 0;

   if(command.equalsIgnoreCase("BreakStart")){
    //Source 2 On, Source 1 Off
    breakSource();}
   
   else if(command.equalsIgnoreCase("BreakStop")){
    //Source 1 On, Source 2 Off
liveSource();}

//override
  else if(command.equalsIgnoreCase("Override")){
    //Source 1 On, Source 2 Off
voidSource();}
   
  command = "";}
  if ((breakStarted) && (millis() > breakUntil)) {
          voidBreak();
        }}

void breakSource() {
  Serial.println("\n \n Break Started \n");
  digitalWrite(Source2,LOW);
  delay(300);
  digitalWrite(Source2,HIGH);
  digitalWrite(StatusLED, HIGH);
  breakUntil = millis() + breakMax;
  breakStarted = true;
}

void StartBreak(){
breakSource();
}

void liveSource() {
  Serial.println("\n \n Break Stopped \n");
  digitalWrite(Source1,LOW);
  delay(300);
  digitalWrite(Source1,HIGH);
  digitalWrite(StatusLED,LOW);
  breakStarted = false;
}

void StopBreak(){
liveSource();
}

void voidSource() {
  Serial.println("\n \n Break Voided \n");
  digitalWrite(voidLED, HIGH);
  digitalWrite(Source1,LOW);
  delay(300);
  digitalWrite(Source1,HIGH);
  digitalWrite(StatusLED, LOW);
  delay(500);
  digitalWrite(voidLED, LOW);

  breakStarted = false;
}

void voidBreak(){
voidSource();
}
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7303
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Glad it works now!
Logged


Pages: [1]   Go Up
Jump to: