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.

how to read a string line by line via rs232

Status
Not open for further replies.

adnan_merter

Full Member level 3
Joined
Jan 23, 2008
Messages
160
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Location
The most beautiful city of the world
Activity points
2,526
hi everybody,

i need to know how i can receive a string from rs232,

i mean (and already know the gets() and get_string() functions) i am sending a bunch of string without no time delay from pc to pic

and i need to read them line by line and compare each line whether it contains particular character and if it contains, pic should send it to the other pc

i already tried gets() function and it doesnt work,

here is my function, i have written to get each line seperately.

Code:
#include "main.h"
#include "LCD.c"
#include <string.h>

#use rs232(baud=9600,parity=N,xmit=PIN_C1,rcv=PIN_C0,bits=8,STOP=1, stream = PC2)
#use rs232(baud=300,parity=E,xmit=PIN_C6,rcv=PIN_C7,bits=7,STOP=1, stream = PC1)

static unsigned char ReceivedMsg[25];

static unsigned int1 endoftransmission = FALSE;

void Readline();

void main()
{

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   lcd_init();
   lcd_putc('\f');
   lcd_gotoxy(1,1);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab 
   
   // TODO: USER CODE!!

   fprintf(PC2,"WAITING FOR THE NECCESSARY VALUES");
   while(1)
   {
   while(!endoftransmission){ 
   Readline();
   fputs(ReceivedMsg,PC2);
   }
   }

}

void Readline()
{
memset(ReceivedMsg,0,sizeof(ReceivedMsg));
unsigned char MsgChar;
int k=0;
while(MsgChar != 0x0A)//getch() until LF char received
   {
      
      MsgChar = fgetc(PC1);   
     if(MsgChr=='!')
         endoftransmission=TRUE;
      ReceivedMsg[k] = MsgChar;
      k++;
   }
}

here is main.h

Code:
#include <18F2550.h>
#device adc=8

#FUSES NOWDT                 	//No Watch Dog Timer
#FUSES WDT128                	//Watch Dog Timer uses 1:128 Postscale
#FUSES HSPLL                 	//High Speed Crystal/Resonator with PLL enabled
#FUSES NOPROTECT             	//Code not protected from reading
#FUSES NOBROWNOUT            	//No brownout reset
#FUSES BORV20                	//Brownout reset at 2.0V
#FUSES NOPUT                 	//No Power Up Timer
#FUSES NOCPD                 	//No EE protection
#FUSES STVREN                	//Stack full/underflow will cause reset
#FUSES NODEBUG               	//No Debug mode for ICD
#FUSES NOLVP                 	//No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                 	//Program memory not write protected
#FUSES NOWRTD                	//Data EEPROM not write protected
#FUSES IESO                  	//Internal External Switch Over mode enabled
#FUSES FCMEN                 	//Fail-safe clock monitor enabled
#FUSES PBADEN                	//PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                	//configuration not registers write protected
#FUSES NOWRTB                	//Boot block not write protected
#FUSES NOEBTR                	//Memory not protected from table reads
#FUSES NOEBTRB               	//Boot block not protected from table reads
#FUSES NOCPB                 	//No Boot Block code protection
#FUSES MCLR                  	//Master Clear pin enabled
#FUSES LPT1OSC               	//Timer1 configured for low-power operation
#FUSES NOXINST               	//Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL5                 	//Divide By 5(20MHz oscillator input)
#FUSES CPUDIV1             	//System Clock by 1
#FUSES USBDIV                	//USB clock source comes from PLL divide by 2
#FUSES VREGEN                	//USB voltage regulator enabled

#use delay(clock=48000000)

(i didnt work on the compare section yet)
i am using pic18f2550 and ccs c compiler, when i tried to run this code, it reads first line and it stops, Readline() function starts for a second line but it doesnt read the second line, everything looks fine but something is definately wrong.

waiting for your advices thanks
 

A couple of things: You need to make sure that your serial-reading loop terminates before the ReceivedMsg variable overflows, and you have not initialised MsgChar - either initialise it before entering the while loop, or use a do...while loop instead.

I fail to see why gets() won't work, provided your string has a carriage return.

How are you programming the PIC - are you using the CCS ICD-U programmer?

Since you already have a serial output, try sending some debug information as well - like send a number for each line of your code, and the variable values, so that we can tell exactly where it stops. That's what I do if not using ICD. I have a serial VFD that I hook up just for that purpose even when not using the serial port for the program itself.
 
Last edited:

A couple of things: You need to make sure that your serial-reading loop terminates before the ReceivedMsg variable overflows, and you have not initialised MsgChar - either initialise it before entering the while loop, or use a do...while loop instead.

I fail to see why gets() won't work, provided your string has a carriage return.

How are you programming the PIC - are you using the CCS ICD-U programmer?

Since you already have a serial output, try sending some debug information as well - like send a number for each line of your code, and the variable values, so that we can tell exactly where it stops. That's what I do if not using ICD. I have a serial VFD that I hook up just for that purpose even when not using the serial port for the program itself.

thanks for reply,

my ReceivedMsg variable is enough to store a line, max line size is 22 char,
i tried to initialise msgChar variable before while loop as a '0' value, nothing changed

gets() function really doesnt work i dont know why? my strings already have CR+LF chars at the end

and i tried to sen some numbers before each line of program and, my program stops running exactly before the while loop in readline() function.

i am using classic a usb pic programmer not a ccs-icdu
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top