Platformio-vscode-ide: serial.serialutil.SerialException: could not open port 'COM5': WindowsError(5, 'Access is denied.')

Created on 12 Aug 2018  ·  2Comments  ·  Source: platformio/platformio-vscode-ide

With a freshly launched vscode, first upload works
subsequent uploads fail even after cleaning
console dump:
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError())) serial.serialutil.SerialException: could not open port 'COM5': WindowsError(5, 'Access is denied.') *** [upload] Error 1

hardware: d1 r32 (esp32) w/ ch240

Because restarting vscode each time solves the issue for one upload, it's unlikely to be caused by code running on the esp32 but in any case I'll post the code here
`#include

include

include

include

include "SSD1306Wire.h"

// #include

const int OLED_SDA=32, OLED_SCL=33, MCP_SDA=32, MCP_SCL=33;

const char* ssid = "<3 2.4";
const char* password = "love8boubou";

int screenW = 128;
int screenH = 64;
int centerX = screenW/2;
int centerY = (screenH/2);
SSD1306Wire display(0x3c, OLED_SDA,OLED_SCL);
void initDisplay()
{
display.init();
display.flipScreenVertically();
display.clear();
}

pragma region SERVER

void connectWIFI()
{
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setColor(WHITE);
display.setFont(ArialMT_Plain_10);
display.drawString(centerX, centerY, "Connecting to Wi-Fi");
display.display();
Serial.println("Connecting to Wi-Fi");
WiFi.mode(WIFI_STA);
WiFi.begin (ssid, password);
int waitCounter=0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
display.drawProgressBar(20, centerY+20, 128-40, 8, waitCounter);
waitCounter++;
waitCounter %=101;
display.display();
delay(100);
}
Serial.println("success!");
Serial.print("IP Address is: ");
Serial.println(WiFi.localIP());
}

// ------------ SERVER

WebServer server(80);
int LDRPin = 5;
int LDRReading = 0;
int milisInterval = 2000;
int count = 0;

void handleRoot() {
LDRReading = analogRead(LDRPin);
String html =" Update Rate

Light Level

";
html += LDRReading;
html +="

Count Number

";
html += count;
html +="

2000
";

server.send(200, "text/html", html);

}

void getData() {
//This is a JSON formatted string that will be served. You can change the values to whatever like.
// {"data":[{"dataValue":"1024"},{"dataValue":"23"}]} This is essentially what is will output you can add more if you like
LDRReading = analogRead(LDRPin);
String text2 = "{\"data\":[";
text2 += "{\"dataValue\":\"";
text2 += LDRReading;
text2 += "\"},";
text2 += "{\"dataValue\":\"";
text2 += count;
text2 += "\"}";
text2 += "]}";
server.send(200, "text/html", text2);
count++;
}

void Server_Task(void *parameter)
{
for (;;)
{
server.handleClient();
delay(10);
}
vTaskDelete(NULL);
}

void InitAndLaunchServer()
{
server.on("/", handleRoot);
server.on("/data", getData);
server.begin();
Serial.println("HTTP server started");
display.clear();
display.drawString(centerX, centerY-12, "connect to");
display.drawString(centerX, centerY, "http://" + WiFi.localIP().toString());
display.display();
xTaskCreatePinnedToCore(Server_Task, "web server", 8192, NULL, 2, NULL, 1);
}

pragma endregion

// Adafruit_MCP23017 mcp;//0x20-0x27
void InitRelays()
{
MCP_begin(00,MCP_SDA, MCP_SCL);
// mcp.begin(00, MCP_SDA, MCP_SCL);
for (int i=0; i<16;i++)
MCP_pinMode(i,OUTPUT);
// mcp.pinMode(i,OUTPUT);
}

int relayIndex = 0;
void SwithRelay()
{
MCP_digitalWrite(relayIndex,!MCP_digitalRead(relayIndex));
// mcp.digitalWrite(relayIndex,!mcp.digitalRead(relayIndex));
Serial.println(String(relayIndex) +"-"+ MCP_digitalRead(relayIndex));
// Serial.println(String(relayIndex) +"-"+ mcp.digitalRead(relayIndex));
relayIndex++;
relayIndex %= 16;//RELAY_COUNT;
}
void UpdateDisplay4Relay()
{
for (int i=0; i<16; i++)
{
int R = 3;
int X = centerX - (R2+2)15/2 + i(R2+2);
int Y = screenH - R-1 ;
display.setColor(WHITE);
display.drawCircle(X, Y, R);
display.setColor(MCP_digitalRead(i)==0?BLACK:WHITE);
// display.setColor(mcp.digitalRead(i)==0?BLACK:WHITE);
display.fillCircle(X, Y, R-2);
display.setColor(WHITE);

}
display.display();

}

void setup()
{
Serial.begin(115200);
while (!Serial){delay(100);}
// initDisplay();
// connectWIFI();
// InitAndLaunchServer();
InitRelays();
}

unsigned long timerSwitchRelay = 0;
const int DELAY_BETWEEN_RELAY_SWITCH = 100;
void loop()
{
if (millis() > timerSwitchRelay)
{
SwithRelay();
// UpdateDisplay4Relay();
timerSwitchRelay = millis() + DELAY_BETWEEN_RELAY_SWITCH;
}
}

////////////////////////
// MCP23017
////////////////////////
#include

define MCP23017_ADDRESS 0x20

// registers

define MCP23017_IODIRA 0x00

define MCP23017_IPOLA 0x02

define MCP23017_GPINTENA 0x04

define MCP23017_DEFVALA 0x06

define MCP23017_INTCONA 0x08

define MCP23017_IOCONA 0x0A

define MCP23017_GPPUA 0x0C

define MCP23017_INTFA 0x0E

define MCP23017_INTCAPA 0x10

define MCP23017_GPIOA 0x12

define MCP23017_OLATA 0x14

define MCP23017_IODIRB 0x01

define MCP23017_IPOLB 0x03

define MCP23017_GPINTENB 0x05

define MCP23017_DEFVALB 0x07

define MCP23017_INTCONB 0x09

define MCP23017_IOCONB 0x0B

define MCP23017_GPPUB 0x0D

define MCP23017_INTFB 0x0F

define MCP23017_INTCAPB 0x11

define MCP23017_GPIOB 0x13

define MCP23017_OLATB 0x15

define MCP23017_INT_ERR 255

uint8_t i2caddr;

// minihelper to keep Arduino backward compatibility
static inline void wiresend(uint8_t x) {

if ARDUINO >= 100

Wire.write((uint8_t) x);

else

Wire.send(x);

endif

}

static inline uint8_t wirerecv(void) {

if ARDUINO >= 100

return Wire.read();

else

return Wire.receive();

endif

}

/**

  • Bit number associated to a give Pin
    */
    uint8_t bitForPin(uint8_t pin){
    return pin%8;
    }

/**

  • Register address, port dependent, for a given PIN
    */
    uint8_t regForPin(uint8_t pin, uint8_t portAaddr, uint8_t portBaddr){
    return(pin<8) ?portAaddr:portBaddr;
    }

/**

  • Reads a given register
    */
    uint8_t readRegister(uint8_t addr){
    // read the current GPINTEN
    Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
    wiresend(addr);
    Wire.endTransmission();
    Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
    return wirerecv();
    }

/**

  • Writes a given register
    */
    void writeRegister(uint8_t regAddr, uint8_t regValue){
    // Write the register
    Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
    wiresend(regAddr);
    wiresend(regValue);
    Wire.endTransmission();
    }

/**

  • Helper to update a single bit of an A/B register.

    • Reads the current register value


    • Writes the new register value

      */

      void updateRegisterBit(uint8_t pin, uint8_t pValue, uint8_t portAaddr, uint8_t portBaddr) {

      uint8_t regValue;

      uint8_t regAddr=regForPin(pin,portAaddr,portBaddr);

      uint8_t bit=bitForPin(pin);

      regValue = readRegister(regAddr);

// set the value for the particular bit
bitWrite(regValue,bit,pValue);

writeRegister(regAddr,regValue);

}

/**

  • Initializes the MCP23017 given its HW selected address, see datasheet for Address selection.
    */
    void MCP_begin(uint8_t addr, int sda, int scl) {
    if (addr > 7) {
    addr = 7;
    }
    i2caddr = addr;
Wire.begin(sda, scl);

// set defaults!
// all inputs on port A and B
writeRegister(MCP23017_IODIRA,0xff);
writeRegister(MCP23017_IODIRB,0xff);

}

/**

  • Sets the pin mode to either INPUT or OUTPUT
    */
    void MCP_pinMode(uint8_t p, uint8_t d) {
    updateRegisterBit(p,(d==INPUT),MCP23017_IODIRA,MCP23017_IODIRB);
    }

void MCP_digitalWrite(uint8_t pin, uint8_t d) {
uint8_t gpio;
uint8_t bit=bitForPin(pin);

// read the current GPIO output latches
uint8_t regAddr=regForPin(pin,MCP23017_OLATA,MCP23017_OLATB);
gpio = readRegister(regAddr);

// set the pin and direction
bitWrite(gpio,bit,d);

// write the new GPIO
regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB);
writeRegister(regAddr,gpio);

}

void MCP_pullUp(uint8_t p, uint8_t d) {
updateRegisterBit(p,d,MCP23017_GPPUA,MCP23017_GPPUB);
}

uint8_t MCP_digitalRead(uint8_t pin) {
uint8_t bit=bitForPin(pin);
uint8_t regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB);
return (readRegister(regAddr) >> bit) & 0x1;
}`

Most helpful comment

It seems that COM port is opened in another monitor. Try to reconnect board.

All 2 comments

It seems that COM port is opened in another monitor. Try to reconnect board.

may be the alignment error or indentation error in the script... double check those lines ...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

baget picture baget  ·  8Comments

DJManas picture DJManas  ·  9Comments

PabloAbraham picture PabloAbraham  ·  5Comments

GeorgeFlorian picture GeorgeFlorian  ·  3Comments

chenjingyuanku picture chenjingyuanku  ·  8Comments