View previous topic :: View next topic |
Author |
Message |
nicotec
Joined: 07 Nov 2008 Posts: 60
|
RS232 reading string |
Posted: Sat Feb 07, 2009 8:36 am |
|
|
Hi, please try to help me since I'm locked to read single character and join in a string; this is my code (using also CCS example):
Code: | #include <24FJ16GA002.h>
#include <string.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOJTAG //JTAG disabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES NODEBUG //No Debug mode for ICD
#FUSES ICS3 //ICD communication channel 3
#FUSES IOL1WAY //Allows only one reconfiguration of peripheral pins
#FUSES WINDIS //Watch Dog Timer in non-Window mode
#FUSES WPRES128 //Watch Dog Timer PreScalar 1:128
#FUSES WPOSTS16 //Watch Dog Timer PostScalar 1:32768
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FRC //Internal Fast RC Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES NOOSCIO //OSC2 is general purpose output
#FUSES NOPR //Primary oscillaotr disabled
#FUSES I2C1SELD
#use delay(clock=8000000)
#pin_select OC1=PIN_B0 //OUTPUT OF PWM PIN 4
#pin_select OC2=PIN_B1 //OUTPUT OF PWM PIN 5
#pin_select U1TX=PIN_B10 //RS-232 TX PIN 21
#pin_select U1RX=PIN_B11 //RS-232 RX PIN 22
#use rs232(UART1,baud=9600,parity=N,bits=8,stop=1)
#BYTE U1RXREG = 0x226
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main()
{
long an_1;
long an_2;
long an_3;
char* s;
unsigned int8 len;
char c;
setup_spi(SPI_SS_DISABLED);
setup_spi2(SPI_SS_DISABLED);
setup_wdt(WDT_ON);
setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
enable_interrupts(INT_RDA);
enable_interrupts(intr_global);
setup_timer2(TMR_INTERNAL | TMR_DIV_BY_8,10000 );
setup_compare(1,COMPARE_PWM | COMPARE_TIMER2 );
setup_compare(2,COMPARE_PWM | COMPARE_TIMER2 );
setup_adc_ports( sAN9 | sAN10 | sAN11 );
setup_adc(ADC_CLOCK_INTERNAL );
set_pwm_duty(1,1000); // 2ms on status
set_pwm_duty(2,1000); // 2ms on status
output_low(PIN_B8); //common pin for led
output_low(PIN_B6); //led off
output_low(PIN_B7); //led off
do
{
set_adc_channel( 9 );
an_1 = read_adc();
set_adc_channel( 10 );
an_2 = read_adc();
set_adc_channel( 11 );
an_3 = read_adc();
output_high(PIN_B6);
printf("%Lu%c%Lu%c%Lu%c%c",an_1,10,an_2,10,an_3,10,13);
delay_ms(300);
if(bgetc()='S')
{
do
{
c=bgetc();
s[len++]=c;
} while(c!=13);
s[len]=0;
puts(s);
}
} while(true);
} |
I'll try on monday the above code and I hope to obtain my string from serial buffer in variable s; before to try I ask you if may be an error to work my code.
This is what I want to do: send serial line ended with LFCR type "S55,92" so after detected letter S I need to put in a string s 55,92 which is a code that I need to be analyzed.
Thanks in advance for your help and regards! |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Feb 07, 2009 9:12 am |
|
|
Unfortunately, you seem neither to be able to tell the observed problem nor to post a code, that is reduced to the part dealing with this problem.
Without guessing too much, I can point to one detail, that surely don't work. Actually, there a several errors at once. 1. You don't provide a storage. 2. You don't initialize s. 3. You don't initialize len.
Code: | char* s;
unsigned int8 len;
//..
s[len++]=c; |
|
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
|
Posted: Sat Feb 07, 2009 9:21 am |
|
|
Thanks for reply, I try to add more details:
code needed to be observed is
Code: | if(bgetc()=='S')
{
do
{
c=bgetc();
s[len++]=c;
} while(c!=13);
s[len]=0;
puts(s);
} |
ok to initialize len with len=0 at the begin, how to initialize and then use s (I extracted from my_get_string example code) and how can I store my string reading each character using bgetc() function?
Thanks |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Feb 07, 2009 1:33 pm |
|
|
The code is O.K., if s is a valid target. |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
|
Posted: Sat Feb 07, 2009 1:53 pm |
|
|
Sorry, but I have some difficult to understand. In your previous reply you have specified that char* s is not correctly defined/initialized. Please could you clarify?
Thank you very much regarding your patience.
Bye |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Feb 07, 2009 4:17 pm |
|
|
You must use an array of char rather than an uninitialized pointer. E.g. char s[32]; |
|
|
nicotec
Joined: 07 Nov 2008 Posts: 60
|
|
Posted: Sun Feb 08, 2009 2:13 am |
|
|
Thanks, so changing variable to s[8] in my case I can obtain a string s="15,18" after the loop reading "1" "5" "," "1" "8". It's correct?
I think that probably I can use strcat function as follows:
Code: | char s;
do
{
c=bgetc();
s=strcat(s,c);
} while(c!=13);
puts(s); |
Thanks in advance and regards |
|
|
|