Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arduino CDC丢弃数据_Arduino_Cdc - Fatal编程技术网

Arduino CDC丢弃数据

Arduino CDC丢弃数据,arduino,cdc,Arduino,Cdc,我正在尝试使用带有ATmega32U4的Serial.write(buff,len)(通过USB虚拟串行端口)发送大量数据。我最初是用LUFA编写应用程序的,它工作得很好。然而,我现在把它移植到Arduino,它正在删除数据 有关守则如下: void uartTask() { static unsigned long timer = 0; if (Serial) { // does the data have somewhere to go? uint16_t ct = Ring

我正在尝试使用带有ATmega32U4的Serial.write(buff,len)(通过USB虚拟串行端口)发送大量数据。我最初是用LUFA编写应用程序的,它工作得很好。然而,我现在把它移植到Arduino,它正在删除数据

有关守则如下:

void uartTask() {
  static unsigned long timer = 0;
  if (Serial) { // does the data have somewhere to go?
    uint16_t ct = RingBuffer_GetCount(&serialBuffer);
    unsigned long curTime = millis();
    if (ct > 64 || (timer-curTime > 100)) { // is there data to send?
      timer = curTime;
      if (serialBuffer.Out + ct <= serialBuffer.End) { // does it loop in our buffer?
        ct = Serial.write(serialBuffer.Out, ct); // dump all the date
        serialBuffer.Out += ct;
        if (serialBuffer.Out == serialBuffer.End)
          serialBuffer.Out = serialBuffer.Start; // loop the buffer
      } 
      else { // it looped the ring buffer
        uint8_t* loopend = serialBuffer.Out + ct;
        uint16_t ct2 = loopend - serialBuffer.End;
        uint16_t ct1 = ct - ct2;
        uint16_t ct1s = Serial.write(serialBuffer.Out, ct1); // dump first block
        if (ct1s == ct1) {
          ct2 = Serial.write(serialBuffer.Start, ct2); // dump second block
          serialBuffer.Out = serialBuffer.Start + ct2; // update the pointers
          ct = ct1+ct2;
        } 
        else {
          ct = ct1s;
          serialBuffer.Out += ct;
        }
      }

        uint_reg_t CurrentGlobalInt = GetGlobalInterruptMask();
        GlobalInterruptDisable();

        serialBuffer.Count -= ct; // update the count

        SetGlobalInterruptMask(CurrentGlobalInt);

    }

    if (RingBuffer_GetCount(&serialBuffer) < 256) {
      SET(TX_BUSY, LOW); // re-enable the serial port
      serialRXEnable();
    }

    int16_t w;
    while ((w = Serial.read()) >= 0) {
      Serial_SendByte(w);
    }
  }
}

ISR(USART1_RX_vect) { // new serial data!
  RingBuffer_Insert(&serialBuffer, UDR1 ); // save it
  if (serialBuffer.Count > 1000) // are we almost out of space?
    SET(TX_BUSY, HIGH); // signal we can't take any more
  if (serialBuffer.Count > 1020) 
    serialRXDisable(); // if our flag is ignored disable the serial port so it doesn't clog things up
}
void uartask(){
静态无符号长定时器=0;
如果(串行){//数据是否有去向?
uint16\u t ct=RingBuffer\u GetCount(&serialBuffer);
无符号长压缩时间=毫秒();
如果(ct>64 | |(定时器curTime>100)){//有数据要发送吗?
定时器=缩短时间;
if(serialBuffer.Out+ct=0){
串行发送字节(w);
}
}
}
ISR(USART1_RX_vect){//新的串行数据!
RingBuffer_Insert(&serialBuffer,UDR1);//保存它
如果(serialBuffer.Count>1000)//我们的空间快用完了吗?
设置(TX_忙,高);//信号我们不能再接收了
如果(serialBuffer.Count>1020)
serialRXDisable();//如果忽略我们的标志,请禁用串行端口,以免阻塞
}
我使用了与LUFA几乎相同的代码,它从不丢弃数据。我在我的电脑上运行Ubuntu

这基本上只是一个USB到串行转换器。当串行数据到达时,它触发一个中断,将数据放入一个环形缓冲区,然后通过USB读取和发送

我不明白为什么基于LUFA的代码可以工作,但Arduino却不行。我认为检查Serial.write()的返回值可以让您知道数据是否一直都不可用


知道发生了什么吗?

你确定只有一个程序打开了串行设备吗?是的,当我使用独立代码而不是Arduino IDE时,它可以完美地工作。如果我将缓冲区大小减少到256,并应用错误修复,它似乎可以工作。但是,对于较大的缓冲区,它仍然会失败。