如何在代码视觉中修剪字符串的未知第一个字符 [英] how to trim unknown first characters of string in code vision

查看:157
本文介绍了如何在代码视觉中修剪字符串的未知第一个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了mega16(16位AVR微控制器)以从串行端口接收数据

已连接到蓝牙模块HC-05以获得可接受的数量

由我的android应用发送,并且android应用发送

形式的数字

最大长度等于10位数字的字符串数组.问题到了

接收数据时使得

处存在一个或两个未知字符(?)

开始接收的字符串.我必须从

中删除这些未知字符

在存在的情况下字符串的开头. 此问题仅适用于HC-05.我的意思是,通过

发送号码时我没有问题

另一个微控制器,而不是android应用程序.

这是我通过手机发送的内容:

"430102030405060 \ r"

以及微控制器的串行端口中收到的内容:

"?? 430102030405060 \ r" 或者 ?430102030405060 \ r"

这是USART接收器中断代码:

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

// 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
   }
}

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

如何从codevision中接收到的数据开头删除多余的字符(?)?

解决方案

您不需要删除它们,只需将它们不传递给您的处理即可. 您可以在将data字符放入行缓冲区(ss)之前进行测试,或者在收到整行之后查找第一个相关字符,然后仅将从该位置开始的字符串传递给处理函数.

变量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;
} 

但是:
相比之下,也许您应该尝试解决问题以仅解决症状(禁止'?'字符).

可疑字符的确切值是多少?我怀疑那是?"仅用于表示不可打印的数据.

也许您的接口配置错误,并且发送方在线路上使用软件流控制,并且可疑字符为XON/XOFF字节

一个附加说明:
如果您在中断服务程序(ISR)中使用更复杂的功能甚至外围设备,则可能会遇到麻烦.

我强烈建议仅在此处填充缓冲区,并在主循环中执行所有其他操作.由某些易失性标志数据缓冲区触发.

我也不明白为什么在ISR中使用附加的缓冲区(ss),因为似乎已经存在一个RX-Buffer.该实现看起来好像有一个不错的RX-receive缓冲区实现,该实现应该具有一些功能/可能性来在主循环中获取缓冲区内容,因此您无需向ISR添加自己的代码.

其他补充说明:

最大长度等于10位数字的字符串数组.

我的计算更多,希望您的ss数组大于该值,并且您还应该考虑以下事实:传输时可能会出问题,并且在下一个'\n'之前您会得到更多的字符.当前,您覆盖了所有的内存.

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?

解决方案

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.

这篇关于如何在代码视觉中修剪字符串的未知第一个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆