'How can i use COM port with 2 code at the same time

I wanna communicate rpi pico and pc. But when I run one of my code, the other code giving me an error which says "Access Denied" probably because the COM port still in use. How can i comminucate with each other.

The "Access Denied" Error ↓

serial.serialutil.SerialException: could not open port 'COM6': PermissionError(13, 'Access Denied.', None, 5)

The code which i wrote for rpi is below ↓

import sys
import machine

led = machine.Pin(25, machine.Pin.OUT)

def led_on():
    led(1)

def led_off():
    led(0)


while True:
    # read a command from the host
    v = sys.stdin.readline().strip()

    # perform the requested action
    if v.lower() == "on":
        led_on()
    elif v.lower() == "off":
        led_off()

The code which i wrote for pc is below ↓

import serial
import time
import os

# open a serial connection
s = serial.Serial("COM6",9600)

# blink the led
while True:
    s.write(b"on\n")
    print("on")
    time.sleep(1)
    s.write(b"off\n")
    print("off")
    time.sleep(1)

Whichever code I run first the other one gives me "access denied" error



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source