Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using Python pyserial
#1
Here is a sample code is written for pyserial. I need some help in improvising the code.

WSt will have
systemdate
systemtime

Serial data received in the format
data1: value \n
data2: value \n
data3: value \n
data4: value \n
This value to be read and written in excel sheet


import serial
import csv
import os
import time
def main():
   pass

if __name__ == '__main__':
    main()
    COUNT=0
    ser=serial.Serial()
    ser.port=2
    ser.baudrate=9600
    foo=open("new.csv","ab");
    result=csv.writer(foo,delimiter=',')
    result_statement=("date","time","data1","Data2","Data3","Data4")
    result.writerow(result_statement)
    foo.close()
    while(COUNT<300):
      ser.open()
      str=ser.read(500)# character are 115
      val=str.split(":")
      print "value is",val
      lines=str.split("\r\n")
      wst=[]
      for line in lines[:-1]:

            parts=line.split(":")
                #print parts
            for p in parts[1:]:
                    wst.append(p)
            #print "wst is", wst
      foo=open("new.csv","a+");
      result=csv.writer(foo,delimiter=',')
      result_statement=wst
      result.writerow(result_statement)
      COUNT=COUNT+1
      print COUNT
      foo.close()
      ser.close()
Reply
#2
I have seen some people writing code like this. How this portion I can assign "/dev/ttyS0"
Because device manager shows some port information.

I am looking for a sample code that take input and receive input.

It take user input 1-9 A-Z and character input and read the value in above mentioned in thread.


try:
  ser = serial.Serial("/dev/ttyS0", 9600) #for COM3
  ser_bytes = ser.readline()
  time.sleep(1)
  inp = ser_bytes.decode('utf-8')
  print (inp)
except:
  pass
Reply
#3
With regard to the ports this should be a guide to listing available ports

import serial.tools.list_ports

ports = list(serial.tools.list_ports.comports())

for p in ports:
	print(p)

print("\n")

for p in ports:
	print(p.name)
If the serial data is separated with a colon and terminated with "\r\n" the following should receive/strip/decode and pack the data into a list of lists suitable for the csv module. This is pseudo code and although its fairly close it still needs work to make it useable in your project. Receiving in this example is done in a separate thread.

lst_cnt=0
Main_List=[]

def serial_thread():

	input_string=""
	
	while True:

		if ser.isOpen():
			input_string=ser.readline().strip().decode("utf-8")
			create_listof_list(input_string)

def create_listof_list(my_string):

	global lst_cnt
	global Main_List

	Main_List.append([])
	Main_List[lst_cnt]=my_string.split(":")

	lst_cnt+=1
			
def save_results():

		with open('My_CSV.csv', 'w', newline="") as file:
			csvwriter = csv.writer(file)
			csvwriter.writerows(Main_List)
Reply
#4
Thanks for help ..i also like to know step involve in connect serial port
Like intialize and read and send data character here

(Feb-03-2022, 06:14 PM)Jeff_t Wrote: With regard to the ports this should be a guide to listing available ports

import serial.tools.list_ports

ports = list(serial.tools.list_ports.comports())

for p in ports:
	print(p)

print("\n")

for p in ports:
	print(p.name)
If the serial data is separated with a colon and terminated with "\r\n" the following should receive/strip/decode and pack the data into a list of lists suitable for the csv module. This is pseudo code and although its fairly close it still needs work to make it useable in your project. Receiving in this example is done in a separate thread.

lst_cnt=0
Main_List=[]

def serial_thread():

	input_string=""
	
	while True:

		if ser.isOpen():
			input_string=ser.readline().strip().decode("utf-8")
			create_listof_list(input_string)

def create_listof_list(my_string):

	global lst_cnt
	global Main_List

	Main_List.append([])
	Main_List[lst_cnt]=my_string.split(":")

	lst_cnt+=1
			
def save_results():

		with open('My_CSV.csv', 'w', newline="") as file:
			csvwriter = csv.writer(file)
			csvwriter.writerows(Main_List)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Can't transmit serial fast Python to Arduino pyserial mRKlean 0 2,373 Mar-29-2020, 08:12 PM
Last Post: mRKlean

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020