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.

[SOLVED] receive string from hyper terminal using uart

Status
Not open for further replies.

PRABAKARDEVA

Full Member level 2
Joined
Sep 16, 2013
Messages
127
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Location
Chennai
Activity points
2,168
hello


I am using pic 16f877a.i had written a program for interfacing pc with micro.

I can transmit a data to pc using uart and the data which can be displayed in hyperterminal.

I can receive a character from hyper terminal(pc) depend upon that character ,program can make the led to glow(something will happen).


i have to receive group of characters from hyper terminal(pc) based on that string something has to be happened.(run motor).

But the problem is that i couldn't receive the string from hyperterminal.I can receive only one character.

how to receive the string from hyperterminal?


Is there any solution is possible?help me


Thanks in Advance
 
Last edited:

You can use some other terminal software like Realterm or Docklight, could be very easy.

Or you can make a text file (testString.txt in desktop or project folder) with all your string and use
'Send Text File' menu in Hyper terminal and select your file and send. Now you will receive all your strings in controller.
 

hello,


you need to use a buffer to receive all char of the string
and have a terminator at the end of the string
it could be 0 (zero value) or CR=13=0x0D carraiage return (Enter on your keyboard!)
or other ,but particular ( ex: &,#,@)
to be able to reconize ..that is a complete string..

then ypu analyse the buffer to decide what action to do....
 

hello,


you need to use a buffer to receive all char of the string
and have a terminator at the end of the string
it could be 0 (zero value) or CR=13=0x0D carraiage return (Enter on your keyboard!)
or other ,but particular ( ex: &,#,@)
to be able to reconize ..that is a complete string..

then ypu analyse the buffer to decide what action to do....

hi paul

In which buffer i have to use?can u tell me clearly
 

But the problem is that i couldn't receive the string from hyperterminal.I can receive only one character.

how to receive the string from hyperterminal?

What exactly your problem , If you explain clearly then only someone can help you proper way.
 

What exactly your problem , If you explain clearly then only someone can help you proper way.

I don't know that how to send group of characters from pc to microcontroller serially.(this is my problem)

But i can send single character to microcontroller,depend upon that character,had written program like running motors(anything else).
 

in the case if assume what paul gave is the problem you looking to solve,

the buffer means an array variable str[10]. You have to write code to store each character you receive from hyperterminal by incrementing the element
eg:
if(charreceived!=0x0d)
{
str =charreceived;
i++;
}//Increment this i until you receive the terminator as stated by paul can be a carriage return '0x0d'

i think this will help you to understand

- - - Updated - - -

I don't know that how to send group of characters from pc to microcontroller serially.(this is my problem)

did you followed the https://www.edaboard.com/threads/316055/#post1351553
 

in the case if assume what paul gave is the problem you looking to solve,

the buffer means an array variable str[10]. You have to write code to store each character you receive from hyperterminal by incrementing the element
eg:
if(charreceived!=0x0d)
{
str =charreceived;
i++;
}//Increment this i until you receive the terminator as stated by paul can be a carriage return '0x0d'

i think this will help you to understand

- - - Updated - - -



did you followed the https://www.edaboard.com/threads/316055/#post1351553


yeah i followed....
i had written isr that will be attached below..

kindly take a look at that program..
 

Attachments

  • isr.rar
    359 bytes · Views: 114

i saw your isr coding in this coding when u receive A then all command store in array right but i don't understand completely what u want please give me a more detail bro!!!

and also there is not any flag cleared in isr routine u must clear inturrupt flag before u receive any char.
 

hi nick.

I don't know that how to send group of characters from pc to microcontroller serially....

why should i clear flag in isr...can u explain that clearly bro..
 

RCIF interrupt flag bit will clear automatically that flag bit is read only bit.that couldn't be cleared by software.

i want to receive predefined group of characters like switch_on and compare those characters with declared string,further action will be happened.


Actually i had tried in many ways.but i couldn't store the strings in buffer.
 

To receive a character from console, you should have some functions like uartgetchar();
Similarly, do u have the function uartgetstring(); ?
 

hello


PHP:
unsigned char string_buffer[12];   
// string_bit is not an appropriate mnemonique ..no link with a bit , but with bytes to form a string

volatile int i;
volatile int Get_Command_Flag;


void interrupt ISR(void)
{
    unsigned char c1;  // local variable
    if(RCIE & RCIF)   //If UART Rx Interrupt
    {
        if(OERR) //if over run error, then reset the receiver
        {
            CREN = 0;
            CREN = 1;
        }
        c1=RCREG;
        if(c1!=13) // test if Carriage return at the end of the commande
        {
            string_buffer[i]=c1;
            uart_putc(c1); // local echo
            i++;    // increase index of buffer
            }
            else
            {
             // we get CR as end of command
            string_bit[i]=0;  // put zero as mark for end of string
            Get_command_Flag=1; // arm a flag
            RCIE=0; // stop any UART receive interrupt
        }    
    }
}


// suppose you write the command switch_on<CR> on keyboard of PC

vois main()
{

i=0;
Get_Command_Flag;
string_buffer[0]=0;  // empty string
..... init UART 
RCIE=1;
GIE=1
...
// inside the main loop of your application
do
{
if (Get_Command_Flag==1) // you receive a message 
 {
 if ((string_buffer[0]=='s')&&(string_buffer[1]=='w')&&( string_buffer[8]=='N')&& (strlen(string_buffer)==9))
 {
  // case switch_on<CR>  as command
   PORTAbits.RA4=1;  // Lampe ON 
  }
  else
  {
   if ((string_buffer[0]=='s')&&(string_buffer[1]=='w')&&( string_buffer[7]=='f')&s (strlen(string_buffer)==10))
   {
    // case switch_off<CR>  as command
    PORTAbits.RA4=0;  // Lampe OFF 
   } 
  }
 // RAZ last command
i=0;
Get_Command_Flag=0; 
string_buffer[0]=0;
RCIF=0;
RCIE=1; // re-enable UART RX interrupt
}
....
.. etc 
while(1);


It is not necessary to compare all bytes of the command ,but maybe some distinctif letter inside the command
and also the lengh of the command are enough
it's depend of the safety level do you need
for more safety: test all bytes and add a checsum + send an acknowledge ..etc .. more complicated
In summary, define a protocole to exchange order and feedback..

What compiler you are using ?
with MikroC you can use MEMCMP to compare all the buffer with command
 
Last edited by a moderator:
Thanks paul.

i am using xc8 compiler in mplab ide..

can strcmp is used to compare two strings?

I edited my coding.when i press 'enter' key always it showing error in hyper terminal..

can u please fixed that code attached below?
 

Attachments

  • uart.rar
    688 bytes · Views: 105
Last edited:

You can use some other terminal software like Realterm or Docklight, could be very easy.

Or you can make a text file (testString.txt in desktop or project folder) with all your string and use
'Send Text File' menu in Hyper terminal and select your file and send. Now you will receive all your strings in controller.

Dear Friend,

Were you able to solve your problem? Cos I am also facing same problem and looking for a solution.

Can you help me? I will post my code here and can you look into it and tell me what is wrong?

I am using PIC16f690 and MPLABX IDE on Ubuntu. My aim is to just send a text from terminal and the PIC should toggle some LEDs.


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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <xc.h>
#include <pic.h>
#include <string.h>
#include <conio.h>
#include <stdlib.h>
#include <htc.h>
 
//#include <usart.h>
//#include <sw_uart.h>
 
#include <pic16f690.h>
//#include "usart_pic16.h"
 
#pragma config FOSC  =  INTRCCLK
#define _XTAL_FREQ      4000000
#define BAUDRATE        115200
#define BUFFER_SIZE     20
 
void initUART();
void toggleLED();
void sendStringUART(char*);
void sendByteUART(char byte);
char receiveByteUART();
void receiveStringUART();
 
char LEDON1[] = "SET LED1 ON";
 
int r, index;
char buffer[BUFFER_SIZE];
char x ,a;
 
void main()
{    
      initUART();      
      while(1){
          TXREG = 0x35;
         // sendStringUART("Hello");
      }
}
 
void initUART()
{
    //configure PORTC for output
    TRISC    = 0xFF;
    TRISC0   = 0x00;
    TRISC1   = 0x00;
    TRISC2   = 0x00;
    TRISC3   = 0x00;
    TRISB    = 0xFF;
    ANSELH   = 0x00;
    SPBRG    = (_XTAL_FREQ / (16 * BAUDRATE)) - 1;
 
    BRGH    = 1;
    SYNC    = 0;
    SPEN    = 1;
    CREN    = 1;
    SREN    = 0;
    TX9     = 0;
    RX9     = 0;
    TXEN    = 0;
    TXEN    = 1;
 
    GIE     = 1;
    PEIE    = 1;
    RCIE    = 1;
    //TXREG   = 'B';
}
 
void interrupt isr(void)
{
    //check the source of the interrupt
    if (RCIE && RCIF)
    {
       if(index < BUFFER_SIZE)
        {
           buffer[index] = RCREG;                                               //read the byte from rx register
            if(buffer[index] == 0x0D) //check for return key
            {
                PORTC = 0xFF;
                for(; index>0; index--)
                    buffer[index] = 0x00; //clear the array
                index=0; //for sanity
                return;
            }
            index++;
            
        }
       else
        {
            
             for(;index>0;index--)
                    buffer[index] = 0x00;
             index  =   0; //for sanity
             return;
        }       
    }
}

 
Last edited by a moderator:

PIC used ? You won't get 115200 bps baudrate when using 4 MHz Clock.
 

PIC used ? You won't get 115200 bps baudrate when using 4 MHz Clock.

Dear Milan,

Thanks for your reply. I am also confused with that baud rate and clock selection. Can you please help me with that also? I don't think I have any external oscillator with me. I am just using a PICKIT2 with a PIC Low Pin Count Demo Board.

Regards,
G Hariprasad
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top