Skip to content
Snippets Groups Projects
Commit 3cc6cff6 authored by Kai Junge's avatar Kai Junge
Browse files

Windows testing.

parent 8c9b5c6e
Branches
No related tags found
No related merge requests found
......@@ -35,9 +35,8 @@ class Arduino:
try:
self._rawReceivedMessage = self.arduino.readline().decode('utf-8')[:-2]
except:
pass
time.sleep(0.001)
#pass
time.sleep(0.000001)
def _startReadingThread(self):
self.__thread = Thread(target=self._serial_readline)
......@@ -89,7 +88,6 @@ class Arduino:
msgName = singleMsg.split(";")[0]
msgPayload = singleMsg.split(";")[1]
if msgName == "echo_python":
self._echo_python_msg = msgPayload
else:
......@@ -100,6 +98,11 @@ class Arduino:
else:
return False
#print(receivedMessageTemp["pot"], self.receivedMessages["pot"])
if receivedMessageTemp["pot"] != self.receivedMessages["pot"]:
print("asdfasdfasfdasd")
print(receivedMessageTemp["pot"])
if receivedMessageTemp == self.receivedMessages:
self.newMsgRecieved = False
......@@ -150,11 +153,12 @@ class Arduino:
break
if self.handshakeStatus:
# while 1:
# self.receive_message()
# if self._echo_python_msg == "NO_PYTHON_MESSAGE":
# break
print("Successfull handshake with " + self.descriptiveDeviceName)
while 1:
self.receive_message()
if self._echo_python_msg == "NO_PYTHON_MESSAGE":
break
time.sleep(1)
else:
print("!! Handshake failed with " + self.descriptiveDeviceName + " !!")
......@@ -199,7 +203,7 @@ class Arduino:
else:
print("Messege received from the arduino: ", self.receivedMessages)
time.sleep(0.000001)
#time.sleep(0.0001)
return True
......@@ -231,7 +235,7 @@ class Arduino:
for key, value in self.receivedMessages.items():
if value is None:
print("Check if message name: ", key, " agrees with the arduino side")
print("Check if message names: ", key, " agrees with the arduino side")
########
......
from comms_wrapper import *
from time import sleep, time
def main():
key = Key()
arduino1 = Arduino(descriptiveDeviceName="myArduino", portName="COM4", baudrate=115200)
arduino1.define_message_names(["pot"])
arduino1.connect_and_handshake()
timer = time()
while 1:
curr_time = round(time() - timer, 5)
curr_time = str(curr_time)[:5]
arduino1.receive_message(printOutput = False)
#print(arduino1._rawReceivedMessage)
# if arduino1.newMsgRecieved:
# print(curr_time, arduino1.receivedMessages)
if __name__ == '__main__':
main()
#include "pyCommsLib.h"
#include "Arduino.h"
#define size_of_array(arr) sizeof(arr) / sizeof(*arr)
String rawMsgFromPython = "NO_PYTHON_MESSAGE";
String payload = "";
void load_msg_to_python(String* msgName, String* msg, int numOfMsg) {
// If we have the same number of data compared to the message
payload = "";
for (int i = 0; i < numOfMsg; i++) {
payload.concat(msgName[i]);
payload.concat(";");
payload.concat(msg[i]);
payload.concat(":");
}
}
void receive_msg_from_python() {
String msg = "";
while (Serial.available()) {
if (Serial.available()) {
msg = Serial.readStringUntil('\n');
}
}
if (msg != "") {
rawMsgFromPython = msg;
}
}
String latest_received_msg() {
return rawMsgFromPython;
}
void init_python_communication() {
while (true) {
// if the python side sent a message
if (Serial.available() > 0) {
String rawMsgFromPython = Serial.readStringUntil('\n');
if (rawMsgFromPython == "handshake1") {
break;
}
}
}
long timer = millis();
while (millis() - timer < 500) {
Serial.println("handshake2");
}
while (Serial.available()) {
Serial.read();
}
}
void sync() {
receive_msg_from_python();
String final_payload = "<echo_python;";
final_payload.concat(latest_received_msg());
final_payload.concat(":");
final_payload.concat(payload);
final_payload.concat(">");
Serial.println(final_payload);
}
#ifndef PYCOMMSLIB_H
#define PYCOMMSLIB_H
#define size_of_array(arr) sizeof(arr) / sizeof(*arr)
#include <Arduino.h>
void load_msg_to_python(String* msgName, String* msg, int numOfMsg);
String latest_received_msg();
void init_python_communication();
void sync();
#endif
#include "pyCommsLib.h"
String msgName[] = {"pot"};
String dataCarrier[1];
void setup() {
// Start the serial communication. The baudrate is arbiturary.
Serial.begin(115200);
// Connect with the Python side
init_python_communication();
}
long timer = millis();
void loop() {
int pot = analogRead(A0);
//Serial.println(pot);
dataCarrier[0] = String(pot);
load_msg_to_python(msgName, dataCarrier, size_of_array(msgName));
sync();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment