Last active
August 29, 2015 14:21
-
-
Save StuffAndyMakes/5e0132ac2e1fdf4ac168 to your computer and use it in GitHub Desktop.
SerialPacket Send Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| typedef struct { | |
| uint8_t device; | |
| uint8_t command; | |
| uint32_t value; | |
| uint64_t serial; | |
| uint8_t ack; | |
| } Command; | |
| void SenderApplication::didReceiveGoodPacket(SerialPacket *p) { | |
| Serial.println("Got good packet!"); | |
| Command _receivedCommand; | |
| memcpy(&_receivedCommand, p->buffer, p->getDataLength()); | |
| Serial.println(_receivedCommand.device, DEC); | |
| } | |
| void SenderApplication::didReceiveBadPacket(SerialPacket *p, uint8_t err) { | |
| Serial.println("Got error " + String(err, DEC) + " receiving packet. Boo!") | |
| } | |
| void SenderApplication::main() { | |
| Serial.begin(115200); // debugging output | |
| Serial1.begin(19200); // sending & receiving packets | |
| SerialPacket p; | |
| p.setDelegate(this); | |
| p.setTimeout(2000); | |
| p.use(&Serial1); | |
| Command aCommandToSend; | |
| // fill Command structure with random stuff | |
| aCommandToSend.device = random(1, 254); | |
| aCommandToSend.command = random(0, 255); // Use a reserved byte (FRAME_START) to test escaping | |
| aCommandToSend.value = 100; | |
| aCommandToSend.serial = 0; | |
| // roughly 1 in 100 packets selected for ack request (set field to STATUS_NACK) | |
| aCommandToSend.ack = random(1, 100) == 25 ? STATUS_NACK : STATUS_ACK; | |
| // send it, returns bytes sent so you can be sure it all went | |
| uint8_t bytesSent = p.send((uint8_t *)&aCommandToSend, sizeof(aCommandToSend)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment