View previous topic :: View next topic |
Author |
Message |
hemnath
Joined: 03 Oct 2012 Posts: 242 Location: chennai
|
how to receive string from PC using RS232 |
Posted: Wed Jul 22, 2020 10:41 pm |
|
|
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
|
|
Posted: Thu Jul 23, 2020 2:49 am |
|
|
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
|
|
Posted: Wed Jul 29, 2020 5:37 am |
|
|
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 |
|
|
|
|