'Sending a text string out a serial port in C without using printf

I'm writing 8-bit PIC microcontroller C code in Microchip's MPLAB X tool using their XC8 compiler for a demo / evaluation board for a sensor IC. The PIC I'm using has multiple serial ports, so I'm using UART2 to output only sensor data. I wanted to be able to format the output data for hex or decimal so I have stdio redirected to this port. The Rx side of UART2 is unused. UART1 is used for command and control. This port needs to be able to output ASCII messages such as "Enter the register address:". The sensor is highly configurable, so my code initializes it with default values, but the user can halt sampling, read and write registers and re-start sampling. Since I can't use printf when writing to UART1, I had to find another way to do this. Here's an example of what I'm doing. The file is large so I'm just cutting and pasting the relevent stuff here:

char msgString[64];
switch(command)
    { 
    case (READCMD):    // Read command
        {
        if (SERIAL_STATE_IDLE == serialPortState)
            {
            strcpy (msgString, "Input Read Address Hex Value 00-18");
            Display_PrintMessage();
            Display_PrintChar(prompt);
            serialPortState = SERIAL_STATE_READ;
            }
...
    
void Display_PrintMessage(void)
    {
    msgLength=strlen(msgString);
    for (i=0; i<msgLength; ++i)
        {
            UART1_Write(msgString[i]);
        }
    Display_PrintChar(newLine);
    }

void Display_PrintChar(char c)
    {
    UART1_Write(c);
    }

I've verified that the port is working by writing some characters out to it early in main and they do appear on my terminal. I think the problem is with how I'm using the strcpy function. In the MPLAB debugger it appears that msgString is still all null characters after the strcpy executes. The strlen function may see msgString as zero length, hence, nothing prints out.

I would love to hear what you think may be wrong with my code, or, please suggest another way of accomplishing what I need.

Thanks in advance for any and all responses, they are much appreciated.

CobraRGuy



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source