python-Arduino serial port transfers data to computer and saves it to excel table

Cause: the school canal cup reported a project to make a weather forecast device. I ran BME280 module with arduino, and realized two Arduino master-slave transmission with Bluetooth module. But in order to analyze, we need to extract data. So I wrote a PC program with python, realized the serial communication between arduiho and computer with pyserial module, then wrote the EXCEL form with xlwt module, and used time module to get the file name of excel.

 1 import xlwt
 2 import time
 3 import serial
 4 #Setting table style
 5 def set_style(name,height,bold=False):
 6     style = xlwt.XFStyle()
 7     font = xlwt.Font()
 8     font.name = name
 9     font.bold = bold
10     font.color_index = 4
11     font.height = height
12     style.font = font
13     return style
14 
15 #write Excel
16 def write_excel():
17     if serial.isOpen():
18         print ('Serial Port Opened\n')
19     f = xlwt.Workbook()
20     sheet1 = f.add_sheet('arduino_data',cell_overwrite_ok=True)
21     row0 = ["temp","pres","hum"]
22     time1=time.localtime(time.time())
23     #Write the first line
24     for i in range(len(row0)):
25         sheet1.write(0,i,row0[i],set_style('Times New Roman',220,True))
26     i=1
27     time.sleep(5)
28     serial.flushInput() 
29     while True:
30         try:
31             size = serial.inWaiting()
32             if size != 0:
33                 response = serial.read(size)        # Read the content and display it
34                 s=response.decode('utf-8').rstrip('\r\n').split('\t')
35                 if len(s)!=3:
36                     serial.flushInput()
37                     continue
38                 else:
39                     try:
40                         for j in range(len(s)):
41                             sheet1.write(i,j,int(s[j]),set_style('Times New Roman',220,False))
42                         print(s)
43                         serial.flushInput()                 # Clear the receive buffer
44                         i = i+1
45                         time.sleep(0.5)
46                     except ValueError:
47                         serial.flushInput()
48                         continue
49         except KeyboardInterrupt:
50             time2=time.localtime(time.time())
51             f.save(r'C:\Users\10020\Desktop\arduino_data\{0}.{1}_{2:0>2d}.{3:0>2d}.{4:0>2d}-{5}.{6}_{7:0>2d}.{8:0>2d}.{9:0>2d}.xls'.format\
52                    (time1[1],time1[2],time1[3],time1[4],time1[5],
53                     time2[1],time2[2],time2[3],time2[4],time2[5]))
54             serial.close()
55             print(time1)
56             print(time2)
57             quit()
58     
59 if __name__ == '__main__':
60     serial = serial.Serial('COM3',9600,timeout=2)
61     write_excel()

 

After running the code, the data of Arduino is read from the serial port and written to excel. Press Ctrl+c to abort the code process, at which time an xls file named "start-end runtime" will be generated under the folder of C: Users 10020 Desktop arduino_data.

Running effect of code:

 

 

It should be noted that:

  1. Serial port and baud rate are determined by COM port displayed on the computer and arduino baud rate set up on the computer.
  2. arduino sends bytes to the computer through serial port, which needs to be coded into utf-8 and processed.
  3. Clear data caching after each acceptance

 2019-10-14-14:44:49

Keywords: Python Excel

Added by adige on Mon, 14 Oct 2019 19:50:21 +0300