include_二世 发表于 2014-8-7 11:15:01

请教一下arduino串口通信的问题!

RT,想请教一下arduino串口通信的问题,在网上查资料说,arduino1.0以前 清除串口缓存的函数是Serial.flush(),但是1.0之后这个函数已经改变了,我想问一下现在还有没有其他的函数能够清除串口缓存呢?

include_二世 发表于 2014-8-7 11:15:34

顶一下!~~~~:lol:lol:lol:lol:lol:lol:lol

include_二世 发表于 2014-8-7 11:35:10

New (Arduino 1.0+ Serial.Flush()) void HardwareSerial::flush()
    {
      while (_tx_buffer->head != _tx_buffer->tail)
      ;
    }
Old (Arduino 23- Serial.Flush())    void HardwareSerial::flush()
    {
      _rx_buffer->head = _rx_buffer->tail;
    }flush函数老版本和新版本的区别,但是这个玩意在哪里改呢?请教一下!~~~

Super169 发表于 2014-8-7 12:20:07

請問你用的是什麼版本?

v1.0.5:
void HardwareSerial::flush()
{
// UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
while (transmitting && ! (*_ucsra & _BV(TXC0)));
transmitting = false;
}

v1.5.6, v1.5.7:
void HardwareSerial::flush()
{
// If we have never written a byte, no need to flush. This special
// case is needed since there is no way to force the TXC (transmit
// complete) bit to 1 during initialization
if (!_written)
    return;

while (bit_is_set(*_ucsrb, UDRIE0) || bit_is_clear(*_ucsra, TXC0)) {
    if (bit_is_clear(SREG, SREG_I) && bit_is_set(*_ucsrb, UDRIE0))
        // Interrupts are globally disabled, but the DR empty
        // interrupt should be enabled, so poll the DR empty flag to
        // prevent deadlock
        if (bit_is_set(*_ucsra, UDRE0))
          _tx_udr_empty_irq();
}
// If we get here, nothing is queued anymore (DRIE is disabled) and
// the hardware finished tranmission (TXC is set).
}


include_二世 发表于 2014-8-7 12:29:42

我用的是1.52

include_二世 发表于 2014-8-7 12:35:56

Super169 发表于 2014-8-7 12:20 static/image/common/back.gif
請問你用的是什麼版本?

v1.0.5:


有没有其他函数来清除串口缓存的呀?
除了这个flush()

Super169 发表于 2014-8-7 13:00:53

include_二世 发表于 2014-8-7 12:35 static/image/common/back.gif
有没有其他函数来清除串口缓存的呀?
除了这个flush()

你的問題有點奇怪.
寫庫的人, 自己不應該寫兩個功能重複的函数, 最多只會是 alias.
其他人修改的或另外寫的庫, 就難以猜測了.

include_二世 发表于 2014-8-7 13:17:39

Super169 发表于 2014-8-7 13:00 static/image/common/back.gif
你的問題有點奇怪.
寫庫的人, 自己不應該寫兩個功能重複的函数, 最多只會是 alias.
其他人修改的或另外 ...

不太明白您的意思,就说我现在用的是1.52的版本,我想问一下清除串口缓存用的是什么函数呢?
Serial.flush();
这个应该不能使用了,已经不是清除缓存的函数了!

Super169 发表于 2014-8-7 13:32:36

1.0.5, 1.5.6 及 1.5.7 的都沒問題.

請問有什麼原因, 一定要用 1.5.2?

再者, 剛在網上下載了 v1.5.2, flush 也是正常的, 跟 1.0.5 一樣.

void HardwareSerial::flush()
{
// UDR is kept full while the buffer is not empty, so TXC triggers when EMPTY && SENT
while (transmitting && ! (*_ucsra & _BV(TXC0)));
transmitting = false;
}


請問你的程式是從那裡得來的?

include_二世 发表于 2014-8-7 13:41:29

Super169 发表于 2014-8-7 13:32 static/image/common/back.gif
1.0.5, 1.5.6 及 1.5.7 的都沒問題.

請問有什麼原因, 一定要用 1.5.2?


(⊙o⊙)…难道是我的用法有问题?
但是我看arduino官网上的介绍:
flush()
Description

Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)

include_二世 发表于 2014-8-7 13:50:51

Super169 发表于 2014-8-7 13:32 static/image/common/back.gif
1.0.5, 1.5.6 及 1.5.7 的都沒問題.

請問有什麼原因, 一定要用 1.5.2?


怎么说呢?我现在通过串口接受数据,上层发过来的数据是10ms一次,每次我接受一次然后进行处理一下,然后我把上层的数据改了,但是arduino这边会有个很长的延迟才会接受到改了的数据,就是说不能适时刷新数据!~~貌似是开始采集了很多数据都卡在了缓存中!~~所以我想每次采集之前把缓存清空,然后就能实时的采集数据了!~~

include_二世 发表于 2014-8-7 13:56:50

Super169 发表于 2014-8-7 13:32 static/image/common/back.gif
1.0.5, 1.5.6 及 1.5.7 的都沒問題.

請問有什麼原因, 一定要用 1.5.2?


用 while(Serial.read>=0);
解决了!~~

Super169 发表于 2014-8-7 14:07:27

include_二世 发表于 2014-8-7 13:56 static/image/common/back.gif
用 while(Serial.read>=0);
解决了!~~

請問你知道 flush 是做什麼的嗎?

你是在清除 input buffer, 而 flush 是把未送出的資料送出去.

照字面的解釋, 1.0 之前是有問題的.

include_二世 发表于 2014-8-7 14:19:17

Super169 发表于 2014-8-7 14:07 static/image/common/back.gif
請問你知道 flush 是做什麼的嗎?

你是在清除 input buffer, 而 flush 是把未送出的資料送出去.


flush()在1.0之前不就是清除缓存的意思么?
只不过1.0之后改了,他官方的文档不就是这么说的么?

Super169 发表于 2014-8-7 14:26:23

include_二世 发表于 2014-8-7 14:19 static/image/common/back.gif
flush()在1.0之前不就是清除缓存的意思么?
只不过1.0之后改了,他官方的文档不就是这么说的么?

1.0 之前應該是做錯了.

由於我是從 1.0.5 開始用, 跟據 flush 的意思, 我沒想過會是用來清除 input buffer 的.
页: [1] 2
查看完整版本: 请教一下arduino串口通信的问题!