kobre98
Full Member level 2
- Joined
- Sep 18, 2011
- Messages
- 145
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,296
- Location
- Gaza, Palestine
- Activity points
- 2,523
/* 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;
}
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
i DON'T want to read the zeros, just the number after the equal sign
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.
when run givesCode:/* 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; }
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,
do you really need no zero, or do you just want to dismiss the trailing zeroes?
does your datastream include [space] or not ?
Klaus
weight --> string
2 --> ?
20 --> ?
205 --> ?
2050 --> ?
Hi,
please write the values of the string in the datastream for the given weight:
Code:weight --> string 2 --> ? 20 --> ? 205 --> ? 2050 --> ?
Klaus
weight --> string
2 --> 20000000
20 --> 02000000
205 --> 50200000
2050 --> 05020000
Thanks for answering me, but is this a code written for MikroC compiler ?!
I couldn't run it.
// 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);
}
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
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); } }
// 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
} } }
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.
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;
} }
// 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
}
}
} } }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?