为什么我的字符串变量 cREC_BUFFER 只得到最后一个字符而不是我输入终端的整个字符串?(MSP430 for C Language)
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)
我正在尝试通过 UART 向终端发送一些命令,因此为了让 MSP430 知道他收到了哪个命令,我写了一些 if 条件以防 cREC_BUFFER 包含某个单词,然后微控制器应该控制它,例如如果字符串 cREC_BUFFER 末尾包含单词“ENDE”,他应该进入里面的 if 条件。
我面临的问题是,当我在调试后检查字符串空字符串 cREC_BUFFER 的内容时,它只包含单词“ENDE”的最后一个字符“E”。
有人可以告诉我我在这里犯了什么错误吗?
非常感谢您的提前帮助!
(我通过删除其他功能的内容来减少此处代码的长度,因为它们不会导致问题)
#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[=11=]") == 0)
{
iGLOBAL = BEFEHL_1;
}
else if (strcmp(cREC_BUFFER,"START:LOGIN[=11=]"))
{
iGLOBAL = BEFEHL_2;
}
else if (strcmp(cREC_BUFFER,"START:WRITE[=11=]") == 0)
{
iGLOBAL = BEFEHL_3;
}
else if (strcmp(cREC_BUFFER,"START:PROTECT[=11=]") == 0)
{
iGLOBAL = BEFEHL_4;
}
else if (strstr(cREC_BUFFER,"START:INPUT_PW_[=11=]") == 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;
}
}
j= 0;
cREC_BUFFER[j++]=UCA0RXBUF;
每次调用中断处理程序时,您都在重置 j
。
只有当您希望新命令开始时,您才必须初始化 j
。 (并且必须保证缓冲区不溢出。)
我正在尝试通过 UART 向终端发送一些命令,因此为了让 MSP430 知道他收到了哪个命令,我写了一些 if 条件以防 cREC_BUFFER 包含某个单词,然后微控制器应该控制它,例如如果字符串 cREC_BUFFER 末尾包含单词“ENDE”,他应该进入里面的 if 条件。 我面临的问题是,当我在调试后检查字符串空字符串 cREC_BUFFER 的内容时,它只包含单词“ENDE”的最后一个字符“E”。 有人可以告诉我我在这里犯了什么错误吗? 非常感谢您的提前帮助! (我通过删除其他功能的内容来减少此处代码的长度,因为它们不会导致问题)
#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[=11=]") == 0)
{
iGLOBAL = BEFEHL_1;
}
else if (strcmp(cREC_BUFFER,"START:LOGIN[=11=]"))
{
iGLOBAL = BEFEHL_2;
}
else if (strcmp(cREC_BUFFER,"START:WRITE[=11=]") == 0)
{
iGLOBAL = BEFEHL_3;
}
else if (strcmp(cREC_BUFFER,"START:PROTECT[=11=]") == 0)
{
iGLOBAL = BEFEHL_4;
}
else if (strstr(cREC_BUFFER,"START:INPUT_PW_[=11=]") == 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;
}
}
j= 0;
cREC_BUFFER[j++]=UCA0RXBUF;
每次调用中断处理程序时,您都在重置 j
。
只有当您希望新命令开始时,您才必须初始化 j
。 (并且必须保证缓冲区不溢出。)