How to send string instead of single variable

Thread Starter

mukesh1

Joined Mar 22, 2020
68
I want to send multiple strings to be sent instead of a single variable. How to send string instead of single variable char in the following code

C:
#define SBIT_TXEN     5
#define SBIT_SPEN     7
#define SBIT_CREN     4

void UART_Init(int baudRate)
{   
    TRISC=0x80;            // Configure Rx pin as input and Tx as output 
    TXSTA=(1<<SBIT_TXEN);  // Asynchronous mode, 8-bit data & enable transmitter
    RCSTA=(1<<SBIT_SPEN) | (1<<SBIT_CREN);  // Enable Serial Port and 8-bit continuous receive
    SPBRG = (20000000UL/(long)(64UL*baudRate))-1;      // baud rate @20Mhz Clock
}

void UART_TxChar(char ch)
{
    while(TXIF==0);    // Wait till the transmitter register becomes empty
    TXREG=ch;          // load the char to be transmitted into transmit reg
}


char UART_RxChar()
{
    while(RCIF==0);    // Wait till the data is received
    return(RCREG);     // Return the received data to calling function
}


int main()
{
    char ch = 'A';
    char a[]={"Welcome to Pic Serial Comm"};

    UART_Init(9600);          //Initialize the UART module with 9600 baud rate

    while(1)
    {
        UART_TxChar(ch);    // Transmit the received char
    }
}
 

BobaMosfet

Joined Jul 1, 2009
2,110
I want to send multiple strings to be sent instead of a single variable. How to send string instead of single variable char in the following code
...
@mukesh1
Have you actually read the datasheet for your PIC MCU? Normally manufacturer provides clear instructions on exactly how to use the UARTS.
 

Thread Starter

mukesh1

Joined Mar 22, 2020
68
Read the following link and note that it assumes your string is zero terminated. Watch for any register names that are different for whichever PIC you are using.
https://circuitdigest.com/microcontroller-projects/uart-communication-using-pic16f877a
Can you tell how the function is working in code from your link

Function take input as pointer. I don't know how string pass in function

C:
void UART_send_string(char* st_pt)
{
    while(*st_pt) //if there is a char
        UART_send_char(*st_pt++); //process it as a byte data
}
@BobaMosfet my question is not how uart works. I want a function that can transfer string because I have a function that transfers a one character and another function that one receives a character via uart simply
 

MrChips

Joined Oct 2, 2009
30,704
If you can send one character then you can send n characters.

Strings in the C programming language are packed one character at a time into memory. A null character (0 in binary) is added at the end to indicate the end of the string.

The statement while(*st_pt) checks each character. If it is not zero then it accepts it, otherwise the loop ends.

The character is fetched via (*st_pt) and is transmitted via the function UART_send_char( ).

The statement (*st_pt++) advances to the next character and the code loops back to the while(*st_pt) statement.
 

Papabravo

Joined Feb 24, 2006
21,157
We are checking value not size of string. I don't understand how while(*st_pt) check size of string
A "value" of '\0' is used to denote the end of the string. You never want to pass a 'string' to a function as that would be a waste of time and resources. You pass a copy of a pointer to a string, which the function can modify, to the function so the function can fetch the characters and output them to the hardware. Go back and read your tutorials on how strings are manipulated and how functions are called. You have a massive misconception of how strings are defined and manipulated.

To get the size of the string you count characters until you get the value '\0'. In practice you seldom, if ever, have to do that.
 

Thread Starter

mukesh1

Joined Mar 22, 2020
68
it makes more sense to me

C:
void UART_send_string(char* st_pt) {

   while(*st_pt  != '\0' ) // Check for the end of  string NULL

      UART_send_char(*st_pt);

      st_pt++;

}
 

MrChips

Joined Oct 2, 2009
30,704
while( boolean value) checks if a boolean value is true or false.

A zero value is used to represent false.
A non-zero value is used to represent true.

while(*st_pt != '\0' ) is doing the same thing as while(*st_pt).

UART_send_char(*st_pt);
sends the current character in the string.

UART_send_char(*st_pt++); uses the post-increment operator. It points to the next character after sending the current charactre. Get accustomed to using it.
 

click_here

Joined Sep 22, 2020
548
it makes more sense to me

C:
void UART_send_string(char* st_pt) {

   while(*st_pt  != '\0' ) // Check for the end of  string NULL

      UART_send_char(*st_pt);

      st_pt++;

}
There is nothing wrong with making the code more readable for yourself (as the assembly code probably won't end up any different), but you have inserted a bug.

You have not used {} after your while statement, which means that "st_pt++" never gets called.

It looks like you have tried to used your indentation to create your loop body, but it won't work in C.

This bug is known as the "goto fail" bug after a famous bug in Apple's iOS system (see here)


Hope this helps
 

BobaMosfet

Joined Jul 1, 2009
2,110
Can you tell how the function is working in code from your link

Function take input as pointer. I don't know how string pass in function

C:
void UART_send_string(char* st_pt)
{
    while(*st_pt) //if there is a char
        UART_send_char(*st_pt++); //process it as a byte data
}
@BobaMosfet my question is not how uart works. I want a function that can transfer string because I have a function that transfers a one character and another function that one receives a character via uart simply
@mukesh1 I understood your question. The point is, if you understand the uart, you should be able to _easily_ figure out how to send more than 1 character. There's no magic. You send one character at a time using the uart, until you have sent a string.

You pass the address of the first character (which is called a pointer) of the string to your function, and then the function reads each character, sends it, increments the pointer, and repeats until the null character is seen, at which is stops and returns.
 
Top