Skip to content

Commit

Permalink
Add SerialTransfer.h compatible examples (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
PowerBroker2 authored Aug 14, 2021
1 parent b09c4e1 commit e6e8deb
Show file tree
Hide file tree
Showing 12 changed files with 416 additions and 0 deletions.
32 changes: 32 additions & 0 deletions examples/data/Arduino/rx_data/rx_data.ino
Original file line number Diff line number Diff line change
@@ -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);
}
}
39 changes: 39 additions & 0 deletions examples/data/Arduino/tx_data/tx_data.ino
Original file line number Diff line number Diff line change
@@ -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);
}
50 changes: 50 additions & 0 deletions examples/data/Python/rx_data.py
Original file line number Diff line number Diff line change
@@ -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()
31 changes: 31 additions & 0 deletions examples/data/Python/tx_data.py
Original file line number Diff line number Diff line change
@@ -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()
22 changes: 22 additions & 0 deletions examples/datum/Arduino/rx_datum/rx_datum.ino
Original file line number Diff line number Diff line change
@@ -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);
}
}
22 changes: 22 additions & 0 deletions examples/datum/Arduino/tx_datum/tx_datum.ino
Original file line number Diff line number Diff line change
@@ -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);
}
32 changes: 32 additions & 0 deletions examples/datum/Python/rx_datum.py
Original file line number Diff line number Diff line change
@@ -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()
20 changes: 20 additions & 0 deletions examples/datum/Python/tx_datum.py
Original file line number Diff line number Diff line change
@@ -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()
39 changes: 39 additions & 0 deletions examples/file/Arduino/rx_file/rx_file.ino
Original file line number Diff line number Diff line change
@@ -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<myTransfer.bytesRead; i++)
{
file[fileIndex] = (char)myTransfer.packet.rxBuff[i]);
fileIndex++;
}
}
}
}
43 changes: 43 additions & 0 deletions examples/file/Arduino/tx_file/tx_file.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "SerialTransfer.h"


SerialTransfer myTransfer;

const int fileSize = 2000;
char file[fileSize] = "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";
char fileName[8] = "test.txt";


void setup()
{
Serial.begin(115200);

myTransfer.begin(Serial);
}


void loop()
{
myTransfer.sendDatum(fileName); // Send filename

uint16_t numPackets = fileSize / (MAX_PACKET_SIZE - 2); // Reserve one byte for current file index

if (fileSize % MAX_PACKET_SIZE) // Add an extra transmission if needed
numPackets++;

for (uint16_t i=0; i<numPackets; i++) // Send all data within the file across multiple packets
{
uint16_t fileIndex = i * MAX_PACKET_SIZE; // Determine the current file index
uint8_t dataLen = MAX_PACKET_SIZE - 2;

if ((fileIndex + (MAX_PACKET_SIZE - 2)) > 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);
}
41 changes: 41 additions & 0 deletions examples/file/Python/rx_file.py
Original file line number Diff line number Diff line change
@@ -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()
Loading

0 comments on commit e6e8deb

Please sign in to comment.