how to trim unknown first characters of string in code vision

青春壹個敷衍的年華 提交于 2020-02-23 06:56:05

问题


I set a mega16 (16bit AVR microcontroller) to receive data from the serial port

which is connected to Bluetooth module HC-05 for attaining an acceptable number

sent by my android app and an android application sends a number in the form of a

string array whose maximum length is equal to 10 digits. The problem arrives

while receiving data such that one or two unknown characters(?) exist at the

beginning of the received string. I have to remove these unknown characters from

the beginning of the string in the case of existence. this problem is just for HC-05. I mean I had no problem while sending numbers by

another microcontroller instead of android applications.

here is what I send by mobile:

"430102030405060\r"

and what is received in the serial port of microcontroller:

"??430102030405060\r" or "?430102030405060\r"

here is USART Receiver interrupt code:

//-------------------------------------------------------------------------

// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char status,data;
status=UCSRA;
data=UDR;

if (data==0x0D)
 {
  puts(ss);printf("\r")
  a=0;
  memset(ss, '\0', sizeof(ss));
  }
 else
 {
 ss[a]=data;
 a+=1;
 }     

if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer[rx_wr_index++]=data;
if RX_BUFFER_SIZE == 256
   // special case for receiver buffer size=256
   if (++rx_counter == 0) rx_buffer_overflow=1;
else
   if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      }
endif
   }
}

//-------------------------------------------------------------------------

how can I remove extra characters (?) from the beginning of received data in codevision?


回答1:


You do not need to remove them, just do not pass them to your processing. You either may test the data character before putting it into your line buffer (ss) or after the complete line was received look for the first relevant character and only pass the string starting from this position to your processing functions.

Var 1:

BOOL isGarbage(char c){
  return c<'0' || c > '9';
}

if (data==0x0D)
{
  puts(ss);printf("\r")
  a=0;
  memset(ss, '\0', sizeof(ss));
} else {
 if(!isGarbage(data))
 {
   ss[a]=data;
   a+=1;
 }
} 

Var2:


if (data==0x0D)
{
  const char* actualString = ss;
  while(isGarbage(*actualString )){
   actualString ++;
  }
  puts(actualString );printf("\r")
  a=0;
  memset(ss, '\0', sizeof(ss));
} else {
  ss[a]=data;
  a+=1;
} 

However:
maybe you should try to solve the issue in contrast to just fix the symptoms (suppress '?' characters).

What is the exact value of the questionable characters? I suspect, that '?' is only used to represent non printable data.

Maybe your interface configuration is wrong and the sender uses software flow control on the line and the suspicious characters are XON/XOFF bytes

One additional note:
You may run into trouble if you use more complex functions or even peripheral devices from your interrupt service routine (ISR).

I would strongly suggest to only fill buffers there and do all other stuff in the main loop. triggered by some volatile flags data buffers.

Also I do not get why you are using an additional buffer (ss) in the ISR, since it seems that there already is a RX-Buffer. The implementation looks like that there is a good RX-receive buffer implementation that should have some functions/possibilities to get the buffer contents within the main loop, so that you do not need to add your own code to the ISR.

Additional additional notes:

string array whose maximum length is equal to 10 digits.

I count more than that, I hope your ss array is larger than that and you also should consider the fact that something may go wrong on transmission and you get a lot more characters before the next '\n'. Currently you overwrite all your ram.



来源:https://stackoverflow.com/questions/59604765/how-to-trim-unknown-first-characters-of-string-in-code-vision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!