r/pythonhelp • • Oct 16 '21

Can't Read from Serial - PermissionError(13, 'Access is denied.', None, 5)

Hello!

I'm using the below code to read serial data being transferred by a MicroBit, save it to a dictionary and apply that information to a function. For the entire span of time that I've been using this code, the connection has worked fine. Unfortunately, for no apparent reason at all, it's been returning the error in the title.

The port works fine and the MicroBit is functioning.

serialPort = serial.Serial(port="COM3", baudrate=115200, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE)
serialString = ""

def backgroundSerial():
    name = multiprocessing.current_process().name
    serial_count = 0
    while True:
        if (serialPort.in_waiting > 0):
            serialString = serialPort.readline()
            reading = serialString.decode('Ascii')
            reading = reading.strip('\r\n')
            reading = reading.strip()
            reading = reading.strip('voltage:')
            reading = int(float(reading))
            serial_count += 1
            voltage_dic[serial_count] = reading
            print(voltage_dic)
            checkVoltage(voltage_dic, serial_count)
            time.sleep(1)
            return checkVoltage, voltage_dic

if __name__ == '__main__':
    procname = multiprocessing.Process(target=backgroundSerial, name="proc 1")
    procname.daemon = True
    procname.start()

def checkVoltage(voltage_dic, serial_count):
    if serial_count > 1:
        serial_count_old = serial_count - 1
        previous_value = voltage_dic.get(serial_count_old)
        current_value = voltage_dic.get(serial_count)
        previous_value = int(previous_value)
        current_value = int(current_value)
        voltage_compare = previous_value - current_value
        voltage_compare = int(voltage_compare)
        if voltage_compare > 8:
            print('good to go')
            return True
        else:
            return False
    else:
        return False

Any help that you guys might be able to provide would be massively appreciated.

2 Upvotes

0 comments sorted by