Serial port changing

Hi

i'm running a sketch on my arduino uno, connected to an app on my pc (windows 7, 64bit).
it uses serial communication to connect, and it works fine for about one hour (it's very inaccurate, sometimes 20 minutes, sometimes 5 hours) and then it suddenly seems to 'switch' COM port.

When I connect my arduino, it says COM3, wich is my arduino. After a while it suddenly seems to connect to a new COM port, COM4. This means the old one is still active (COM3, since the number increases), but is not recognized anymore.

i have no idea on how to solve the problem, since i don't do anything special but Serial.read() and Serial.println()

here's the arduino code:

#include <PinChangeInt.h>
#include "profiles.h"

#define FBLED      11
#define FBBTN      10

#define TWTLED     9
#define TWTBTN     8

#define MAILLED    6
#define MAILBTN    7

#define RSSLED     3
#define RSSBTN     2

#define ONLED      5

#define dataStart   1  //start of heading
#define dataEnd     4  //end of transmission

#define notifData  17  //device control 1
#define msgData    18  //device control 2
#define frndData   19  //device control 3
#define delData    20  //clear all facebook data

Profile fb(FBLED, FBBTN);
Profile twt(TWTLED, TWTBTN);
Profile mail(MAILLED, MAILBTN);
Profile rss(RSSLED, RSSBTN);

bool done = true;

unsigned long pingTmr = millis();

void setup()
{
  Serial.begin(9600);
  /*
  fb.msgs = 1;
  twt.ntfs = 9;
  mail.mail = 3;
  rss.feed = 11;
  */
  PCintPort::attachInterrupt(FBBTN, sendFacebookRequest,RISING);
  PCintPort::attachInterrupt(TWTBTN, sendTwitterRequest,RISING);
  PCintPort::attachInterrupt(MAILBTN, sendMailRequest,RISING);
  PCintPort::attachInterrupt(RSSBTN, sendRssRequest,RISING);
  
  analogWrite(ONLED, 150);
}

void loop()
{
  while(Serial.available()>1)
  {
    int tmpByte = Serial.read();
    
    if(tmpByte == notifData)
      fb.ntfs = Serial.read();
    else if(tmpByte == msgData)
      fb.msgs = Serial.read();
    else if(tmpByte == frndData)
      fb.frnd = Serial.read();
    else if(tmpByte == delData)
    {
      fb.setNull();
      analogWrite(fb.ledPin, 0);
    }
  }
  
  updateAll();
  
}

void updateAll()
{
  fb.update();
  twt.update();
  mail.update();
  rss.update();
}

void sendFacebookRequest()
{
  Serial.println("facebookHTTP");
  fb.setNull();
  analogWrite(fb.ledPin, 0);
}

void sendTwitterRequest()
{
  Serial.println("twitterHTTP");
  twt.setNull();
  analogWrite(twt.ledPin, 0);
}

void sendMailRequest()
{
  Serial.println("mailHTTP");
  mail.setNull();
  analogWrite(mail.ledPin, 0);
}

void sendRssRequest()
{
  Serial.println("rssHTTP");
  rss.setNull();
  analogWrite(rss.ledPin, 0);
}
#include "Arduino.h"

#ifndef profiles_h
#define profiles_h

struct Profile
{
  volatile uint16_t msgs;
  volatile uint16_t ntfs;
  volatile uint16_t frnd;
  volatile uint16_t mail;
  volatile uint16_t feed;
  
  volatile uint16_t total;
  volatile uint16_t alerted;
  
  volatile uint8_t ledState;
  uint8_t ledPin;
  uint8_t btnPin;
  
  unsigned long lastMillis;
  
  Profile(uint8_t, uint8_t);
  void update();
  void setNull();
};

void sendFacebookRequest();
void sendTwitterRequest();
void sendMailRequest();
void sendRssRequest();

#endif
#include "profiles.h"

#define ONLED 5

void sendFacebookRequest();
void sendTwitterRequest();
void sendMailRequest();
void sendRssRequest();

Profile::Profile(uint8_t _led, uint8_t _btn)
{
  msgs = 0;
  ntfs = 0;
  frnd = 0;
  mail = 0;
  feed = 0;
  
  total = 0;
  alerted = 0;
  
  lastMillis = millis();
  
  ledState = 0;
  
  ledPin = _led;
  btnPin = _btn;
  
  pinMode(ledPin, OUTPUT);
  
  pinMode(btnPin, INPUT);
}

void Profile::update()
{
  
  total = msgs + ntfs + frnd + mail + feed;
  
  //total = (total>=5) ? 5 : total;
  
  if(millis() - lastMillis > 500)
  {
    if(alerted < total)
    {
      if(ledState == 255)
        ledState = 0;
      else
      {
        ledState = 255;
        alerted++;
      }
      analogWrite(ledPin, ledState);
    }
    else if(total > 0)
      analogWrite(ledPin, 255);
    
    lastMillis = millis();
  }
}

void Profile::setNull()
{
  msgs = 0;
  ntfs = 0;
  frnd = 0;
  mail = 0;
  feed = 0;
  
  total = 0;
  alerted = 0;
  
  ledState = 0;
}

the PC code I use to write to the arduino is this:

Dim tmpByteList As Byte() = {17, notifs, 18, Asc(msgsStr) - 48, 19, friends}
notifCom.Write(tmpByteList, 0, 6)

with notifCom being the serial port (arduino)
I don't have any other commucation but this, which happens once a minute.

thanks for reading this, and hopefully it can get solved.
Nick

After a while it suddenly seems to connect to a new COM port, COM4.

How do you detect that this (impossible) feat has happened? There is no way for the Arduino to change ports on the PC. The PC must be doing something that makes you think that the Arduino has moved.

The pc app throws an error that COM3 does not exist, then I check the ports and COM3 really dissapeared, but COM4 suddenly appears to be my arduino

UPDATE!!

I removed the #include <PinChangeInt.h> and all references to the lib ( the attachinterrupt() ) and now it's running smooth for about 7 hours, without port change.

Could it be that this lib somehow messes with ports 0 and 1, so the serial communication gets interrupted or something? i took a look at the lib, but i didn't really understand a lot of it.
i'll see if that lib really is the reason to the breakdown, by adding it tomorrow and see if it fails again. if so, is there another way to check the rising of any port by some kind of interrupt, or will a digitalRead() be needed?

Steen:
UPDATE!!

I removed the #include <PinChangeInt.h> and all references to the lib ( the attachinterrupt() ) and now it's running smooth for about 7 hours, without port change.

Could it be that this lib somehow messes with ports 0 and 1, so the serial communication gets interrupted or something? i took a look at the lib, but i didn't really understand a lot of it.
i'll see if that lib really is the reason to the breakdown, by adding it tomorrow and see if it fails again. if so, is there another way to check the rising of any port by some kind of interrupt, or will a digitalRead() be needed?

If this is happening in Windows, something very weird is happening, even on physical reconnections windows will try to use the same com port number

well, it selects the number based on the already used COM's, so if 3 COM's are used, the next one will be COM4.. First it connects to COM3, but after a while to COM4, that means the arduino connection is somehow still present/active in the system, but fails to be recognized, so it assigns the next available port number to it, which is COM4. The strange thing is that COM3 doesn't show up anywhere as connected COM port, it just disappears.

Ok, wth is going on here?? now i got it connected to my pc, and this is the result (image 1).

the error i get from the app is the second image.
I really don't understand it anymore. in the hardware manager it says it is on COM3, but the IDE and the app are saying COM3 does not exist, this is a strange thing going on imo.

In fact i don't really know if this still has something to do with my arduino, since it shows up in the hardware manager as it should...

edit: a little more info on the app
It's written in VB2010, and uses standard serial communication. i added a serial port, of which the communication works fine.
I'm running the app in debug mode, so i haven't compiled it for real yet. (maybe this has something to do with it, i really don't know anymore).

problem_1.JPG

problem_2.jpg

I get this problem, often. There does not seem to be a permanent relationship between the USB slots on the computer and the COM port numbers.

When I get this problem, I unplug the arduino usb cable at the computer end, plug it back in again, and use control panel/device manager to see what COM number it is. I often have to restart the arduino IDE to get the IDE to recognise the arduino again.

unfortunatly same issue and same solution as michinyon!

To see more clearly which arduino is on which port (multiple arduinos in I2C network), I change the port com number like this: