-1

im trying to read data on a COM port using python and the COM port allows me to access only one application at a time..

example: if i configure the putty or hyper terminal then i can read the tag data on the putty/hyperterminal console but when i try and run the python code ,then it shows me an error

as u see in pyserial image that i have attached below..it says "access denied'

later if i first run the python code, the code runs without error .. but the configure for putty/hyperterminl doesnt happen.. it say "unable to open the COM port" in putty/hyperterminal

https://i.stack.imgur.com/FUALN.png

seva titov
  • 11,720
  • 2
  • 35
  • 54

2 Answers2

0

Use this code to read data from serial port : If you want to read from COM1 just replace 27 with 0,for COM2 replace 27 with 1 and so on.'Access Denies' is shown if your COM port is already in use so try to re-insert your device in the USB and try or simple close the app using the COM port

from serial import *
import sys
try:
        ser=Serial(27)
        print("port opened")
        #ser.stopbits=2
        while 1:
            data=ser.read()
            sys.stdout.write(data.decode())

except:
        ser.close()
Aakash
  • 83
  • 1
  • 12
0
Before accessing this port. You need to close another application that is accessing it. And then, you can use the following source code: 
   #!/usr/bin/env python3
    import serial
    
    ser = serial.Serial(
     port='ComPort',
     baudrate = 2400,
     parity=serial.PARITY_EVEN,
     stopbits=serial.STOPBITS_ONE,
     bytesize=serial.SEVENBITS,
     timeout=None
    )
    
    while 1:
     x = ser.readline()
     print(x)
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Dec 24 '21 at 00:42