Nov-18-2018, 05:00 PM
(This post was last modified: Nov-18-2018, 05:00 PM by MichaelDean.)
I'm a newbie to Python. My task is to fetch serial port output (from Arduino), then push the output to the Microsoft SQL Database using Python.
My Arduino project is recording student attendance using fingerprint. Every time a student has scanned his fingerprint, the Arduino will generate output like this:
My Arduino project is recording student attendance using fingerprint. Every time a student has scanned his fingerprint, the Arduino will generate output like this:
Output:{'SID':1,'Date':2018/11/18,'Time':12:17:36}
{'SID':2,'Date':2018/11/18,'Time':12:28:02}
...
...
I would like to let Python code to fetch these outputs, and push it to Microsoft SQL Database. And I want to make it real-time, which means a output will be push to MSSQL every single time when students scanned their fingerprint. So I have a while loop like this in my Python:import pyodbc import serial import time import datetime import ast cnxn = pyodbc.connect("Driver={SQL Server Native Client 11.0};" "Server=DESKTOP-H7KQUT1;" "Database=SAOS1;" "Trusted_Connection=yes;") cursor = cnxn.cursor() arduino = serial.Serial('COM4', 9600, timeout=.1) while True: data = arduino.readline()[:-2].decode("utf-8") if data!="": Atd_Date = ast.literal_eval(data)['Date'] Atd_InTime = ast.literal_eval(data)['Time'] SID = ast.literal_eval(data)['SID'] tsql = "INSERT INTO attendance (Atd_Date, Atd_InTime, SID) VALUES (?,?,?);" with cursor.execute(tsql,Atd_Date,Atd_InTime,SID): print ('Successfuly Inserted!')Unfortunately, it won't work as expected. I can't really fetch the output from serial and push it to MSSQL. I got this error as well:
Error:line 20, in Atd_Date = ast.literal_eval(data)['Date'] node_or_string = parse(node_or_string, mode='eval')
These two websites have the similar case with me, first and second. Am I doing in a correct way?