# Weird behavior Closing and opening serial Port in Windows. Bug?

**URL:** <https://discourse.processing.org/t/weird-behavior-closing-and-opening-serial-port-in-windows-bug/39559>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 2, 2022, 11:28pm UTC](https://discourse.processing.org/t/weird-behavior-closing-and-opening-serial-port-in-windows-bug/39559 "2022-11-02T23:28:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PAk](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@PAk](https://discourse.processing.org/u/PAk)\
**Post date:** [November 2, 2022, 11:28pm UTC](https://discourse.processing.org/t/weird-behavior-closing-and-opening-serial-port-in-windows-bug/39559/1 "2022-11-02T23:28:21Z")

</div>

Hello I am trying to close and reopening a serial port in Windows ( CP210x) and it is not possible.

This is some pretty simple code:

```auto
  case '9': 
    println("pre-serial: "+serial);
      serial.clear();
      serial.stop();
      //serialPort.closePort();
      delay(1000);
      println("mid-serial: "+serial);
      
      synched=false;
      serial = new Serial(this, serialPort, BaudRate);
      println("post-serial: "+serial);
    break;

```

And this is the output:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/3/335fd12cef5935450a494edeffeb7dc8eab9626f.png)

It will never recover communications again, since after closing the port and reopening the serial.available() is zero.  
On the other hand, I have tested a simple C program and it is working as expected, this issue only happens with Processing.

Is there an error on the Library?  
Thank you

---

<div class="post-metadata">

**Author:** ![PAk](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@PAk](https://discourse.processing.org/u/PAk)\
**Post date:** [November 3, 2022, 1:08am UTC](https://discourse.processing.org/t/weird-behavior-closing-and-opening-serial-port-in-windows-bug/39559/2 "2022-11-03T01:08:41Z")

</div>

It seems the stop is done correctly, because if I try to open then the port with another application it works, but processing is unable to open it again.

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/6/5/65e617183b8998f7727e24f3c2231168f6fb7c42.png)

---

<div class="post-metadata">

**Author:** ![PAk](https://avatars.discourse-cdn.com/v4/letter/p/4bbf92/32.png) [@PAk](https://discourse.processing.org/u/PAk)\
**Post date:** [November 3, 2022, 9:38pm UTC](https://discourse.processing.org/t/weird-behavior-closing-and-opening-serial-port-in-windows-bug/39559/3 "2022-11-03T21:38:49Z")

</div>

Is it a library issue or can we modify something at sketch level?  
Thank you

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [November 5, 2022, 2:32pm UTC](https://discourse.processing.org/t/weird-behavior-closing-and-opening-serial-port-in-windows-bug/39559/4 "2022-11-05T14:32:27Z")

</div>

Hello @PAk ,

Some code I used to explore this:

```auto
// Example by Tom Igoe

import processing.serial.*;
Serial serial; // The serial port
String comPort;
int BAUD = 9600;

boolean synched;

void setup() 
  {
  size(400, 300);
  // List all the available serial ports:
  printArray(Serial.list());
  // Open the port you are using at the rate you want:
  comPort = Serial.list()[4];
  serial = new Serial(this, comPort, BAUD);
  //delay(1000);
  if (serial.active())
    {
    synched = true;
    println(synched);
    }
  
  textAlign(CENTER, CENTER);  
  textSize(48); //Max textSize set
  }

int hrtBeat;

void draw()
  {
  background(255);
  //heartbeat
  
  int timer = frameCount%30;
  
  if (timer == 0)
    {
    //println(hrtBeat);
    hrtBeat++; 
    }
  fill(0);
  text(hrtBeat, 200, 100-20);
    
  if( (timer == 0) && synched )
    {
    for (int i=0; i<16; i++)
      {
      serial.write(i);
      }
    serial.write('\r');
    serial.write('\n');
    
    println(frameCount, "Serial sending...");
    //text("Serial sending...", 200, 200-20); 
    }
     
  }
  
// Stop (s)
void keyPressed()
  {
  if (key == 's')
    {  
    serial.clear();
    serial.stop();
    println(serial.active());
    println("mid-serial: "+serial);       
    synched = false;
    }
  
// Restart (r)  
  if (key == 'r' && !serial.active())
    {
    printArray(Serial.list());
    serial = new Serial(this, comPort, BAUD);
    //delay(500);
    println(serial.active());
    println("post-serial: "+serial); 
    synched = true;
    }    
  }

```

I use W10 and Processing 4.01.

I was able to start stop the serial port with Processing to:

- [Br@y’s Terminal](https://sites.google.com/site/terminalbpp/) through a virtual COM port on PC
- Arduino MEGA 2560 R3

I do not have any devices with a CP210x to test.

`:)`
