Hi,
I using AS1_OnRxChar() to read 1char. It work fine.
void AS1_OnRxChar(void)
{
AS1_TComData ReceiveChar;
// Write your code here ...
AS1_RecvChar(&ReceiveChar);
Switch(ReceiveChar)
{
case 'C':
Pin_RCK_Power = 0; // RCK POWER OFF
break;
case 'O':
Pin_RCK_Power = 1; // RCK POWER ON
break;
default:
break;
}
return;}
Now, I want to read more than 1 char. But it still read 1st char only.
void AS1_OnRxCharExt(AS1_TComData Chr)
{
uint8_tRxStatus;
uint16_t ReadSize=128, ReceiveLength, CompareCount, CheckInCount;static uint8_t CheckIn[10]= {0x2f, 0xae, 0x22, 0x1e, 0x8d, 0x01, 0x0d, 0x6e, 0xcb, 0xf0};
/* Write your code here ... */
ReceiveLength = AS1_GetCharsInRxBuf(); ==> Always return 1, Why?
RxStatus = AS1_RecvBlock(&Chr, ReadSize, &ReceiveLength); ==> Retuen 10 (ERR_RXEMPTY) Chr receive 1st char and ReceiveLength=1 Why?CompareCount=0;
CheckInCount=0;
while(CompareCount<10)
{
if(CheckIn[CompareCount++] == Chr++)
CheckInCount++;
else
CompareCount = 10;
}if(CompareCount == CheckInCount)
Pin_RCK_Power = 1; // RCK POWER ON}
Do you have any recommend?
Best Regards,
Aaron
Hi,
AS1_RecvBlock(&Chr, ReadSize, &ReceiveLength);
How to receive a string (not a char)? I can't understand that ReceiveLength always 1.
Best Regard,
Aaron
Hello Aaron,
You can use the method of " ASerialLdd1_ReceiveBlock()" to receive more than one char.
There is Typical usage :
Hope it helps
Have a great day,
TIC
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
I use interrupt for SCI transmitting and receiving a number of chars. I use a fixed packet format and read it into an array called RxData. Example....
/* *********** SCI Packet *************
RxData[0] = $ start char
RxData[1-2] = Packet size (Word)
Rxdata[3] = Command ID (byte)
and at the end
RxData[????] = Checksum (32bit)
When the whole packet is received the flag GotRx data is set. I call the function CheckCommsActivity(); every 10 msec to see if there is any data.
Below is how I do it...
//----------------------- SCI0 Rx/Tx Interrupt -----------------------------
interrupt void SCI0_ISR(void) {
static uint16_t pkt = 0;
static uint16_t x = 0;
static uint8_t Schar = FALSE; // Start char for comms
uint8_t RxByte;
static uint16_t i = 0;
unsigned char scicr2,scisr1; //prection against randomly flag clearing
scisr1 = SCI0SR1; // save status register actual status
scicr2 = SCI0CR2; // save control register actual status
//if receiver interrupt is enabled and corresponding interrupt flag is set
if((scicr2 & SCI0CR2_RIE_MASK) && ((scisr1 & (SCI0SR1_OR_MASK | SCI0SR1_RDRF_MASK)))) {
if(scisr1 & SCI0SR1_OR_MASK) { // if overrun error do nothing
(void)SCI0DRL; // clear interrupt flag
} else {
RxByte = SCI0DRL; // read received character + clear interrupt flag
switch (RxByte) {
case 0x24 : RxData[x++] = RxByte; // $ Start char
Schar = TRUE;
break;
default : if (Schar == TRUE) {
RxData[x++] = RxByte;
}
break;
} // End Switch
if (x == 3) {
pkt = (RxData[x - 2] << 8);
pkt = pkt + (RxByte & 0xFF);
}
if (x == pkt) { // Last byte received
x = 0;
pkt = 0;
Schar = FALSE;
gotRx = TRUE;
}
}
}
}
//------------------ Check Comms Activity ---------------------------
void CheckCommsActivity(void) {
uint8_t cmd;
if (gotRx == TRUE) {
cmd = RxData[3]; // Read the forth byte which is the command
switch (cmd) {
case 0x01 : sendRealTimeData();
break;
case 0x02 : receiveWholeMap();
break;
case 0x03 : WriteStructureToEEprom((uint8_t *)&cal, sizeof(cal)); // Lock changes into flash
sendReply(0x03,TRUE); // Okay
break;
case 0x04 : sendWholeMap();
break;
case 0x05 : changeOfValueRequested();
break;
case 0x06 : SetFpsMin();
break;
case 0x07 : SetFpsMax();
break;
case 0x08 : SetFuelTanksMin();
break;
case 0x09 : SetFuelTanksMax();
break;
}
gotRx = FALSE;
}
}
Hi, Ray,
Thanks for your reply. I'll try it later.
Our board connect to a instrument by RS232. The instrument sent data with rule as following:
- uint8_t Command_A[10]= {0x2f, 0xae, 0x22, 0x1e, 0x8d, 0x01, 0x0d, 0x6e, 0xcb, 0xf0};
- uint8_t Command_B[8]= {0x2f, 0xae, 0x31, 0x31, 0x30, 0x30, 0x30, 0x30};
- uint8_t Command_C[5]= {0x74, 0x75, 0x76, 0x77, 0x78};
- uint8_t Command_D[7]= {0x30, 0x74, 0x32, 0x33, 0x2A, 0x01, 0xf3};
We need to know the instrument sent command is A, B, C or D. The challenge is that we don't know received char was 1st, 2nd, 3rd or other char of command.
Best Regard,
Aaron