Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

RS232 search received text

Status
Not open for further replies.

kobre98

Full Member level 2
Full Member level 2
Joined
Sep 18, 2011
Messages
145
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
Gaza, Palestine
Visit site
Activity points
2,523
Hey guys,
I have a device that send continuous signal in the following format
0000000=0000000=0000000=00000000000000=0000000=0000000=0000000=0000000=0000000=4500000=4700000=6500000=4800000

What I need to do is to search this and store the number that follows the "=" mark in a variable and convert it to integer or display it on LCD.

i DON'T want to read the zeros, just the number after the equal sign
 

Hi,

define a string REF = "123456789"

search your datastream character by character and check if it is included in REF.
If included, then print on display,

*****

This is a weird data format.
non constant number of digits,
"=" in it
spaces in it.

*****

if you´d say what programming language you use you get better help.


Klaus
 

I use mikroC v6.5
The data is coming from a weight scale with rs232 cable
I need to cut the numbers after the = mark and display it without zeros in LCD
 

you can use the string tokeniser to break the string into tokens (seperaated by =) and then use sscanf() to convert the values to integers, e.g.
Code:
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="0000000=0000000=0000000=00000000000000=0000000=0000000=0000000=0000000=0000000=4500000=4700000=6500000=4800000";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str,"=");
  while (pch != NULL)
  {
    int x=999;
    printf ("string found %s\n",pch);
    sscanf(pch,"%d",&x);
    printf("integer value decoded %d\n",x);
     pch = strtok (NULL, "=");
  }
  return 0;
}
when run gives
Code:
Splitting string "0000000=0000000=0000000=00000000000000=0000000=0000000=0000000=0000000=0000000=4500000=4700000=6500000=4800000" into tokens:
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 00000000000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 4500000
integer value decoded 4500000
string found 4700000
integer value decoded 4700000
string found 6500000
integer value decoded 6500000
string found 4800000
integer value decoded 4800000
 

Hi,

i DON'T want to read the zeros, just the number after the equal sign

do you really need no zero, or do you just want to dismiss the trailing zeroes?

does your datastream include [space] or not ?

Klaus
 

you can use the string tokeniser to break the string into tokens (seperaated by =) and then use sscanf() to convert the values to integers, e.g.
Code:
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="0000000=0000000=0000000=00000000000000=0000000=0000000=0000000=0000000=0000000=4500000=4700000=6500000=4800000";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str,"=");
  while (pch != NULL)
  {
    int x=999;
    printf ("string found %s\n",pch);
    sscanf(pch,"%d",&x);
    printf("integer value decoded %d\n",x);
     pch = strtok (NULL, "=");
  }
  return 0;
}
when run gives
Code:
Splitting string "0000000=0000000=0000000=00000000000000=0000000=0000000=0000000=0000000=0000000=4500000=4700000=6500000=4800000" into tokens:
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 00000000000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 0000000
integer value decoded 0
string found 4500000
integer value decoded 4500000
string found 4700000
integer value decoded 4700000
string found 6500000
integer value decoded 6500000
string found 4800000
integer value decoded 4800000

Thanks for answering me, but is this a code written for MikroC compiler ?!
I couldn't run it.

- - - Updated - - -

Hi,



do you really need no zero, or do you just want to dismiss the trailing zeroes?

does your datastream include [space] or not ?

Klaus

I need to eliminate the trailing zeros, so that I can only display the weight without zeros
The data stream doesn't include spaces, just numbers and = sign
 

Hi,

please write the values of the string in the datastream for the given weight:

Code:
weight  --> string
2       -->  ?
20      -->  ?
205     -->  ? 
2050    -->  ?

Klaus
 

Hi,

please write the values of the string in the datastream for the given weight:

Code:
weight  --> string
2       -->  ?
20      -->  ?
205     -->  ? 
2050    -->  ?

Klaus

Code:
weight  --> string
2       -->  20000000
20      -->  02000000
205     -->  50200000 
2050    -->  05020000

These are the values but I need to get the txt after the = sign first.
 

Thanks for answering me, but is this a code written for MikroC compiler ?!
I couldn't run it.

I don't use microc
what was the problem?
would it compile? if not what were the errors
due to the size of your integer values you may need to make the integer variable long int
 

you could string tokenise , strip out trainlins 0's then convert to integer

e.g.this run on an Explorer 16 with a PIC24 using the XC16 compiler
Code:
// test1.c -  test printf and strtok()


#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <ctype.h>
#include "Hardware.h"
#include "uart2.h"
#include <stdio.h>



// Setup configuration bits
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) 
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_HS & FNOSC_PRIPLL ) 


// main program code
int main(void)
{
  char str[] ="6500000=0000000=0000000=0000000=00000000000000=0000000=0000000=0000000=0000000=0000000=4500000=4700000=6500000=4800000";
  char * pch;
    UART2Init(9600ul);										// Setup the UART
    UART2PrintString("\n\nPIC24 \n\r");
    printf("\n\nPIC24 printf() test\n\r");
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str,"=");
  while (pch != NULL)
  {
    long int x=0;
    printf ("\nstring found %s ",pch);
    sscanf(pch,"%ld",&x);
    printf("     - integer value decoded %ld\n",x);
    // removed trailing 0's
	while(strlen(pch)>0)
      // if traing '0' replace with string terminator 0 (NULL)
      if(pch[strlen(pch)-1]=='0') pch[strlen(pch)-1]=0; 
      // else if not '0' exit while()
      else break;
    printf ("remove trailing 0's  %s",pch);
    sscanf(pch,"%ld",&x);
    printf("   - updated integer value decoded %ld\n",x);
     pch = strtok (NULL, "=");
  }
	while(1);
}
when run it gives
Code:
PIC24 printf() test
Splitting string "6500000=0000000=0000000=0000000=00000000000000=0000000=0000000=0000000=0000000=0000000=4500000=4700000=6500000=4800000" into tokens:

string found 6500000      - integer value decoded 6500000
remove trailing 0's  65   - updated integer value decoded 65

string found 0000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 0000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 0000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 00000000000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 0000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 0000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 0000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 0000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 0000000      - integer value decoded 0
remove trailing 0's     - updated integer value decoded 0

string found 4500000      - integer value decoded 4500000
remove trailing 0's  45   - updated integer value decoded 45

string found 4700000      - integer value decoded 4700000
remove trailing 0's  47   - updated integer value decoded 47

string found 6500000      - integer value decoded 6500000
remove trailing 0's  65   - updated integer value decoded 65

string found 4800000      - integer value decoded 4800000
remove trailing 0's  48   - updated integer value decoded 48
 

Isn't there a way to do this in MikroC complier ?!?!!
I have this code

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int i;
  char receive[11];
 
void main() {
    UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
 
  UART1_Write_Text("Start:");
  UART1_Write(10);
  UART1_Write(13);
 
  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
    for(i=0;i<10;i++){
    receive[i]= UART1_Read();
    //Delay_ms(500);
    } }
    //rcreg =0;
    UART1_Write_Text(receive);
  }
 
 
}



I need to check the data after receiving it and see where the equal sign is and take 6 to 8 digits after it and display them on LCD
 
Last edited by a moderator:

Code:
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[] = "=test";

char uart_rd; 
void main() 
{ 

  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,6,txt1);                 // Write text in first row


UART1_Init(9600); // Initialize UART module at 9600 bps 
Delay_ms(100); // Wait for UART module to stabilize 
UART1_Write_Text("Start"); 
UART1_Write(10); 
UART1_Write(13); 
while (1) 
{ // Endless loop 

if (UART1_Data_Ready()) { // If data is received, 
uart_rd = UART1_Read(); // read the received data,
if(uart_rd=='='){Lcd_Out(1,6,"=123456"); }
UART1_Write(uart_rd); // and send data via UART

 } } }

This is a new code I'm working on
could any one help me solving this issue
 

Please learn how to use serial interrupts, it makes your program far more efficient.

Why the delay after initializing the UART? It isn't necessary.

At the moment the code in post #14 should output "=123456" on the LCD and send "=" to UART1 every time it reads "=" from UART1. That is assuming you don't miss the incoming character by using polling instead of interrupts.

The solution to your problem depends on how consistent the data you receive is. Do all transmissions start with a special character or something you can use to say "this is the start of the string" or is a an ever ending stream of = symbols and numbers?

If it does have something to identify the first character, are you trying to extract just one block of numbers or all the blocks?

Does the string have a terminating character? For example a carriahge return or NUL character at the end of the lines?

I'm trying to establish whether capturing 'on the fly' or parsing a complete received string is the optimal solution.

Brian.
 

@betwixt

mikroE tells that the 100 ms delay is necessary after UART intialization for stabilization of UART. They have given that in their UART example code.
 

I can't think why that should be necessary unless possibly the parameters are being changed while a transmission or reception is already underway. That would give time for existing data in the FIFO to clear but switching settings in mid flow would be unwise for many reasons. It wouldn't apply when first setting up the UART anyway. If MikroE think it necessary, I wonder why they don't build the delay into the UART init routine. The more I hear about Mikro compilers, the less I like them.

Brian.
 

Please learn how to use serial interrupts, it makes your program far more efficient.

Why the delay after initializing the UART? It isn't necessary.

At the moment the code in post #14 should output "=123456" on the LCD and send "=" to UART1 every time it reads "=" from UART1. That is assuming you don't miss the incoming character by using polling instead of interrupts.

The solution to your problem depends on how consistent the data you receive is. Do all transmissions start with a special character or something you can use to say "this is the start of the string" or is a an ever ending stream of = symbols and numbers?

If it does have something to identify the first character, are you trying to extract just one block of numbers or all the blocks?

Does the string have a terminating character? For example a carriahge return or NUL character at the end of the lines?

I'm trying to establish whether capturing 'on the fly' or parsing a complete received string is the optimal solution.

Brian.

Thanks for answering me
I will answer your questions


The solution to your problem depends on how consistent the data you receive is. Do all transmissions start with a special character or something you can use to say "this is the start of the string" or is a an ever ending stream of = symbols and numbers?
- All the transmissions start with = sign and then a number of 6 digits that represent the weight, these reading are coming from a weight meter that measure the weight of people and send data at baud rate of 9600, the data stream is continuous, I want to read it until it's stabilized then display the final reading after the reading loop ends.
---------------------------------------------
If it does have something to identify the first character, are you trying to extract just one block of numbers or all the blocks?
the "=" sign is what identify the first character.
--------------------------------
Does the string have a terminating character? For example a carriahge return or NUL character at the end of the lines?
No terminating character the data steam is continuous.


-------
I edited the code it now display characters after recieving = sign but it doesn't display what I send in the same order or in the same number so If I sent =12453 it display = 1111 on LCD

Code:
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
int i;
char txt1[] = "=test";
char receive[11];
char uart_rd; 
void main() 
{ 

  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,6,txt1);                 // Write text in first row


UART1_Init(9600); // Initialize UART module at 9600 bps 
Delay_ms(100); // Wait for UART module to stabilize 
UART1_Write_Text("Start"); 
UART1_Write(10); 
UART1_Write(13); 
while (1) 
{ // Endless loop 

if (UART1_Data_Ready()) { // If data is received, 
//uart_rd = UART1_Read(); // read the received data,
  for(i=0;i<10;i++){
    receive[i]= UART1_Read();
    Delay_ms(500);
    if(receive[i]=='='){
Lcd_Out(1,6,receive);
//UART1_Write_Text(receive);
 }

    } }
    //rcreg =0;



 } }
 
Last edited:

If = is received then start filling a buffer until another = is received. Then subtract 48 from each character in the buffer convert the buffer value to integer value like

buff[3] = "123"

buff[0] = buff[0] - 48;
buff[1] = buff[1] - 48;
buff[2] = buff[2] - 48;

intVal = buff[0] * 100 + buff[1] * 10 + buff[2]

Can the value be like 004700 or does it come like 470000 and you want to extract only 47 and convert it to int value 47 ?

What if value 480 comes as 480000 ? You read it as 48 ?

If yes, then you can just read until a 0 is read but problem is if value is like 40800000.
 
Last edited:

This is in addition to your code.
It reads every char that comes in and if it isn't 0 after the = it puts it on the LCD and sends it to UART.

Code:
// LCD module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

char txt1[] = "=test";

char uart_rd; 
void main() 
{ 

  Lcd_Init();                        // Initialize LCD

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,6,txt1);                 // Write text in first row


UART1_Init(9600); // Initialize UART module at 9600 bps 
Delay_ms(100); // Wait for UART module to stabilize 
UART1_Write_Text("Start"); 
UART1_Write(10); 
UART1_Write(13); 
while (1) 
{ // Endless loop 

if (UART1_Data_Ready()) { // If data is received, 
uart_rd = UART1_Read(); // read the received data,
if(uart_rd=='='){
     if (UART1_Data_Ready()) { // If data is received, 
     uart_rd = UART1_Read(); // read the received data,
          if(uart_rd){             //if digit after = not zero  
              Lcd_Out_Cp(uart_rd); 
              UART1_Write(uart_rd); // and send data via UART
          }
     }
 } } }
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top