G
G
Georgy Grigoriev2015-12-29 17:32:37
C++ / C#
Georgy Grigoriev, 2015-12-29 17:32:37

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 "";
        }
    }
}

And actually the question is what. Here my colleague claims that it is common practice to keep the COM port always open, and therefore your finder should be stuffed into the device itself and called AutoConnect. But I argue that this is redundant, and that it is enough to transfer the device to the device after finding the port and speed, and it will already work specifically with one port and speed. The catch is that my colleague found an argument more. If we want to re-initialize the device while the application is running, then there is a risk that DeviceFinder will return NotFound instead of the device, because the port is occupied by another object. A cursory googling on our and not our Internet did not give anything sensible about the correct work with com-ports. I understand that this is certainly the last century, but still I wanted to understand how best to organize the work of the device subsystem in a normal way. AND, Of course, the question that worries me a lot is is it really such a big overhead for opening a port that it must be kept open all the time? In my opinion, this is absolutely illogical, because it creates so many potential problems.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily, 2015-12-29
@IamKarlson

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 question

Ask a Question

731 491 924 answers to any question