CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

how to receive string from PC using RS232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hemnath



Joined: 03 Oct 2012
Posts: 242
Location: chennai

View user's profile Send private message

how to receive string from PC using RS232
PostPosted: Wed Jul 22, 2020 10:41 pm     Reply with quote

Hi,

Code:

#include "18F2520.h"
#fuses INTRC_IO
#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=pin_c6,rcv=pin_c7,bits=8, Errors, stream=PC) 

void main()
{
   float value = 32.000;
   int i = 0, j;
   char unit[] = "BAR";
   printf("Enter 1 or 2 to display the following\n\r");
   printf("1. PROCESS VALUE\n\r");
   printf("2. UNITS\n\r");

   while(1)
   {
      i = 0;   
            
      if(kbhit())
      {
         j = getc();
         if(j == '1')
         {
            printf("Process Value: %8f\n\r",value);
         }
      
         else if(j == '2')
         {
            while(unit[i] != '\0')
            {
               putc(unit[i]);                            // Write character
               i++;
            }
            printf("\n\r");   
         }
         else
         {
            putc(j);
            printf("\r\nBAD COMMAND\n\r");
         }
      }
   }
}


This code is working good with receiving a character. How to receive string?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jul 23, 2020 2:49 am     Reply with quote

Examples supplied by CCS shows you how to do it.

You've been around for eight years, you should not need to be asking.

Mike
mdemuth



Joined: 16 Apr 2007
Posts: 71
Location: Stuttgart, Germany

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 29, 2020 5:37 am     Reply with quote

Code:
// Receive data
   for (rx_buffid=0; rx_buffid < RX_BUFFER_LENGTH; rx_buffid++) rx_buffer[rx_buffid]=0; // empty the buffer
   rx_complete = false;
   while (rx_complete == false)
    {
    if (kbhit())
      {
      rx_character = getc();      // Character into buffer
      if (rx_character == '*' || rx_character == '#') rx_buffid = 0;   // start identifier '*' = reset pointer
      else  rx_buffid++; // increment buffer pointer
      if (rx_buffid > RX_BUFFER_LENGTH) rx_buffid = RX_BUFFER_LENGTH;
      rx_buffer[rx_buffid] = rx_character; // prevent buffer overflow
      if (rx_character == 0x0d) rx_complete = true; // CR => end of telegram, start analysis
      }
    else
      {
      // do something else...maybe increment a timeout counter.....
      }
    restart_wdt();   
    }

// Interpretation
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group