From e6e8debd106fe60c902044f995d81b9b2e7e5b11 Mon Sep 17 00:00:00 2001 From: PB2 Date: Sat, 14 Aug 2021 16:27:55 -0700 Subject: [PATCH] Add SerialTransfer.h compatible examples (#37) --- examples/data/Arduino/rx_data/rx_data.ino | 32 +++++++++++++ examples/data/Arduino/tx_data/tx_data.ino | 39 +++++++++++++++ examples/data/Python/rx_data.py | 50 ++++++++++++++++++++ examples/data/Python/tx_data.py | 31 ++++++++++++ examples/datum/Arduino/rx_datum/rx_datum.ino | 22 +++++++++ examples/datum/Arduino/tx_datum/tx_datum.ino | 22 +++++++++ examples/datum/Python/rx_datum.py | 32 +++++++++++++ examples/datum/Python/tx_datum.py | 20 ++++++++ examples/file/Arduino/rx_file/rx_file.ino | 39 +++++++++++++++ examples/file/Arduino/tx_file/tx_file.ino | 43 +++++++++++++++++ examples/file/Python/rx_file.py | 41 ++++++++++++++++ examples/file/Python/tx_file.py | 45 ++++++++++++++++++ 12 files changed, 416 insertions(+) create mode 100644 examples/data/Arduino/rx_data/rx_data.ino create mode 100644 examples/data/Arduino/tx_data/tx_data.ino create mode 100644 examples/data/Python/rx_data.py create mode 100644 examples/data/Python/tx_data.py create mode 100644 examples/datum/Arduino/rx_datum/rx_datum.ino create mode 100644 examples/datum/Arduino/tx_datum/tx_datum.ino create mode 100644 examples/datum/Python/rx_datum.py create mode 100644 examples/datum/Python/tx_datum.py create mode 100644 examples/file/Arduino/rx_file/rx_file.ino create mode 100644 examples/file/Arduino/tx_file/tx_file.ino create mode 100644 examples/file/Python/rx_file.py create mode 100644 examples/file/Python/tx_file.py diff --git a/examples/data/Arduino/rx_data/rx_data.ino b/examples/data/Arduino/rx_data/rx_data.ino new file mode 100644 index 0000000..c1c1776 --- /dev/null +++ b/examples/data/Arduino/rx_data/rx_data.ino @@ -0,0 +1,32 @@ +#include "SerialTransfer.h" + + +SerialTransfer myTransfer; + +struct STRUCT { + char z; + float y; +} testStruct; + +char arr[6]; + + +void setup() +{ + Serial.begin(115200); + myTransfer.begin(Serial); +} + + +void loop() +{ + if(myTransfer.available()) + { + // use this variable to keep track of how many + // bytes we've processed from the receive buffer + uint16_t recSize = 0; + + recSize = myTransfer.rxObj(testStruct, recSize); + recSize = myTransfer.rxObj(arr, recSize); + } +} diff --git a/examples/data/Arduino/tx_data/tx_data.ino b/examples/data/Arduino/tx_data/tx_data.ino new file mode 100644 index 0000000..c0362ff --- /dev/null +++ b/examples/data/Arduino/tx_data/tx_data.ino @@ -0,0 +1,39 @@ +#include "SerialTransfer.h" + + +SerialTransfer myTransfer; + +struct STRUCT { + char z; + float y; +} testStruct; + +char arr[] = "hello"; + + +void setup() +{ + Serial.begin(115200); + myTransfer.begin(Serial); + + testStruct.z = '$'; + testStruct.y = 4.5; +} + + +void loop() +{ + // use this variable to keep track of how many + // bytes we're stuffing in the transmit buffer + uint16_t sendSize = 0; + + ///////////////////////////////////////// Stuff buffer with struct + sendSize = myTransfer.txObj(testStruct, sendSize); + + ///////////////////////////////////////// Stuff buffer with array + sendSize = myTransfer.txObj(arr, sendSize); + + ///////////////////////////////////////// Send buffer + myTransfer.sendData(sendSize); + delay(500); +} diff --git a/examples/data/Python/rx_data.py b/examples/data/Python/rx_data.py new file mode 100644 index 0000000..1d9295b --- /dev/null +++ b/examples/data/Python/rx_data.py @@ -0,0 +1,50 @@ +from time import sleep +from pySerialTransfer import pySerialTransfer as txfer + + +class struct(object): + z = '' + y = 0.0 + + +arr = '' + + +if __name__ == '__main__': + try: + testStruct = struct + link = txfer.SerialTransfer('COM11') + + link.open() + sleep(5) + + while True: + if link.available(): + recSize = 0 + + testStruct.z = link.rx_obj(obj_type='c', start_pos=recSize) + recSize += txfer.STRUCT_FORMAT_LENGTHS['c'] + + testStruct.y = link.rx_obj(obj_type='f', start_pos=recSize) + recSize += txfer.STRUCT_FORMAT_LENGTHS['f'] + + arr = link.rx_obj(obj_type=str, + start_pos=recSize, + obj_byte_size=6) + recSize += len(arr) + + print('{}{} | {}'.format(testStruct.z, testStruct.y, arr)) + + elif link.status < 0: + if link.status == txfer.CRC_ERROR: + print('ERROR: CRC_ERROR') + elif link.status == txfer.PAYLOAD_ERROR: + print('ERROR: PAYLOAD_ERROR') + elif link.status == txfer.STOP_BYTE_ERROR: + print('ERROR: STOP_BYTE_ERROR') + else: + print('ERROR: {}'.format(link.status)) + + + except KeyboardInterrupt: + link.close() \ No newline at end of file diff --git a/examples/data/Python/tx_data.py b/examples/data/Python/tx_data.py new file mode 100644 index 0000000..e294c5d --- /dev/null +++ b/examples/data/Python/tx_data.py @@ -0,0 +1,31 @@ +from time import sleep +from pySerialTransfer import pySerialTransfer as txfer + + +class struct(object): + z = '$' + y = 4.5 + + +arr = 'hello' + + +if __name__ == '__main__': + try: + testStruct = struct + link = txfer.SerialTransfer('COM11') + + link.open() + sleep(5) + + while True: + sendSize = 0 + + sendSize = link.tx_obj(testStruct.z, start_pos=sendSize) + sendSize = link.tx_obj(testStruct.y, start_pos=sendSize) + sendSize = link.tx_obj(arr, start_pos=sendSize) + + link.send(sendSize) + + except KeyboardInterrupt: + link.close() \ No newline at end of file diff --git a/examples/datum/Arduino/rx_datum/rx_datum.ino b/examples/datum/Arduino/rx_datum/rx_datum.ino new file mode 100644 index 0000000..760b75b --- /dev/null +++ b/examples/datum/Arduino/rx_datum/rx_datum.ino @@ -0,0 +1,22 @@ +#include "SerialTransfer.h" + + +SerialTransfer myTransfer; + +float y; + + +void setup() +{ + Serial.begin(115200); + myTransfer.begin(Serial); +} + + +void loop() +{ + if(myTransfer.available()) + { + myTransfer.rxObj(y); + } +} diff --git a/examples/datum/Arduino/tx_datum/tx_datum.ino b/examples/datum/Arduino/tx_datum/tx_datum.ino new file mode 100644 index 0000000..b9a3fcb --- /dev/null +++ b/examples/datum/Arduino/tx_datum/tx_datum.ino @@ -0,0 +1,22 @@ +#include "SerialTransfer.h" + + +SerialTransfer myTransfer; + +float y; + + +void setup() +{ + Serial.begin(115200); + myTransfer.begin(Serial); + + y = 4.5; +} + + +void loop() +{ + myTransfer.sendDatum(y); + delay(500); +} diff --git a/examples/datum/Python/rx_datum.py b/examples/datum/Python/rx_datum.py new file mode 100644 index 0000000..a4dfab8 --- /dev/null +++ b/examples/datum/Python/rx_datum.py @@ -0,0 +1,32 @@ +from time import sleep +from pySerialTransfer import pySerialTransfer as txfer + + +y = 0.0 + + +if __name__ == '__main__': + try: + link = txfer.SerialTransfer('COM11') + + link.open() + sleep(5) + + while True: + if link.available(): + y = link.rx_obj(obj_type='f') + print(y) + + elif link.status < 0: + if link.status == txfer.CRC_ERROR: + print('ERROR: CRC_ERROR') + elif link.status == txfer.PAYLOAD_ERROR: + print('ERROR: PAYLOAD_ERROR') + elif link.status == txfer.STOP_BYTE_ERROR: + print('ERROR: STOP_BYTE_ERROR') + else: + print('ERROR: {}'.format(link.status)) + + + except KeyboardInterrupt: + link.close() \ No newline at end of file diff --git a/examples/datum/Python/tx_datum.py b/examples/datum/Python/tx_datum.py new file mode 100644 index 0000000..c31f0a7 --- /dev/null +++ b/examples/datum/Python/tx_datum.py @@ -0,0 +1,20 @@ +from time import sleep +from pySerialTransfer import pySerialTransfer as txfer + + +y = 4.5 + + +if __name__ == '__main__': + try: + link = txfer.SerialTransfer('COM11') + + link.open() + sleep(5) + + while True: + sendSize = link.tx_obj(y) + link.send(sendSize) + + except KeyboardInterrupt: + link.close() \ No newline at end of file diff --git a/examples/file/Arduino/rx_file/rx_file.ino b/examples/file/Arduino/rx_file/rx_file.ino new file mode 100644 index 0000000..3b64941 --- /dev/null +++ b/examples/file/Arduino/rx_file/rx_file.ino @@ -0,0 +1,39 @@ +#include "SerialTransfer.h" + + +SerialTransfer myTransfer; + +const int fileSize = 2000; +char file[fileSize]; +uint16_t fileIndex = 0; +char fileName[10]; + + +void setup() +{ + Serial.begin(115200); + + myTransfer.begin(Serial); +} + + +void loop() +{ + if (myTransfer.available()) + { + if (!myTransfer.currentPacketID()) + { + myTransfer.rxObj(fileName); + } + else if (myTransfer.currentPacketID() == 1) + { + myTransfer.rxObj(fileIndex); + + for(uint8_t i=sizeof(fileIndex); i fileSize) // Determine data length for the last packet if file length is not an exact multiple of MAX_PACKET_SIZE-2 + dataLen = fileSize - fileIndex; + + uint8_t sendSize = myTransfer.txObj(fileIndex); // Stuff the current file index + sendSize = myTransfer.txObj(file[fileIndex], sendSize, dataLen); // Stuff the current file data + + myTransfer.sendData(sendSize, 1); // Send the current file index and data + delay(1000); + } + delay(10000); +} diff --git a/examples/file/Python/rx_file.py b/examples/file/Python/rx_file.py new file mode 100644 index 0000000..f670beb --- /dev/null +++ b/examples/file/Python/rx_file.py @@ -0,0 +1,41 @@ +from time import sleep +from pySerialTransfer import pySerialTransfer as txfer + + +file = '' +fileName = '' + + +if __name__ == '__main__': + try: + link = txfer.SerialTransfer('COM11') + + link.open() + sleep(5) + + while True: + if link.available(): + if not link.idByte: + file = '' + fileName = link.rx_obj(str, obj_byte_size=8) + + print('\n\n\nFile Name: {}\n'.format(fileName)) + + else: + nextContents = link.rx_obj(str, start_pos=2, obj_byte_size=link.bytesRead-2) + file += nextContents + + print(nextContents, end='') + + elif link.status < 0: + if link.status == txfer.CRC_ERROR: + print('ERROR: CRC_ERROR') + elif link.status == txfer.PAYLOAD_ERROR: + print('ERROR: PAYLOAD_ERROR') + elif link.status == txfer.STOP_BYTE_ERROR: + print('ERROR: STOP_BYTE_ERROR') + else: + print('ERROR: {}'.format(link.status)) + + except KeyboardInterrupt: + link.close() \ No newline at end of file diff --git a/examples/file/Python/tx_file.py b/examples/file/Python/tx_file.py new file mode 100644 index 0000000..5ae47f4 --- /dev/null +++ b/examples/file/Python/tx_file.py @@ -0,0 +1,45 @@ +from time import sleep +from pySerialTransfer import pySerialTransfer as txfer + + +file = 'Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc, quis gravida magna mi a libero. Fusce vulputate eleifend sapien. Vestibulum purus quam, scelerisque ut, mollis sed, nonummy id, metus. Nullam accumsan lorem in dui. Cras ultricies mi eu turpis hendrerit fringilla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; In ac dui quis mi consectetuer lacinia. Nam pretium turpis et arcu. Duis arcu tortor, suscipit eget, imperdiet nec, imperdiet iaculis, ipsum. Sed aliquam ultrices mauris. Integer ante arcu, accumsan a, consectetuer eget, posuere ut, mauris. Praesent adipiscing. Phasellus ullamcorper ipsum rutrum nunc. Nunc nonummy metus. Vestib' +fileSize = len(file) +fileName = 'test.txt' + + +if __name__ == '__main__': + try: + link = txfer.SerialTransfer('COM11') + + link.open() + sleep(5) + + while True: + link.send(link.tx_obj(fileName)) + + numPackets = int(fileSize / (txfer.MAX_PACKET_SIZE - 2)) + + if numPackets % txfer.MAX_PACKET_SIZE: + numPackets += 1 + + + + for i in range(numPackets): + fileIndex = i * txfer.MAX_PACKET_SIZE + dataLen = txfer.MAX_PACKET_SIZE - 2 + + if (fileIndex + (txfer.MAX_PACKET_SIZE - 2)) > fileSize: + dataLen = fileSize - fileIndex + + dataStr = file[fileIndex:dataLen] + + sendSize = link.tx_obj(fileIndex, val_type_override='h') + sendSize = link.tx_obj(dataStr, start_pos=sendSize) + link.send(sendSize) + + sleep(1) + + sleep(10) + + except KeyboardInterrupt: + link.close() \ No newline at end of file