大佬教程收集整理的这篇文章主要介绍了serial.serialutil.SerialException: 无法打开端口 'COM4': PermissionError(13, 'Access is denied.', None, 5),大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经用com4端口连接了板esp8266通用模块。
我的python代码是:
import serial # add serial library for serial communication
import pyautogui # add pyautogui library for progrAMMatically controlling the mouse and keyboard.
Arduino_serial = serial.serial('COM4',9600) # Initialize serial and Create serial port object called Arduino_serial
print(Arduino_serial.is_open)
while 1:
incoming_data = str (Arduino_serial.readline()) # read the serial data and print it as line
print ("incoming_data") # print the incoming serial data
if 'next' in incoming_data: # if incoming data is 'next'
pyautogui.hotkey('ctrl','pgdn') # perform "ctrl+pgdn" operation which moves to the next tab
if 'prevIoUs' in incoming_data: # if incoming data is 'prevIoUs'
pyautogui.hotkey('ctrl','pgup') # perform "ctrl+pgup" operation which moves to the prevIoUs tab
if 'down' in incoming_data: # if incoming data is 'down'
#pyautogui.press('down') # performs "down arrow" operation which scrolls down the page
pyautogui.scroll(-100)
if 'up' in incoming_data: # if incoming data is 'up'
#pyautogui.press('up') # performs "up arrow" operation which scrolls up the page
pyautogui.scroll(100)
if 'change' in incoming_data: # if incoming data is 'change'
pyautogui.keyDown('alt') # performs "alt+tab" operation which switches the tab
pyautogui.press('tab')
pyautogui.keyUp('alt')
Arduino_serial.close()
incoming_data = ""; # clears the data
Arduino v.1.8.15 代码:
const int trigPin1 = 11; // the number of the trigger output pin ( sensor 1 )
const int echoPin1 = 10; // the number of the echo input pin ( sensor 1 )
const int trigPin2 = 6; // the number of the trigger output pin ( sensor 2 )
const int echoPin2 = 5; // the number of the echo input pin ( sensor 2 )
////////////////////////////////// variables used for distance calculation
long duration;
int distance1,distance2;
float r;
unsigned long temp=0;
int temp1=0;
int l=0;
////////////////////////////////
voID find_distance (voID);
// this function returns the value in cm.
/*we should not trigger the both ultrasonic sensor at the same time.
it might cause error result due to thE intraction of the both soundswaves.*/
voID find_distance (voID)
{
digitalWrite(trigPin1,LOW);
delaymicroseconds(2);
digitalWrite(trigPin1,HIGH);
delaymicroseconds(10);
digitalWrite(trigPin1,LOW);
duration = pulseIn(echoPin1,HIGH,5000);// here this pulsein function wont wait more then 5000us for the ultrasonic sound to came BACk. (due to this it wont measure more than 60cm)
// it Helps this project to use the gesture control in the defined space.
// so that,it will return zero if Distance greater then 60m. ( it Helps usually if we remove our hands infront of the sensors ).
r = 3.4 * duration / 2; // calculation to get the measurement in cm using the time returned by the pulsein function.
distance1 = r / 100.00;
/////////////////////////////////////////upper part for left sensor and lower part for right sensor
digitalWrite(trigPin2,LOW);
delaymicroseconds(2);
digitalWrite(trigPin2,HIGH);
delaymicroseconds(10);
digitalWrite(trigPin2,LOW);
duration = pulseIn(echoPin2,5000);
r = 3.4 * duration / 2;
distance2 = r / 100.00;
delay(100);
}
voID setup()
{
serial.begin(115200);
pinMode(trigPin1,OUTPUT); // initialize the trigger and echo pins of both the sensor as input and output:
pinMode(echoPin1,input);
pinMode(trigPin2,OUTPUT);
pinMode(echoPin2,input);
delay (1000);
}
voID loop()
{
find_distance(); // this function will stores the current distance measured by the ultrasonic sensor in the global variable "distance1 and distance2"
// no matter what,the program has to call this "find_distance" function conTinuously to get the distance value at all time.
if(distance2<=35 && distance2>=15) // once if we placed our hands in front of the right sensor in the range between 15 to 35cm this condition becomes true.
{
temp=millis(); // store the current time in the variable temp. (" millis " Returns the number of milliseconds since the Arduino board began running the current program )
while(millis()<=(temp+300)) // this loop measures the distance for another 300 milliseconds. ( it Helps to find the difference between the swipe and stay of our hand in front of the right sensor )
find_distance();
if(distance2<=35 && distance2>=15) // this condition will true if we place our hand in front of the right sensor for more then 300 milli seconds.
{
temp=distance2; // store the current position of our hand in the variable temp.
while(distance2<=50 || distance2==0) // this loop will run untill we removes our hand in front of the right sensor.
{
find_distance(); // call this function conTinuously to get the live data.
if((temp+6)<distance2) // this condition becomes true if we moves our hand away from the right sensor (**but in front of it ). here " temp+6 " is for calibration.
{
serial.println("down"); // send "down" serially.
}
else if((temp-6)>distance2) // this condition becomes true if we moves our hand closer to the right sensor.
{
serial.println("up"); // send "up" serially.
}
}
}
else // this condition becomes true,if we only swipe in front of the right sensor .
{
serial.println("next"); // send "next" serially.
}
}
else if(distance1<=35 && distance1>=15) // once if we placed our hands in front of the left sensor in the range between 15 to 35cm this condition becomes true.
{
temp=millis();
while(millis()<=(temp+300))
{
find_distance();
if(distance2<=35 && distance2>=15) // if our hand detects in the right sensor before 300 milli seconds this condition becomes true. ( usually it happens if we swipe our hand from left to right sensor )
{
serial.println("change"); // send "change" serially.
l=1; // store 1 in variable l. ( it avoIDs the program to enter into the upcoming if condition )
break; // break the loop.
}
}
if(l==0) // this condition will become true,only if we swipe our hand in front of left sensor.
{
serial.println("prevIoUs"); // send "prevIoUs" serially.
while(distance1<=35 && distance1>=15) // this loop will rotate untill we removes our hand infront of the left sensor. this will avoID not to enter this if condition again.
find_distance();
}
l=0; // make l=0 for the next round.
}
}
获取错误:serial.serialutil.serialException:无法打开端口“COM4”:PermissionError(13,“访问被拒绝。”,无,5)
我也在不同的系统中尝试过,我认为我的代码有问题。 需要帮助。
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
以上是大佬教程为你收集整理的serial.serialutil.SerialException: 无法打开端口 'COM4': PermissionError(13, 'Access is denied.', None, 5)全部内容,希望文章能够帮你解决serial.serialutil.SerialException: 无法打开端口 'COM4': PermissionError(13, 'Access is denied.', None, 5)所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。