开发者

Share SerialPort over different instances

开发者 https://www.devze.com 2023-04-13 10:08 出处:网络
I have many instances off a program that use a SerialPort-Connection. The first instance should open the serialport and the last should close the connection.

I have many instances off a program that use a SerialPort-Connection.

The first instance should open the serialport and the last should close the connection.

The instances between this two should check if the connection is open and use it.

The instances cant communicate between themselves.

My idea was this:

All instances do this at the beginning:

port = n开发者_开发技巧ew SerialPort( portName );
if (!port.IsOpen)
{
    port.Open();
}

The last one check if a property is set and close the port if necessary

if (ClosePort && port.IsOpen)
    port.Close();

But this didn't work (permission denied). I think it is because every instances create a new SerialPort object?!.

But I cant give every instances the SerialPort-object from the first instance. Is there another way to do it?


You should adopt a Client/Server schema, where the server manages the serial port, and clients connect to the server to make its operations.

You can do it with a simple socket.

0

精彩评论

暂无评论...
验证码 换一张
取 消