/      日本語

WirelessUART

Performs serial communication.
WirelessUART performs serial communication.

Act Features

  • Communicates between two TWELITE devices connected via UART using ASCII format.

How to Use the Act

Required TWELITE Devices

Use two devices connected to the PC via serial connection as follows:

Explanation of the Act

setup()

void setup() {
	auto&& set = the_twelite.settings.use<STG_STD>();
	auto&& nwk = the_twelite.network.use<NWK_SIMPLE>();

	/*** INTERACTIVE MODE */
	// settings: configure items
	set << SETTINGS::appname("WirelessUART");
	set << SETTINGS::appid_default(DEFAULT_APP_ID); // set default appID
	set << SETTINGS::ch_default(DEFAULT_CHANNEL); // set default channel
	set << SETTINGS::lid_default(DEFAULT_LID); // set default lid
	set.hide_items(E_STGSTD_SETID::OPT_DWORD2, E_STGSTD_SETID::OPT_DWORD3, E_STGSTD_SETID::OPT_DWORD4, E_STGSTD_SETID::ENC_KEY_STRING, E_STGSTD_SETID::ENC_MODE);
	set.reload(); // load from EEPROM.

	/*** SETUP section */
	// the twelite main class
	the_twelite
		<< set                      // from interactive mode (APPID/CH/POWER)
		<< TWENET::rx_when_idle();  // open receive circuit (if not set, it can't listen packets from others)

	// Register Network
	nwk	<< set;						// from interactive mode (LID/REPEAT)

	/*** BEGIN section */
	SerialParser.begin(PARSER::ASCII, 128); // Initialize the serial parser
	the_twelite.begin(); // start twelite!

	/*** INIT message */
	Serial << "--- WirelessUart (id=" << int(nwk.get_config().u8Lid) << ") ---" << mwx::crlf;
}

Initializes the interactive mode. In this sample, prepare two or more devices with different logical device IDs (LID).

SerialParser.begin(PARSER::ASCII, 128);

Initializes the serial parser.

loop()

while(Serial.available())  {
	if (SerialParser.parse(Serial.read())) {
		Serial << ".." << SerialParser;
		const uint8_t* b = SerialParser.get_buf().begin();
		uint8_t addr = *b; ++b; // the first byte is destination address.
		transmit(addr, b, SerialParser.get_buf().end());
	}
}

When data input is detected from the serial port, one byte is input to the serial parser. When the ASCII format is fully received, SerialParser.parse() returns true.

SerialParser allows access to the internal buffer via smplbuf. In the example above, the first byte of the buffer is extracted as the destination address, and the bytes from the second byte to the end are passed to the transmit() function.

on_rx_packet()

When a packet is received, a buffer smplbuf_u8<128> buf is created containing the sender as the first byte followed by the payload, and it is output to the serial port via the serial parser serparser_attach pout.

void on_rx_packet(packet_rx& rx, bool_t &handled) {
	// check the packet header.
	const uint8_t* p = rx.get_payload().begin();
	if (rx.get_length() > 4 && !strncmp((const char*)p, (const char*)FOURCHARS, 4)) {
		Serial << format("..rx from %08x/%d", rx.get_addr_src_long(), rx.get_addr_src_lid()) << mwx::crlf;

		smplbuf_u8<128> buf;
		mwx::pack_bytes(buf
				, uint8_t(rx.get_addr_src_lid())            // src addr (LID)
				, make_pair(p+4, rx.get_payload().end()) );	// data body

		serparser_attach pout;
		pout.begin(PARSER::ASCII, buf.begin(), buf.size(), buf.size());
		Serial << pout;
	}
}

Commands for Testing

Example

:FE00112233X

:FE001122339C

Send 00112233 to any child device.

Example

:03AABBCC00112233X

:03AABBCC0011223366

Send AABBCC00112233 to child device number 3.

Example

:FF00112233X

:00112233X

Send to any parent or child device (0xFF), or to the parent device (0x00).