Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: USBDevice
Fork of CommunicationHandler by
Communication Protocol
ASCII and C# API Commands¶
Configuration and Status Commands¶
Add Interface¶
Add new interface.
| ASCII | C# | |
|---|---|---|
| Command | add_interface <interface_type>:<interface_no> add_interface <interface_type>:<interface_no> <pin_map> | int AddInterface(InterfaceType interfaceType, byte interfaceNo); int AddInterface(InterfaceType interfaceType, byte interfaceNo, params Pin[] pinMap); |
| Parameters | interface_type: I2C, SPI, GPIO, UART, ONEWIRE, PWM, AIN, AOUT interface_no : 0, 1, 2, … | pin_map: SPI: MOSI,MISO,SCK,SSEL I2C: SDA, SCL UART: TX, RX, CTS, RTS GPIO: MSB ... LSB ONEWIRE: PIN ADC: PIN PWM: PIN |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | add_interface i2c:0 P1_6 P1_7 add_interface uart:0 P0_0 P0_1 P0_2 P0_3 add_interface spi:0 P0_5 P0_6 P0_4 P0_7 add_interface onewire:0 AIN2 add_interface gpio:0 P0_0 P0_1 P0_2 P0_3 add_interface gpio:1 P4_4 add_interface pwm:0 P0_7 | Bridge.AddInterface(InterfaceType.I2C, 0, Pin.P1_6, Pin.P1_7); Bridge.AddInterface(InterfaceType.UART, 0, Pin.P0_0, Pin.P0_1, Pin.P0_2, Pin.P0_3); Bridge.AddInterface(InterfaceType.SPI, 0, Pin.P0_4, Pin.P0_5, Pin.P0_6, Pin.P0_7); Bridge.AddInterface(InterfaceType.ONEWIRE, 0, Pin.AIN2); Bridge.AddInterface(InterfaceType.GPIO, 0, Pin.P0_0, Pin.P0_1, Pin.P0_2, Pin.P0_3); Bridge.AddInterface(InterfaceType.GPIO, 1, Pin.P4_4); Bridge.AddInterface(InterfaceType.PWM, 0, Pin.P0_7); |
Remove Interface¶
Remove a specific interface.
| ASCII | C# | |
|---|---|---|
| Command | remove_interface <int_type>:<int_no> | int RemoveInterface(InterfaceType interfaceType, byte interfaceNo); |
| Parameters | interface_type: I2C, SPI, GPIO, UART, ONEWIRE, PWM, AIN, AOUT | interface_no : 0, 1, 2, … |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | remove_interface I2C:0 | Bridge.RemoveInterface(InterfaceType.I2C, 0); |
Remove All Interfaces¶
Remove all interfaces.
| ASCII | C# | |
|---|---|---|
| Command | remove_all_interfaces | int RemoveAllInterfaces(); |
| Parameters | - | - |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | remove_all_interfaces | Bridge.RemoveAllInterfaces(); |
List Interfaces¶
List all added interfaces.
| ASCII | C# | |
|---|---|---|
| Command | list_interfaces | int ListInterfaces(out Interface[] interfaces); |
| Parameters | Interface[] interfaces as a reference parameter | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | list_interfaces | Bridge.ListInterfaces(out interface); |
Serial Settings¶
Configures the parameters for serial communication through selected port and connects to the device.
| ASCII | C# | |
|---|---|---|
| Command | N/A | int SerialSettings(string portName); |
| Parameters | N/A | string portName (eg. "COM9") |
| Returns | N/A | Error code: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | N/A | Bridge.SerialSettings("COM9"); |
Switch to ASCII Mode¶
Switch from binary mode to ASCII mode. Only available in binary mode.
| ASCII | C# | |
|---|---|---|
| Command | N/A | int SwitchToAsciiMode(); |
| Parameters | N/A | - |
| Returns | N/A | Error code: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | N/A | Bridge.SwitchToAsciiMode(); |
Switch to Binary Mode¶
Switch from ASCII mode to binary mode. Only available in ASCII mode.
| ASCII | C# | |
|---|---|---|
| Command | switch_to_binary_mode | N/A |
| Parameters | - | N/A |
| Returns | Error code | N/A |
| Examples | switch_to_binary_mode | N/A |
Get Build Version¶
Gets the build version of Bridge.dll.
| ASCII | C# | |
|---|---|---|
| Command | N/A | string GetBuildVersion(); |
| Parameters | N/A | - |
| Returns | N/A | Build version (e.g. "1.0.10") |
| Examples | N/A | Bridge.GetBuildVersion(); |
Get Communication Status¶
Gets current communication status.
| ASCII | C# | |
|---|---|---|
| Command | N/A | CommunicationStatus GetCommunicationStatus(); |
| Parameters | N/A | - |
| Returns | N/A | Communication status (e.g. CommunicationStatus.Busy) |
| Examples | N/A | Bridge.GetCommunicationStatus(); |
Get Available Ports¶
Gets available ports which connects to PICO via the vendor and product ids.
| ASCII | C# | |
|---|---|---|
| Command | N/A | string[] GetAvailablePorts(); |
| Parameters | N/A | - |
| Returns | N/A | Available ports (e.g. ["COM9"]) |
| Examples | N/A | Bridge.GetAvailablePorts(); |
Get Platform Info¶
Gather platform specific information.
| ASCII | C# | |
|---|---|---|
| Command | get_platform_info | int GetPlatformInfo(out PlatformInfo platform_info); |
| Parameters | PlatformInfo platform_info as a reference parameter for pass by reference | |
| Returns | platform_name=<platform_name>, version=<version> | Error code: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | get_platform_info | Bridge.GetPlatformInfo(out platform_info); |
Close¶
Closes the port used for the serial communication.
| ASCII | C# | |
|---|---|---|
| Command | N/A | void Close(); |
| Parameters | N/A | - |
| Returns | N/A | - |
| Examples | N/A | void Close(); |
SPI Commands¶
SPI Format¶
Set SPI format of SPI interface.
| ASCII | C# | |
|---|---|---|
| Command | spi_format <interface_no> <bits> <mode> | int SpiFormat(byte interfaceNo, byte bits, byte spiMode); |
| Parameters | interface_no: 0, 1, 2, … bits : Number of bits per SPI frame | mode : Clock polarity and phase mode (0, 1, 2, 3) mode 0: POL=0 & PHA=0 mode 1: POL=0 & PHA=1 mode 2: POL=1 & PHA=0 mode 3: POL=1 & PHA=1 |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | spi_format 0 8 0 | Bridge.SpiFormat(0, 8, 0); |
SPI Frequency¶
Set frequency of SPI interface.
| ASCII | C# | |
|---|---|---|
| Command | spi_frequency <interface_no> <frequency_in_hz> | int SpiFrequency(byte interfaceNo, uint frequencyHz); |
| Parameters | interface_no: 0, 1, 2, … frequency_in_hz: The bus frequency in hertz | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | spi_frequency 0 24000000 | Bridge.SpiFrequency(0, 24000000); |
SPI Write¶
Write byte over SPI interface. Read response data byte from SPI slave.
| ASCII | C# | |
|---|---|---|
| Command | spi_write <interface_no> <value> | int SpiWrite(byte interfaceNo, uint[] value, out uint[] response); |
| Parameters | interface_no: 0, 1, 2, … value : Data to be sent to the SPI slave response : SPI slave response data with reference | |
| Returns | spi_response=0x<data> | response status: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) Integer SPI slave response data with reference. |
| Examples | spi_frequency 0 24000000 | Bridge.SpiFrequency(0, 24000000); |
I2C Commands¶
I2C Frequency¶
Set frequency of I2C interface.
| ASCII | C# | |
|---|---|---|
| Command | i2c_frequency <interface_no> <frequency_in_hz> | int I2cFrequency(byte interfaceNo, uint frequencyHz); |
| Parameters | interface_no: 0, 1, 2, … frequency_in_hz: The bus frequency in hertz | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | i2c_frequency 0 24000000 | Bridge.I2cFrequency(0, 24000000); |
I2C Read¶
Read data from I2C interface.
| ASCII | C# | |
|---|---|---|
| Command | i2c_read <interface_no> <address> <length> To specify memory address during i2c read: i2c_read <interface_no> <address> <length><memory_start_address> | int I2cRead(byte interfaceNo, byte address, byte length, out int[] read_data); |
| Parameters | interface_no: 0, 1, 2, … address : 7-bit I2C slave address | length : Number of bytes to read read_data : Integer I2C slave response data via pass by reference |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) and Integer I2C slave response data with reference |
| Examples | i2c_read 0 0x2A 3 | Bridge.I2cRead(0, 0x2A, 3, out read_data); |
I2C Write¶
Write data over I2C interface.
| ASCII | C# | |
|---|---|---|
| Command | i2c_write <interface_no><address><data…> | int I2cWrite(byte interfaceNo, byte address, byte[] data); |
| Parameters | interface_no: 0, 1, 2, … address : 7-bit I2C slave address data : data to send | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | i2c_write 0 0x2A 0x01 0x02 0x03 | Bridge.I2cWrite(0, 0x2A, new int[] {0x01, 0x02, 0x03}); |
I2C Repeated Write¶
Write data over I2C interface with repeated option.
| ASCII | C# | |
|---|---|---|
| Command | i2c_write_r <interface_no> <address> <data…> | int I2cWriteRepeated(byte interfaceNo, byte address, byte[] data); |
| Parameters | interface_no: 0, 1, 2, … address : 7-bit I2C slave address data : data to send | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | i2c_write_r 0 0x2A 0x01 0x02 0x03 | Bridge.I2cWriteRepeated(0, 0x2A, new int[] {0x01, 0x02, 0x03}); |
I2C Start¶
Sends a start flag to I2C bus.
| ASCII | C# | |
|---|---|---|
| Command | i2c_start <interface_no> | int I2cStart(byte interfaceNo); |
| Parameters | interface_no: 0, 1, 2, … | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | i2c_start 0 | Bridge.I2cStart(0); |
I2C Stop¶
Sends a stop flag to I2C bus.
| ASCII | C# | |
|---|---|---|
| Command | i2c_stop <interface_no> | int I2cStop(byte interfaceNo); |
| Parameters | interface_no: 0, 1, 2, … | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | i2c_stop 0 | Bridge.I2cStop(0); |
UART Commands¶
UART Format¶
Set UART interface format.
| ASCII | C# | |
|---|---|---|
| Command | uart_format <interface_no> <bits> <parity> <stop_bits> | int UartFormat(byte interfaceNo, byte bits, Parity parity, byte stopBits) ; |
| Parameters | interface_no: 0, 1, 2, … bits : The number of bits in a word (5-8) | parity: Use the number as 0:none, 1:odd, 2:even stop_bits : The number of stop bits (1 or 2) |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | uart_format 0 8 0 1 | Bridge.UartFormat(0, 9, Parity.None, 1); |
UART Set Baudrate¶
Set UART interface baudrate.
| ASCII | C# | |
|---|---|---|
| Command | uart_baudrate <interface_no> <baudrate> | int UartBaud(byte interfaceNo, uint baudrate) ; |
| Parameters | interface_no: 0, 1, 2, … baudrate : Integer baudrate | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | uart_baudrate 0 115200 | Bridge.UartBaud(0, 115200); |
UART Read¶
Read data from UART interface.
| ASCII | C# | |
|---|---|---|
| Command | uart_read <interface_no> <length> | int UartRead(byte interfaceNo, byte length, out int[] read_data); |
| Parameters | interface_no: 0, 1, 2, … length : Number of bytes to read | read_data : Integer UART data array via pass by reference |
| Returns | uart_read=0x<data1>,0x<data2>,... | response status: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) and Integer UART data array with reference |
| Examples | uart_read 0 3 | Bridge.UartRead(0, 3, out read_data); |
UART Write¶
Write data over UART interface.
| ASCII | C# | |
|---|---|---|
| Command | uart_write <interface_no> <data…> | int UartWrite(byte interfaceNo, byte[] data); |
| Parameters | interface_no: 0, 1, 2, … data: data array to send | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | uart_write 0 0x01 0x02 0x03 | Bridge.UartWrite(0, new int[] {0x01, 0x02, 0x03}); |
GPIO Commands¶
GPIO Read¶
Read data from GPIO.
| ASCII | C# | |
|---|---|---|
| Command | gpio_read <interface_no> | int GpioRead(byte interfaceNo, out int read_data); |
| Parameters | interface_no: 0, 1, 2, … read_data : Integer data with reference | |
| Returns | gpio_read=0x... | response status: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) and Integer data with reference |
| Examples | gpio_read 0 | Bridge.GpioRead(0, out read_data); |
GPIO Write¶
Write data to GPIO.
| ASCII | C# | |
|---|---|---|
| Command | gpio_write <interface_no> <data> | int GpioWrite(byte interfaceNo, byte data); |
| Parameters | interface_no: 0, 1, 2, … data: data byte to send | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | gpio_write 0 0xAA | Bridge.GpioWrite(0, 0xAA); |
PWM Commands¶
PWM Write¶
Write data to PWM.
| ASCII | C# | |
|---|---|---|
| Command | pwm_write <interface_no> <duty_cycle> | int PwmWrite(byte interfaceNo, float duty_cycle); |
| Parameters | interface_no: 0, 1, 2, … duty_cycle: A floating-point value representing the output duty-cycle. | Duty cycle should lie between 0.0 (representing on 0%) and 1.0 (representing on 100%). Values outside this range will be saturated to 0.0 or 1.0 |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | pwm_write 0 0.23 | Bridge.PwmWrite(0, 0.23); |
PWM Read¶
Read previously set data from PWM.
| ASCII | C# | |
|---|---|---|
| Command | pwm_read <interface_no> | int PwmRead(byte interfaceNo, out float read_data); |
| Parameters | interface_no: 0, 1, 2, … read_data : float PWM data with reference | |
| Returns | pwm_read=0x<data> | response status: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) and float PWM data with reference |
| Examples | pwm_read 0 | Bridge.PwmRead(0, out read_data); |
PWM Period¶
Set PWM period.
| ASCII | C# | |
|---|---|---|
| Command | pwm_period <interface_no> <period_in_us> | int PwmPeriod(byte interfaceNo, int periodUs); |
| Parameters | Set the PWM period, specified in micro-seconds, keeping the duty cycle the same | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | pwm_period 0 5000 | Bridge.PwmPeriod(0, 5000); |
PWM Pulsewidth¶
Set PWM pulsewidth.
| ASCII | C# | |
|---|---|---|
| Command | pwm_pulsewidth <interface_no> <pulse_width> | int PwmPulseWidth(byte interfaceNo, int pulseWidth); |
| Parameters | Set the PWM pulsewidth, specified in ms, keeping the period the same | |
| Returns | response status: (int)ResponseStatus | (e.g. 0 for SuccessfulResponse, -1 for NullResponse) |
| Examples | pwm_pulsewidth 0 12000 | Bridge.PwmPulseWidth(0, 12000); |
ADC Commands¶
ADC Read¶
Read data from ADC.
| ASCII | C# | |
|---|---|---|
| Command | adc_read <interface_no> | int AdcRead(byte interfaceNo, out int read_data); |
| Parameters | interface_no: 0, 1, 2, … | read_data : Integer ADC data with reference |
| Returns | adc_read=0x<data> | response status: (int)ResponseStatus (e.g. 0 for SuccessfulResponse, -1 for NullResponse) and Integer ADC data with reference |
| Examples | adc_read 0 | Bridge.AdcRead(0, out read_data); |
C# API Data Structures¶
Platform Info¶
struct PlatformInfo{
string name;
int versionNumberMajor;
int versionNumberMinor;
int versionNumberBuild;
};
Interface Type¶
enum InterfaceType {
I2C, SPI, GPIO, UART, ONEWIRE, PWM, ADC
};
Response Status¶
enum ResponseStatus {
SuccessfulResponse = 0,
NullResponse = -1,
Timeout = -2,
FunctionIDUnidentified = -3,
FunctionIDUnmatched = -4,
Overflow = -5
};
Communication Status¶
enum CommunicationStatus
{
Waiting,
Idle,
Busy,
Closed
}
Parity¶
enum Parity
{
None = 0, Odd, Even, Forced1, Forced0
};
Pin¶
enum Pin {
P0_0 = (0 << PORT_SHIFT), P0_1, P0_2, P0_3, P0_4, P0_5, P0_6, P0_7,
P1_0 = (1 << PORT_SHIFT), P1_1, P1_2, P1_3, P1_4, P1_5, P1_6, P1_7,
P2_0 = (2 << PORT_SHIFT), P2_1, P2_2, P2_3, P2_4, P2_5, P2_6, P2_7,
P3_0 = (3 << PORT_SHIFT), P3_1, P3_2, P3_3, P3_4, P3_5, P3_6, P3_7,
P4_0 = (4 << PORT_SHIFT), P4_1, P4_2, P4_3, P4_4, P4_5, P4_6, P4_7,
Analog input pins
AIN_0 = (0xA << PORT_SHIFT), AIN_1, AIN_2, AIN_3, AIN_4, AIN_5, AIN_6, AIN_7, AIN_8, AIN_9,
LEDs
LED1 = P2_4,
LED2 = P2_5,
LED3 = P2_6,
LED_RED = LED1,
LED_GREEN = LED2,
LED_BLUE = LED3,
Push button
SW1 = P2_7,
USB bridge connected UART pins
USBTX = P2_1,
USBRX = P2_0,
USBTX = P0_1,
USBRX = P0_0,
STDIO_UART_TX = USBTX,
STDIO_UART_RX = USBRX,
I2C pins
I2C0_SCL = P1_7,
I2C0_SDA = P1_6,
I2C1_SCL = P3_5,
I2C1_SDA = P3_4,
UART pins
UART0_RX = P0_0,
UART0_TX = P0_1,
UART0_CTS = P0_2,
UART0_RTS = P0_3,
UART1_RX = P2_0,
UART1_TX = P2_1,
UART2_RX = P3_0,
UART2_TX = P3_1,
UART2_CTS = P3_2,
UART2_RTS = P3_3,
SPI pins
SPI0_SCK = P0_4,
SPI0_MOSI = P0_5,
SPI0_MISO = P0_6,
SPI0_SS = P0_7,
SPI1_SCK = P1_0,
SPI1_MOSI = P1_1,
SPI1_MISO = P1_2,
SPI1_SS = P1_3,
SPI2_SCK = P2_4,
SPI2_MOSI = P2_5,
SPI2_MISO = P2_6,
SPI2_SS = P2_7,
Not connected
NC = 255
};
Interface¶
struct Interface {
InterfaceType type;
int no;
Pin pinMap[];
};
Parity¶
struct Parity {
None = 0, Odd, Even, Forced1, Forced0
};
