LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Com Port Problem

Hi All

 

In my program i open a com port for use and close it as surppose to and that works great.

The problem comes in when i got the port open and for some reason the pc shutsdown, i then cant open the com port for use.

 

Is there a way i can gain access to that port without restarting?

Is there a way to force close a com port?

Is there a way to restart a com port?

Help share your knowlegde
Message 1 of 9
(4,857 Views)

Shako, what do intend with "the PC shutsdown"? If it has turned off, at restart the port should not be open and your program should restart normally. At least this is my experience.

Are you using some special PC unit? Is this backed up with a UPS so that it does not shuts down but it goes to sleep instead?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 9
(4,853 Views)

Im using a standaard pc.

Whats seems to be happening is that windows still sees the comm port as been used and my program cannot open it.

To keep the question simple.

Is there a way i force close a comport (Release all handles for that com port)?

Help share your knowlegde
0 Kudos
Message 3 of 9
(4,847 Views)

OpenComConfig fails if the port is already open by some other process.  Com ports don't have sharing semantics in Windows as far as I know.

 

The first question is what other process wants to use the same port?  Configure the processes so they don't contend for the same port.

 

The locked port will release (it's a resource to the process) if the owning process terminates for any reason, even if it doesn't release the port.  Windows will clean up resources that were held by a terminated process.

 

So you can release the port by terminating the process holding it, but the situaution shouldn't be happening in the first place.

 

I get caught by this sometimes when I have the port open using a terminal emulator (e.g. hyperterminal or terminal) and try to run a CVI app that then wants the port.  It can be confusing.

0 Kudos
Message 4 of 9
(4,820 Views)

Hi!

I am blocked on a similar situation and I did not find an answer to my problem so I am bringing this thread back.

 

I get the following error:

 

NON-FATAL RUN-TIME ERROR:   "serial.c", line 60, col 8, thread id 0x00001514:   Function OpenComConfig: (return value == -7 [0xfffffff9]). Cannot open port

 

 

from this line:

 

status = OpenComConfig(port, device_name, baud_rate, 0, 8, 1, 512, 512)

 

 

That comes right after this line:

 

status = CloseCom(port);

 

From theses definitions:

CloseCom

OpenComConfig

 

I am not using any other programs when I run my code right after a PC restart.

port = 7

device_name = "COM7"

baud_rate = 115200

 

I tryed monitoring the port while running the program and the error occurs, but nothing show up on Serial Port Monitor.

 

How can I make sure this command is actualy trying to do what's it's entended? Or how can I follow step by step the port opening?

 

What else could block a port from opening?

 

Edit:

I turned off windows firewall, because I didnt know what to try at this point.

It changed the error to something more criptic:

NON-FATAL RUN-TIME ERROR:   "serial.c", line 127, col 22, thread id 0x00000630:   Function GetOutQLen: (return value == -1 [0xffffffff]). The operation completed successfully.  

That happens at this line:

 

while((pending = GetOutQLen(port)) > 0); 

From the error code I can see that this makes even less sence and I am truly lost here. Please help!

 

 

 

0 Kudos
Message 5 of 9
(4,357 Views)


What else could block a port from opening?


 


Because the CloseCom operation started just before isn't finished yet ?   Can you still reproduce that problem if you add some delay between close and open ? Either by adding a Delay() Statement. Or set a breakpoint before the open.

0 Kudos
Message 6 of 9
(4,343 Views)

Additionally, I would not pass an empty string as the device name, thus letting the system handle it internally. Assigning the port number is enough to open it.

Finally, I would pass -1 in OutputQueueSize parameter: this prevents the system from using an internal buffer, reducing the latency of the serial subsystem. This can also prevent the system from delays in closing the port. Are yuu handling CloseCom return value? Does it return any error?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 7 of 9
(4,334 Views)

Yes it works, I used to have to restart the PC or logout to reset the port association, but giving it a delay of 0.125 seconds is enought to give it time to properly close the port before trying to open it.

 

Working code for anybody who needs it:

 

int serial_open(int port, int baud_rate)
{
    char device_name[16];
    int status;

    // Device name
    sprintf(device_name, "COM%d", port);

    status = CloseCom(port);

    // Added a delay between closing and opening the same port not to have the error 7 "Cannot open port"
    Delay(0.125);
    
    // Open
    status = OpenComConfig(port, device_name, baud_rate, 0, 8, 1, 512, 512);
             
    if(status < 0){
        status = CloseCom(port);
        return -1;
    }

    FlushInQ(port);
    FlushOutQ(port);
    return 0;
}

 

Thanks a lot!

0 Kudos
Message 8 of 9
(4,316 Views)

use SetBreakOnLibraryErrors just before the 1st close will also stop non-fatal run time problems, 

0 Kudos
Message 9 of 9
(4,229 Views)