openframeworks / openframeworks/openFrameworks
ofSerial - Windows does not detect disconnections or errors in connection.
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 10.4k
- Forks
- 2.6k
- Avg merge
- 1d 21h
- Merged PRs (30d)
- 9
Description
If you plug in an Arduino, initialize the serial port, and then go and manually disconnect the Arduino's USB cable, then ofSerial will not detect an error in the connection if you try to read bytes (readByte or readBytes).
This is a known issue: http://stackoverflow.com/questions/20713207/identify-disconnect-event-with-a-windows-usb-virtual-com-port
So, since I'm working on a project in which disconnections are frequent, I coded a little function to know if the connection is healthy, it is only coded for windows, but will do a OSX Linux version now and will do a pull request.
This function needs for baud to be stored as an attribute, so there will be few changes in some other functions and in the header file.
bool ofSerial::getConnectionStatus(){
if(bInited){
COMMCONFIG cfg;
DWORD cfgSize;
char buf[80];
cfgSize=sizeof(cfg);
GetCommConfig(hComm,&cfg,&cfgSize);
int bps = this->baud;
sprintf(buf,"baud=%d parity=N data=8 stop=1",bps);
#if (_MSC_VER) // microsoft visual studio
// msvc doesn't like BuildCommDCB,
//so we need to use this version: BuildCommDCBA
if(!BuildCommDCBA(buf,&cfg.dcb)){
ofLogError("ofSerial") << "setup(): unable to build comm dcb, (" << buf << ")";
}
#else
if(!BuildCommDCB(buf,&cfg.dcb)){
ofLogError("ofSerial") << "setup(): unable to build comm dcb, (" << buf << ")";
}
#endif
if(!GetCommState(hComm, &cfg.dcb))
return false;
if(!SetCommState(hComm, &cfg.dcb))
return false;
//if execution passes both conditions, then the COM port is healthy
return true;
}
else{
// not initialized so we return false
return false;
}
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at ofSerial and its header, then trace the readByte and readBytes paths involved after a device is disconnected. Review how the baud value is stored and use the proposed getConnectionStatus behavior as the reference; done means a disconnected Windows serial device is reported as unhealthy without breaking normal reads.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100