Python from pc to arduino comport for linux?

Hello, New to this whole python arduino on pc please bare with me. A friend of mine said I should use python to talk from pc to arduino maybe it can work. Normally I can do a desktop application in C language. However that is for windows. If I want to make it for linux I need to make something else. I wanted to find a Universal way to work on windows and linux. and My friend told me to try python. I never never full python script to do anything. however I have played around with them to change things around.

My question is I know in order to use arduino on a pc I have to use com ports. I know they change all the time. I was wondering if anyone knows of a way it can detect the port the arduino is on so I don't have to go around searching for it. Linux is a lot different then windows I admit that and finding things in terminal is a little harder for me. Someone please help me to figure this out?

Joseph

I’ve written a small tutorial on interfacing with Python. See Two ways communication between Python3 and Arduino

I've moved your topic to a more suitable location on the forum.

to get code portable (so you don't have to edit the source code) between Windows and Linux I tend to use Java but Python is an alternative
have a look at this python terminal emulator

# simple GUI terminal emulator

import os
import sys
import serial
import PySimpleGUI as psg
from serial.tools import list_ports     # list the serial ports

# scan serial ports open first one found
if __name__ == "__main__":
    for port in list_ports.comports():
        if "USB" in port.hwid:
            name=port.name
            if os.name == 'posix':   # if linux system add /dev/ to name
                name='/dev/' + name
            #print(f"Name: {port.name}")
            print("Name:" + name)
            print(f"Description: {port.description}")
            serial1=serial.Serial(port=name,baudrate=115200) # open port
# if no ports found or open fails            
try: serial1
except:
   print("no serial port found/opened - exiting program")
   sys.exit()
         

text = "" #Serial port " + port.description + " open - enter text\n"
#l1 = psg.Text('Simple terminal emulator', font=('Arial Bold', 20), expand_x=True, justification='center')
t1 = psg.Multiline(text, autoscroll=True, enter_submits=True,  key='INPUT', expand_x=True, expand_y=True, justification='left')
layout = [[t1]]
window = psg.Window('Simple terminal emulator port ' + port.description , layout, size=(600, 700))
#ml = window['-INPUT-']
while True:
   # serial data received?  if so read byte and print it
   while serial1.in_waiting > 0:
            char = serial1.read()                                     # read character received
            print(str(char,'ASCII'),end='')
            window['INPUT'].update(str(char,'ASCII'), text_color_for_value='green', append=True)  #append it to window
   event, values = window.read(timeout=500000)     # check for event
   print(event, values)
   if event == psg.WIN_CLOSED:                  # if window closed
      break
   if event == "_Enter":                       # if characters input
       print("enter")
   if event == 'INPUT'  + "_Enter":                       # if characters input
      #text=str(values['INPUT'])
      print("screen text " + text + "'")     
      #window['-IN-'].update("\ntransmitting>" + text[len(text)-1], text_color_for_value='blue', append=True)   #append text to window
      print("entered character " + str(len(text)) + " " + text[len(text)-1])     
      serial1.write(text[len(text)-1].encode())

window.close
serial1.close()
print("window and serial port closed")

it scans the serial ports and opens the first one it finds
e.g. Windows

Raspberry Pi

also try a web search for python terminal emulators

@sterretje Thank you. I didn't know where to put this at.

@J-M-L Thank you. I'm looking at it. very interesting. I never never used python myself but saw great thanks about it. I'm reading your article now.

@horace what I'm trying to see I can do is. deploy this and have it connect to a pc or server to be able to read out the computer information such as the hard drive, memory and power of how much it is taking. then save all that to an SD card. problem is I don't normally know what comport it is on and it does change over time after reboots. Sometime it stays the same sometime it changes. So I asked my friend what can I do about that so I don't have to keep going in there and changing it manually.

He told me to write a python script that detect which port it is on and talk to someone in the arduino community becuase it doesn't work with arduinoat all. That is why I'm here.

what I usually do is scan the available COM ports prompting each for a response, e.g. transmit a '?'
the required target device would be programmed to respond with some identifier, e.g. text such as "ESP32 pump control 1"

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.