Serial Failure after begining for several minutes

Enviroment: I'm doing a project of kinect controlling.
A C#-programme writing by VisualStudio sends data through COM to mega 2560.And 2560 reads the data to control 16 I/OPorts.

Howerer, everytime when i luanch the C# programme in windows 7 32bit, the COM will always shutdown after sending data several times. But then I can still see the COM exsit in Windows Device manager. But cannot connect the COM with whatever C#programme or arduino IDE. Sometimes it needs to reset the usb line and it can luanch another minutes but somtimes it needs to restart the computer.

I used Port 2 ~ Port17 and with no AC-DC adapter. and ferer than 3 HIGH output at the sametime.

Im seriously fall in trouble. THX

Giraffe Cai.

Did you connect the reset line?

I think everybody's crystal balls are offline today so they can't see your code or setup!

If you want help then you are going to have to provide more info. All you've just said really is:-

'I have a Megas2560 using 16 control lines to interface with a kinect and my serial link keeps failing'

How is it wired? What data are you sending? Post your sketch etc.

Ahaaaa....Mmmmm, :~
Computer sends strings of 2Byte everytime?It is ’0 or 1‘+’one of a~z‘.

‘char cond;
char now;

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(14, OUTPUT);
pinMode(15, OUTPUT);
pinMode(16, OUTPUT);
pinMode(17, OUTPUT);
pinMode(18, OUTPUT);
pinMode(19, OUTPUT);
pinMode(20, OUTPUT);
pinMode(21, OUTPUT);
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
pinMode(25, OUTPUT);
pinMode(26, OUTPUT);
pinMode(27, OUTPUT);

Serial.begin(9600);
}

void loop() {
}

void serialEvent()
{
while (Serial.available())
{
now=(char)Serial.read();
if (now==49)cond=1;
else if (now==48) cond=0;
else if (cond==1){
digitalWrite((now-95),HIGH);
}
else if (cond==0){
digitalWrite((now-95),LOW);
}
}
}'
It's absolutely a simple sketch...

And i just used Port 2~27 and 2 GNDs.

Please use CODE tags when posting code. You can add them by editing your post, selecting the code and clicking the # button in the editor window.

I recommend using character literals such as '0', '1' rather than the corresponding ascii codes, but apart from that the logic in your sketch look as if it should work. You assume that the only characters you receive are the ones you expect, so this could be fooled by incoming line feed characters, or any other unexpected input. I suggest it would be better to use a switch/case statement to deal explicitly with the characters you expect, and ignore any others:

switch(now)
{
	case '0':
		cond = 0;
		break;
	case '1':
		cond = 1;
		break;
	case 'a' ... 'z':
		digitalWrite((2 + now - 'a'),cond);
		break;		
}

Do you have anything connected to the I/O pins? If so I recommend that you disconnect everything and see if that affects the stability. Is it being powered by the USB connection? If not, what is the power supply? If there was a poor connection on the USB that could cause the device to be re-enumerated. Have you tried using the Arduino serial monitor instead of the C# application?

Let me see if I have this right. Reformatted for readability.

    now=(char)Serial.read();
    if (now==49) {
       cond=1;
   }
    else if (now==48) {
       cond=0;
   }
    else if (cond==1) {
       digitalWrite((now-95),HIGH);
  }
    else if (cond==0) {
      digitalWrite((now-95),LOW);
  }
  }
}

To take just one path through that...
If you receive 48 ('1'), you want to set pin -46 HIGH. Does that sound right?