cmsp430

Why does my string variable cREC_BUFFER get only the last character instead of the whole string that I put into the terminal?(MSP430 for C Language)


I am trying to send some commands to a Terminal through UART, so in order for the MSP430 to know which command he got, I wrote some if-conditions in case cREC_BUFFER contains a certain word, the microcontroller should controller it then, for example if the string cREC_BUFFER contains the word "ENDE" at the end, he should go into the if condition inside. The problem that I am facing, is that when I check what the string empty string cREC_BUFFER has after debugging, it contains only the last character "E" of the word "ENDE". Can someone tell me what mistakes I am making here? Thanks a lot for the help in advance! (I reduced the length of the code in here by deleting the content of the other functions, since they do not cause the problem)


#include "nfc_app.h"
#include "trf79xxa.h"

//===============================================================
extern uint8_t g_pui8TrfBuffer[NFC_FIFO_SIZE];
static volatile tTrfStatus g_sTrfStatus;
bool UART_Not_Recieved = true;
uint8_t RX = 0x00;
uint8_t Data_Byte[4]= {0x00, 0x00, 0x00, 0x00};
uint8_t Block_Number= 0x00;
bool Write_Data = true;
bool New_Password = true;
bool Change_Protect_Status = true;
uint8_t Login_Password_Byte[4] = {0x00, 0x00, 0x00, 0x00};
uint8_t New_Password_Byte[4] = {0x00, 0x00, 0x00, 0x00};
uint8_t  ui8LoopCount = 0;       // ensure that the VICCs are ready to receive it. (ISO15693-3)

uint8_t Start_Page = 0x00;
uint8_t End_Page = 0x00;

uint8_t Protection_Status = 0x00;

const char cString[100] = {};
char cREC_BUFFER[100] = {};
unsigned int j = 0; //Counter


#define BEFEHL_1    1
#define BEFEHL_2    2
#define BEFEHL_3    3
#define BEFEHL_4    4
#define BEFEHL_5    5

#define STARTTAG    "START:"
#define ENDTAG      ":ENDE"


static uint8_t g_pui8Iso15693UId[8];
void AskForWriteOperation(void)
{
}
void AskForProtectStatus(void)
{
}
void AskForNewPassword(void)
{
    
}
void AskForPassword(void)
{
     
}
void ProtectPage(uint8_t Protect_Status, uint8_t Page)
{
    
}
void Login(uint8_t Byte1, uint8_t Byte2, uint8_t Byte3, uint8_t Byte4)
{
   
  }

void writePassword(uint8_t Byte1, uint8_t Byte2, uint8_t Byte3, uint8_t Byte4)
{
    
}
uint8_t My_sendWriteSingleBlock(uint8_t ui8BlockNumber, uint8_t Byte1, uint8_t Byte2, uint8_t Byte3, uint8_t Byte4)
{
     
}


uint8_t My_sendReadMultipleBlocks(uint8_t ui8ReqFlag, uint8_t ui8FirstBlock, uint8_t ui8NumberOfBlocks)
{
    
}
uint8_t My_sendReadSingleBlock(uint8_t ui8ReqFlag, uint8_t ui8BlockNumber)
{
   
}

void Send_UID(void)
{
  
}




void ISO15693_SingleInventory(void)
{
}

int iGLOBAL;

void main(void)
{
    uint8_t ui8VLOCalibCount = 0;
    uint8_t i = 0x00;
//  TODO: Remove LED2 Jumper on G2 LaunchPad if using it, otherwise SPI will not work.

    // Stop the Watchdog timer,

    iGLOBAL = 0;
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR |= BIT0;
    P1OUT &= ~BIT0;
    // Select DCO to be 8 MHz
    MCU_initClock();
    MCU_delayMillisecond(10);

    // Calibrate VLO
    MCU_calculateVLOFreq();

    // Set the SPI SS high
    SLAVE_SELECT_PORT_SET;
    SLAVE_SELECT_HIGH;

    // Four millisecond delay between bringing SS high and then EN high per TRF7970A Datasheet
    MCU_delayMillisecond(4);

    // Set TRF Enable Pin high
    TRF_ENABLE_SET;
    TRF_ENABLE;

    // Wait until TRF system clock started
    MCU_delayMillisecond(5);

    // Set up TRF initial settings
    TRF79xxA_initialSettings();
    TRF79xxA_setTrfPowerSetting(TRF79xxA_3V_FULL_POWER);

#ifdef ENABLE_HOST
    // Set up UART
    UART_setup();
    IE2 |= UCA0RXIE;
    _BIS_SR(GIE);
#endif

    // Initialize all enabled technology layers
    NFC_init();



    // Enable global interrupts
    __bis_SR_register(GIE);

    // Enable IRQ Pin
    IRQ_ON;

    TRF79xxA_setupInitiator(0x02);

          // The VCD should wait at least 1 ms after it activated the
            // powering field before sending the first request, to
    while(1)
    {
       switch(iGLOBAL)
        {
            case 0:         //DO NOTHING
                            break;

            case BEFEHL_1:  WritePW();  //
                            iGLOBAL = 0;
                            break;
            case BEFEHL_2:  Anmeldung();
                            iGLOBAL = 0;
                            break;
            case BEFEHL_3:  WriteDATA();
                            iGLOBAL = 0;
                            break;

            case BEFEHL_4:  ProtectDATA();
                            iGLOBAL = 0;
                            break;

            case BEFEHL_5:  ChangePW();
                            iGLOBAL = 0;
                            break;
        }
    }


void WritePW (void)
{
    AskForPassword();
    uint8_t  ui8LoopCount = 0;       // ensure that the VICCs are ready to receive it. (ISO15693-3)
    MCU_delayMillisecond(20);
    ISO15693_resetTagCount();
    ISO15693_SingleInventory();
    Send_UID();
}
void Anmeldung(void)
{
    Login(Login_Password_Byte[0], Login_Password_Byte[1], Login_Password_Byte[2], Login_Password_Byte[3]);
    MCU_delayMillisecond(20);
    }
void WriteDATA(void)
{
    AskForWriteOperation();
                if(Write_Data == true)
                {
                My_sendWriteSingleBlock(Block_Number, Data_Byte[0], Data_Byte[1], Data_Byte[2], Data_Byte[3]);
                }
    }
void ProtectDATA(void)
{
    uint8_t i = 0x00;
    if(Change_Protect_Status)
               { AskForProtectStatus();
               }
                if(Change_Protect_Status)
                {
                for( i = Start_Page; i <= End_Page; i++)
                {
                ProtectPage(Protection_Status, i);
                MCU_delayMillisecond(20);
                }
                }
                MCU_delayMillisecond(20);
    }
void ChangePW(void)
{
    uint8_t ui8VLOCalibCount = 0;
    if(New_Password)
               {
               AskForNewPassword();
               }
               if(New_Password)
               {
              writePassword( New_Password_Byte[0], New_Password_Byte[1], New_Password_Byte[2], New_Password_Byte[3]);
               }
              // My_sendReadSingleBlock(0x22, 0x04);
              // My_sendReadMultipleBlocks(0x22,0x00,25);
              //My_sendWriteSingleBlock(0x22, 0x04, 0x04);
              // ISO15693_sendSingleSlotInventory();                           // Send a single slot inventory request to try and detect a single ISO15693 Tag

               for (ui8LoopCount = 0; ui8LoopCount < 8; ui8LoopCount++)
                                                               {
                                                                  g_pui8Iso15693UId[ui8LoopCount] = 0;        // Send UID to host
                                                               }

           // VLO drifts with temperature and over time, so it must be periodically recalibrated
           // Calibrate the VLO every 25 passes of the NFC polling routine
           ui8VLOCalibCount++;
           if (ui8VLOCalibCount == 25)
           {
               // Calibrate VLO
               MCU_calculateVLOFreq();
               // Reset Calibration Counter
               ui8VLOCalibCount = 0;

               }
    }
#pragma vector = USCIAB0RX_VECTOR
__interrupt void ReceiveInterrupt(void)
{
    j= 0;
    cREC_BUFFER[j++]=UCA0RXBUF;

    char *pch = strstr(cREC_BUFFER, "ENDE");

    if(pch)
    {
        if (strcmp(cREC_BUFFER,"START:PW\0") == 0)
        {
            iGLOBAL = BEFEHL_1;
        }
        else if (strcmp(cREC_BUFFER,"START:LOGIN\0"))
        {
            iGLOBAL = BEFEHL_2;
        }
        else if (strcmp(cREC_BUFFER,"START:WRITE\0") == 0)
        {
            iGLOBAL = BEFEHL_3;
        }
        else if (strcmp(cREC_BUFFER,"START:PROTECT\0") == 0)
        {
            iGLOBAL = BEFEHL_4;
        }
        else if (strstr(cREC_BUFFER,"START:INPUT_PW_\0") == 1)
        {
            iGLOBAL = BEFEHL_5;
            //START:INPUT_PW_dddd:ENDE
            //START:INPUT_PW_a1234:ENDE
            // Passwort raus holen und der switch case zur Verfügung stellen
        }
        else
        {
            strcpy(cString,"WAS WILLST DU VON DEM MSP WISSEN? ICH KENNE DEN BEFEHL NICHT!");
            int i = 0;
            UC0IE |= UCA0TXIE; // Enable USCI_A0 TX interrupt
            UCA0TXBUF = cString[i++];
        }
        memset(cREC_BUFFER,0,sizeof(cREC_BUFFER));
        j = 0;
    }
}






Solution

  •   j= 0;
      cREC_BUFFER[j++]=UCA0RXBUF;
    

    You are resetting j every time the interrupt handler is called.

    You must initialize j only when you expect a new command to begin. (And you must ensure that the buffer does not overflow.)