Answer the question
In order to leave comments, you need to log in
Do I need to keep the com port open all the time?
A colleague and I got into a heated argument. We are sawing a user-resistant system, where there is a bunch of devices working on com-ports. User-resistant, I mean that they can easily specify another instead of one port, well, we need to find these pieces of iron. Accordingly, I made DeviceSeeker, which accepts some kind of test method (something like send a bunch of bytes at the output, wait for another one), with which if you go through all the ports / speeds, you can find the desired device. To implement the port poller, I took a GenericComDevice, like this:
public class GenericComDevice{
public string Port {get;set;}
public string BaudRate {get;set;}
private MySerialPort comPort = new MySerialPort();
public GenericComDevice(string port, string baudRate){
this.Port = port;
this.BaudRate=baudRate;
}
public string SendCommand(string command){
try{
if(!comPort.IsOpen){
comPort.Open();
}
string answer = comPort.WriteDataWithAnswer(command);
comPort.Close();
return answer;
}
catch{
return "";
}
}
}
Answer the question
In order to leave comments, you need to log in
Borrowing a resource unnecessarily is bad form.
Opening a port in terms of time and resources is comparable to opening a small file, if you do this 1000 times per second, then it's an overhead, if once a minute, then I think it's nothing serious.
Decide for yourself.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question