![](/img/trans.png)
[英]How can I send a string instead of a character through Serial with the MSP430? ( Code Composer Studio(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 包含某個字,然后微控制器應該 controller 它,例如,如果字符串cREC_BUFFER結尾包含單詞“ENDE”,他應該go進入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\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;
}
}
j= 0;
cREC_BUFFER[j++]=UCA0RXBUF;
每次調用中斷處理程序時,您都會重置j
。
僅當您期望新命令開始時才必須初始化j
。 (並且您必須確保緩沖區不會溢出。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.