Python Check Serial Port Available

Автор:
Python Check Serial Port Available 4,3/5 6549 reviews

Thank you for helping me understand socket programming. As a small improvement to the script I would suggest the following: Instead of writing the exceptions like this: except socket.error: print 'Couldn't connect to server' sys.exit() I would write: except socket.error: sys.exit('Couldn't connect to server') This has the following advantages: - The return value to the Shell is 'failure' instead of success. - The error message (should) be written to the error stream instead of standard out. - It is shorter:-) •. I am really new to python (so new that I have never attempted coding in it. But I found this post and it seems like the easiest way to achieve what I am looking for) I and was wondering if anyone would be willing to help.

I need this code to scan only certain ports (I have 6) which can be manually put into the code and then run continuously in a loop. This: for port in range(1,1025): tells python to give 'port' the value of 1 to 1025 right? How do I change this complete statement to select only certain ports? And if someone can please point me in the direction of creating a loop I would be greatful.

I am connecting with my Arduino through a USB port and sending data to it by using PySerial module. At first I can check if the device is connected by using this code: try: ser = serial.Serial('COM3', 9600) except serial.serialutil.SerialException: print 'Arduino not connected' Now what I want to do is to check periodically if the Arduino is still connected to the computer. I tried ser.isOpen() but this returns true even if the Arduino is disconnected. I would also like to know how to reconnect the device. I mean once you disconnect the device the program can no longer send any data to Arduino. I suggest to use a python thread class to istantiate a serial connection, in the run methos put your while loop, set an var that you use for kill iy at the end, the second public var that you use for store data if have receive and load data in main method.

Most of the answers propose 2 approaches: • In some point of the code, send some sort of message through serial to check if your device is still alive • Start a separate thread and continuously check if the device is alive by opening a comunication The problem with the first solution is that you are not always checking the connection, but only checking in some specific points: this solution isn't very elegant and if badly written could even be not working. The second solution solves the problem of the first solution, but introduces a new problem: checking the connection, or worst sending a message, in a threaded loop will cause problem or may even interrupt the connection to the device from other functions.

It doesn't seem that pySerial closes the serial port after readline, so unless you're doing more work that uses the serial port, you can just move the ser.close() down. – Jacob Hayes Apr 30 '14 at 18:46.

Open serial port python

A solution that allows you to constantly check the connection without monopolizing the comunication involves the reading of the existing COM: import serial.tools.list_ports myports = [tuple(p) for p in list(serial.tools.list_ports.comports())] print myports output: [(u'COM3', u'Arduino Due Programming Port (COM3)', u'some more data.' ), (u'COM6', u'USB Serial Port (COM6)', u'some more data.' ), (u'COM100', u'com0com - serial port emulator (COM100)', u'some more data.'

Bot tales of pirates. )] then we save the tuple that contains our port: arduino_port = [port for port in myports if 'COM3' in port ][0] then we create a function that checks if this port is still present: import time def check_presence(correct_port, interval=0.1): while True: myports = [tuple(p) for p in list(serial.tools.list_ports.comports())] if arduino_port not in myports: print 'Arduino has been disconnected!' Break time.sleep(interval) At last, we run this function as a daemon thread: import threading port_controller = threading.Thread(target=check_presence, args=(arduino_port, 0.1,)) port_controller.setDaemon(True) port_controller.start() in this way, you'll check each 0.1 secs if the arduino is still connected, and the thread will end when arduino is disconnected or all other activities have ended.