From 7d93f770dcbb6acbf2b79de4f37ada4aa9d3bb17 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Mon, 18 Jan 2021 23:40:38 -0500 Subject: [PATCH] Initial commit --- README.md | 3 ++ python_serial/.gitignore | 1 + python_serial/serial_client.py | 25 ++++++++++++ teensy/Binary/Binary.ino | 74 ++++++++++++++++++++++++++++++++++ teensy/Blink/Blink.ino | 27 +++++++++++++ 5 files changed, 130 insertions(+) create mode 100644 README.md create mode 100644 python_serial/.gitignore create mode 100644 python_serial/serial_client.py create mode 100644 teensy/Binary/Binary.ino create mode 100644 teensy/Blink/Blink.ino diff --git a/README.md b/README.md new file mode 100644 index 0000000..8a424ef --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Learn Arduino + +Experiments in learning arduino and communication protocols diff --git a/python_serial/.gitignore b/python_serial/.gitignore new file mode 100644 index 0000000..bdaab25 --- /dev/null +++ b/python_serial/.gitignore @@ -0,0 +1 @@ +env/ diff --git a/python_serial/serial_client.py b/python_serial/serial_client.py new file mode 100644 index 0000000..01aa9b4 --- /dev/null +++ b/python_serial/serial_client.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +""" +A very basic serial client that can unpack a binary 32 int from a message +and print it to the console. + +message: b'H\r\n' +""" + +import serial + +PORT = '/dev/cu.usbmodem2667001' + +def main(): + print('Connecting to serial port: ', PORT) + with serial.Serial(PORT, 9600, timeout=1) as ser: + _ = ser.readline() + print('Connection made') + while True: + msg = ser.readline() + val = int.from_bytes(msg[1:-2], byteorder='little', signed=False) + print('Recieved: ', val) + + +if __name__ == '__main__': + main() diff --git a/teensy/Binary/Binary.ino b/teensy/Binary/Binary.ino new file mode 100644 index 0000000..ab647bf --- /dev/null +++ b/teensy/Binary/Binary.ino @@ -0,0 +1,74 @@ +/* + Binary Send + + This code is meant to demonstrate the sending of binary information + from the arduino to a serial client. See the serial_client.py file + which can read the payload being sent + +*/ + +unsigned int large_number = 3257493648; +/* +Decimal: 3257493648 +Hex: C2296890 +Binary: 11000010 00101001 01101000 10010000 +b1 byte: 11000010 +b1 hex: C2 +b2 byte: 101001 +b2 hex: 29 +b3 byte: 1101000 +b3 hex: 68 +b4 byte: 10010000 +b4 hex: 90 +Number: 11000010001010010110100010010000 + */ + +// unpacks the int into bytes +byte b1 = large_number >> 24; +byte b2 = large_number >> 16; +byte b3 = large_number >> 8; +byte b4 = large_number; + +// Places the bytes in little endian +byte byteBuffer[4] = {b4, b3, b2, b1}; + +void setup() { + //Initialize serial and wait for port to open: + Serial.begin(9600); + while (!Serial) { + ; // wait for serial port to connect. Needed for native USB port only + } + + // prints title with ending line break + Serial.print("Decimal: ");Serial.print(large_number); Serial.println(); + Serial.print("Hex: ");Serial.print(large_number, HEX); Serial.println(); + Serial.print("Binary: ");Serial.print(large_number, BIN); Serial.println(); + Serial.print("b1 byte: ");Serial.print(b1, BIN); Serial.println(); + Serial.print("b1 hex: ");Serial.print(b1, HEX); Serial.println(); + Serial.print("b2 byte: ");Serial.print(b2, BIN); Serial.println(); + Serial.print("b2 hex: ");Serial.print(b2, HEX); Serial.println(); + Serial.print("b3 byte: ");Serial.print(b3, BIN); Serial.println(); + Serial.print("b3 hex: ");Serial.print(b3, HEX); Serial.println(); + Serial.print("b4 byte: ");Serial.print(b4, BIN); Serial.println(); + Serial.print("b4 hex: ");Serial.print(b4, HEX); Serial.println(); + Serial.print("Number: "); Serial.print(large_number, BIN); Serial.println(); + Serial.println("---------------------------------"); + Serial.print("high byte: ");Serial.print(highByte(large_number >> 16), BIN); Serial.println(); + Serial.print("high hex: ");Serial.print(highByte(large_number >> 16), HEX); Serial.println(); + Serial.print("low byte: ");Serial.print(lowByte(large_number >> 16), BIN); Serial.println(); + Serial.print("low hex: ");Serial.print(lowByte(large_number >> 16), HEX); Serial.println(); + Serial.println("---------------------------------"); + Serial.print("high byte: ");Serial.print(highByte(large_number), BIN); Serial.println(); + Serial.print("high hex: ");Serial.print(highByte(large_number), HEX); Serial.println(); + Serial.print("low byte: ");Serial.print(lowByte(large_number), BIN); Serial.println(); + Serial.print("low hex: ");Serial.print(lowByte(large_number), HEX); Serial.println(); + +} + +void loop() { + Serial.write('H'); + Serial.write(byteBuffer, sizeof(byteBuffer)); + Serial.println(); + delay(1000); + +} diff --git a/teensy/Blink/Blink.ino b/teensy/Blink/Blink.ino new file mode 100644 index 0000000..577e0fb --- /dev/null +++ b/teensy/Blink/Blink.ino @@ -0,0 +1,27 @@ +/* + Blink + Turns on an LED on for one second, then off for one second, repeatedly. + + This example code is in the public domain. + */ + +// Pin 13 has an LED connected on most Arduino boards. +// Pin 11 has the LED on Teensy 2.0 +// Pin 6 has the LED on Teensy++ 2.0 +// Pin 13 has the LED on Teensy 3.0 +// give it a name: +int led = 13; + +// the setup routine runs once when you press reset: +void setup() { + // initialize the digital pin as an output. + pinMode(led, OUTPUT); +} + +// the loop routine runs over and over again forever: +void loop() { + digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) + delay(100); // wait for a second + digitalWrite(led, LOW); // turn the LED off by making the voltage LOW + delay(100); // wait for a second +}