Avrdude: ser_open(): can't set com-state for "\\.\COM3"

int soilMoistureValue = 0;

int percentage = 0;

void setup()

{

pinMode(3,OUTPUT);

Serial.begin(9600);

}

void loop()

{

soilMoistureValue=analogRead(A0);

Serial.println(percentage);

percentage = map(soilMoistureValue,490,1023,100,0);

if (percentage<10)

{

Serial.println("pump on");

digitalWrite(3,LOW);

}

if (percentage>80)

{

Serial.println("pump off");

digitalWrite(3,HIGH);

}

}
This is my code

I'm getting error i tried rebooting restarting, checked pins 1 and 2

Hi @kevin1984,

please place your code inside the <code> tags
image
Then your sketch will look like this:

int soilMoistureValue = 0;
int percentage = 0;
void setup()
{
  pinMode(3, OUTPUT);
  Serial.begin(9600);
}

void loop()
{
  soilMoistureValue = analogRead(A0);
  Serial.println(percentage);
  percentage = map(soilMoistureValue, 490, 1023, 100, 0);
  if (percentage < 10)
  {
    Serial.println("pump on");
    digitalWrite(3, LOW);
  }
  if (percentage > 80)
  {
    Serial.println("pump off");
    digitalWrite(3, HIGH);
  }
}

That's much easier to read due to color coding and will also provide scrollbars for longer sketches.

To allow other members in the forum to support you please also provide further information on your application:

  • Development platform (Arduino IDE? Version? Windows or Linux?)
  • Type of board/processor
  • Further components
  • Drawing of your wiring

That all helps to find an answer!

Hi @kevin1984.

There were some previous reports of this "can't set com-state for ..." error occurring specifically when uploading to the boards that use the WCH CH340 USB chip (and apparently only specific batches of those chips). The problem only occurs when using the latest version of the CH340 driver.

The affected users have reported that doing a "roll back" to an older version of the driver was an effective workaround. You can give that a try.

I'll provide instructions:

  1. Open this tutorial from SparkFun in your browser:
    https://learn.sparkfun.com/tutorials/how-to-install-ch340-drivers/all#drivers-if-you-need-them
    We are using this tutorial as a source for an older version of the CH340 drivers. Generally I recommend only obtaining drivers from the manufacturer of the device, but WCH does not provide older versions of their drivers and SparkFun is trustworthy.
  2. Click the "Windows (EXE)" link you see in red under the "Drivers (If You Need Them)" section of the tutorial.
  3. Wait for the download to finish.
  4. Run the downloaded CH341SER.EXE file.
  5. A "User Account Control" dialog may now appear asking "Do you want to allow this app to make changes to your device?". Click the "Yes" button.
  6. A "DriverSetup(X64)" dialog will appear. Click the "INSTALL" button in the dialog.
  7. Wait for the driver installation to finish, as indicated by the appearance of a "Driver install success!" dialog.
  8. Click the "OK" button in the "Driver install success!" dialog.
  9. Click the X icon at the top right of the "DriverSetup(X64)" dialog to close it.
  10. Close Arduino IDE if it is running.
  11. Connect the Arduino board to your computer with a USB cable.
  12. Open the Windows Device Manager.
  13. Select View > Devices by type from the Device Manager menus.
  14. Open the "View" menu.
  15. If there is a to the left of the "Show hidden devices" menu item, click on "Show hidden devices" to disable it.
  16. Open the "Ports (COM & LPT)" section of the Device Manager tree.
  17. You should see a port identified as "USB-SERIAL CH340 (COMn)" under the "Ports (COM & LPT)" section (where "COMn" is some serial port e.g., COM4). Right click on that item.
    The "USB-SERIAL CH340 (COMn) Properties" dialog will open.
  18. Select the "Drivers" tab of the "USB-SERIAL CH340 (COMn) Properties" dialog.
  19. You will see a "Roll Back Driver" button in the dialog. If it is not clickable, perform the following instructions:
    1. Click the "Update driver" button.
      An "Update Drivers Device - USB-SERIAL CH340 (COMn)" dialog will open.
    2. Click on "Search automatically for drivers" in the "Update Drivers Device - USB-SERIAL CH340 (COMn)" dialog.
    3. You should now see the driver installation wizard update the driver. Wait for the update to finish, as indicated by the message "Windows has successfully updated your drivers" in the dialog.
      :exclamation: It is possible you will instead see the message "The best drivers are already installed". If so, please stop following the instructions here and reply on the forum thread to let me know. I'll provide alternative instructions you can follow.
    4. Click the "Close" button in the "Update Drivers Device - USB-SERIAL CH340 (COMn)" dialog.
  20. Click the "Roll Back Driver" button in the "USB-SERIAL CH340 (COMn) Properties" dialog.
    The "Driver Package rollback" dialog will open.
  21. Click the radio button next to "My apps don't work with this driver" in the "Driver Package rollback" dialog .
  22. Click the "Yes" button.
  23. Click the "Close" button in the "USB-SERIAL CH340 (COMn) Properties" dialog.

Now start Arduino IDE again and repeat whatever you were doing before when you encountered that error before. Hopefully this time you will not encounter that "can't set com-state" error during the sketch upload.

It helped a lot but I got other error

I'm glad the error is fixed. It is progress at least!

For the benefit of the other forum helpers, I'll add a link to the thread where the "other error" is being supported:

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x03

I got this error after solving last one can you tell how can I supposed to solve this error

@kevin1984 please only add replies to this topic that are directly relevant to the "can't set com-state for ..." error you encountered.

For other errors, please use your other topic:

Fragmenting the discussion across multiple topics will be quite counterproductive. Since you seem to be in a hurry to solve the problem, you should be extra careful to be very efficient in your communications with the helpers here.

Ok you mean After I made a topic if I have another error I should reply to My topic so it will be easy for helpers to answer to me

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.