Initial commit
commit
7d93f770dc
@ -0,0 +1,3 @@
|
|||||||
|
# Learn Arduino
|
||||||
|
|
||||||
|
Experiments in learning arduino and communication protocols
|
@ -0,0 +1 @@
|
|||||||
|
env/
|
@ -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<bytes>\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()
|
@ -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);
|
||||||
|
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue