'How to send my data from Arduino to my computer through the ports?
I am generating random data on an Arduino that stays connected to my computer and I would like to transfer this data to a local database on my computer or to a port.
I have tried to adapt the code from this post but that uses the port /dev/ttyACM0
and I'm on Windows so I replaced this part with COM7
Yet it seems to wait forever:
(venv) C:\Users\antoi\Documents\Programming\Work\two_way_communication>python main.py
Serial port COM7 opened Baudrate 115200
Waiting for Arduino to reset
Enough blah, blah, blah. Here are my codes.
Arduino side, which uses Port COM 7, to generate data:
void setup() {
Serial.begin(115200);
randomSeed(analogRead(0));
}
void loop() {
SendSensorData();
}
//function to send sensor data
void SendSensorData() {
String sensorData1,sensorData2,sensorData3, postData;
sensorData1=random(25, 200);
sensorData2=random(25, 200);
sensorData3=random(25, 200);
//Post Data
postData = "sensor1=" + sensorData1 + "&sensor2=" + sensorData2+ "&sensor3=" + sensorData3;
Serial.println(postData);
delay(1000);
}
And, PC side main.py
, to receive the data from the Arduino:
import serial
import time
startMarker = '<'
endMarker = '>'
dataStarted = False
dataBuf = ""
messageComplete = False
# ========================
# ========================
# the functions
def setupSerial(baudRate, serialPortName):
global serialPort
serialPort = serial.Serial(port=serialPortName, baudrate=baudRate, timeout=0, rtscts=True)
print("Serial port " + serialPortName + " opened Baudrate " + str(baudRate))
waitForArduino()
# ========================
def sendToArduino(stringToSend):
# this adds the start- and end-markers before sending
global startMarker, endMarker, serialPort
stringWithMarkers = (startMarker)
stringWithMarkers += stringToSend
stringWithMarkers += (endMarker)
serialPort.write(stringWithMarkers.encode('utf-8')) # encode needed for Python3
# ==================
def recvLikeArduino():
global startMarker, endMarker, serialPort, dataStarted, dataBuf, messageComplete
if serialPort.inWaiting() > 0 and messageComplete == False:
x = serialPort.read().decode("utf-8") # decode needed for Python3
if dataStarted == True:
if x != endMarker:
dataBuf = dataBuf + x
else:
dataStarted = False
messageComplete = True
elif x == startMarker:
dataBuf = ''
dataStarted = True
if (messageComplete == True):
messageComplete = False
return dataBuf
else:
return "XXX"
# ==================
def waitForArduino():
# wait until the Arduino sends 'Arduino is ready' - allows time for Arduino reset
# it also ensures that any bytes left over from a previous message are discarded
print("Waiting for Arduino to reset")
msg = ""
while msg.find("Arduino is ready") == -1:
msg = recvLikeArduino()
if not (msg == 'XXX'):
print(msg)
# ====================
# ====================
# the program
if __name__ == "__main__":
setupSerial(115200, "COM7")
count = 0
prevTime = time.time()
while True:
# check for a reply
arduinoReply = recvLikeArduino()
if not (arduinoReply == 'XXX'):
print("Time %s Reply %s" % (time.time(), arduinoReply))
# send a message at intervals
if time.time() - prevTime > 1.0:
sendToArduino("this is a test " + str(count))
prevTime = time.time()
count += 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|