Serial
Serial 类层次结构
Serial 接口提供 UART 功能。串行链路有两个单向通道,一个用于发送,一个用于接收。链接是异步的,因此必须将串行链路的两端配置为使用相同的设置。
其中一个串行连接使用 Arm Mbed USB 端口,可让您轻松与主机 PC 通信。
Serial 类参考
公共成员函数 | |
Serial (PinName tx, PinName rx, const char *name=NULL, int baud=MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE) | |
Serial (PinName tx, PinName rx, int baud) | |
bool | readable () |
bool | writable () |
bool | writeable () |
公共成员函数继承自 mbed::SerialBase | |
void | baud (int baudrate) |
void | format (int bits=8, Parity parity=SerialBase::None, int stop_bits=1) |
int | readable () |
int | writeable () |
void | attach (Callback< void()> func, IrqType type=RxIrq) |
template<typename T > | |
void | attach (T *obj, void(T::*method)(), IrqType type=RxIrq) |
template<typename T > | |
void | attach (T *obj, void(*method)(T *), IrqType type=RxIrq) |
void | send_break () |
void | set_flow_control (Flow type, PinName flow1=NC, PinName flow2=NC) |
int | write (const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE) |
int | write (const uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE) |
void | abort_write () |
int | read (uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH) |
int | read (uint16_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH) |
void | abort_read () |
int | set_dma_usage_tx (DMAUsage usage) |
int | set_dma_usage_rx (DMAUsage usage) |
公共成员函数继承自 mbed::Stream | |
Stream (const char *name=NULL) | |
int | putc (int c) |
int | puts (const char *s) |
int | getc () |
char * | gets (char *s, int size) |
int | printf (const char *format,...) |
int | scanf (const char *format,...) |
int | vprintf (const char *format, std::va_list args) |
int | vscanf (const char *format, std::va_list args) |
operator std::FILE * () | |
公共成员函数继承自 mbed::FileLike | |
FileLike (const char *name=NULL) | |
公共成员函数继承自 mbed::FileHandle | |
virtual off_t | lseek (off_t offset, int whence) |
virtual int | fsync () |
virtual off_t | flen () |
virtual int | set_blocking (bool blocking) |
virtual bool | is_blocking () const |
virtual short | poll (short events) const |
bool | writable () const |
bool | readable () const |
virtual void | sigio (Callback< void()> func) |
公共成员函数继承自 mbed::FileBase | |
FileBase (const char *name, PathType t) | |
const char * | getName (void) |
PathType | getPathType (void) |
受保护的成员函数 | |
virtual int | _getc () |
virtual int | _putc (int c) |
virtual void | lock () |
virtual void | unlock () |
受保护的成员函数继承自 mbed::SerialBase | |
void | start_read (void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match) |
void | start_write (const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event) |
void | interrupt_handler_asynch (void) |
SerialBase (PinName tx, PinName rx, int baud) | |
int | _base_getc () |
int | _base_putc (int c) |
受保护的成员函数继承自 mbed::Stream | |
virtual int | close () |
virtual ssize_t | write (const void *buffer, size_t length) |
virtual ssize_t | read (void *buffer, size_t length) |
virtual off_t | seek (off_t offset, int whence) |
virtual off_t | tell () |
virtual void | rewind () |
virtual int | isatty () |
virtual int | sync () |
virtual off_t | size () |
受保护的属性 | |
PlatformMutex | _mutex |
受保护的属性继承自 mbed::SerialBase | |
CThunk< SerialBase > | _thunk_irq |
DMAUsage | _tx_usage |
DMAUsage | _rx_usage |
event_callback_t | _tx_callback |
event_callback_t | _rx_callback |
serial_t | _serial |
Callback< void()> | _irq [IrqCnt] |
int | _baud |
受保护的属性继承自 mbed::Stream | |
std::FILE * | _file |
其他继承成员 | |
继承自的公共类型 mbed::SerialBase | |
enum | Parity { None = 0, Odd, Even, Forced1, Forced0 } |
enum | IrqType { RxIrq = 0, TxIrq, IrqCnt } |
enum | Flow { Disabled = 0, RTS, CTS, RTSCTS } |
静态公共成员函数继承自 mbed::SerialBase | |
static void | _irq_handler (uint32_t id, SerialIrq irq_type) |
静态公共成员函数继承自 mbed::FileBase | |
static FileBase * | lookup (const char *name, unsigned int len) |
static FileBase * | get (int n) |
注意: 在 Windows 计算机上,您需要安装 USB 串行驱动程序。请参阅 Windows 串行配置。
串行通道有许多可配置的参数:
- 波特率 - 有许多标准波特率,从每秒几百比特到每秒兆比特。Mbed 微控制器上串行连接的默认设置为 9600 波特。
- 数据长度 - 传输的数据可以是 7 位或 8 位长。Mbed 微控制器上串行连接的默认设置是 8 位。
- 奇偶校验 - 您可以添加可选的奇偶校验位。奇偶校验位将自动设置为使数据中的 1 的数量为奇数或偶数。奇偶校验设置为奇数,偶数或无。Arm Mbed 微控制器上串行连接的默认设置为 None。
- 停止位 - 发送数据和奇偶校验位后,插入一个或两个停止位以 “frame” 数据。Mbed 微控制器上串行连接的默认设置是一个停止位。
Mbed 微控制器的默认设置描述为 9600 - 8 - N - 1,这是串行端口设置的常用符号。
Serial hello, world
/* mbed Example Program
* Copyright (c) 2006-2014 ARM Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
int main() {
pc.printf("Hello World!\n\r");
while(1) {
pc.putc(pc.getc() + 1); // echo input back to terminal
}
}
Serial 示例
示例一
以波特率 19200 向设备写入消息。
#include "mbed.h"
Serial device(USBTX, USBRX); // tx, rx
int main() {
device.baud(19200);
device.printf("Hello World\n");
}
示例二
在 PC 和外部 UART 之间提供串行传递。
#include "mbed.h"
//K64F
Serial pc(USBTX, USBRX); // tx, rx
Serial device(MBED_CONF_APP_UART1_TX, MBED_CONF_APP_UART1_RX); // tx, rx
int main() {
while(1) {
if(pc.readable()) {
device.putc(pc.getc());
}
if(device.readable()) {
pc.putc(device.getc());
}
}
}
相关内容
- Serial 词汇表。
- Windows 串行配置。