From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Changing the port into a thread

Hi.

In my project, before RunInterface function I start a thread for the comunication port.

In this thread first of all I open the comunication port and set the settings for it.
In the main program I need to change the port at one moment. Now I ask how can do that?

I will explain what I tryed to do.
In the main program I closed the port and than I use the same function to start a new
threadpool.

This mettod works but for a limited number of times. After a few number of changing the
port the call of the thread function stops to work.
I think that I need to stop the first thead before starting another one.

I tryed to change the port in the main program :

rs232Status2 = CloseCom (comport);
GetCtrlIndex (GetPort_handle, Config_ComPort, &portindex);
GetValueFromIndex (GetPort_handle, Config_ComPort, portindex, &comport);
cmtStatus = CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE,ThreadFunction,
NULL,&threadID);

This is the thread rutine:

int CVICALLBACK ThreadFunction (void *functionData)
{
int status;
SetBreakOnLibraryErrors (0);
OpenComConfig (comport, "", 19200, 0, 8, 1, 5000, 512);
status = GetComStat (comport);
if(status>=0)
{
/* Turn off Hardware handshaking (loopback test will not function with

it on) */
SetCTSMode (comport, LWRS_HWHANDSHAKE_OFF);
ComSetEscape (comport, 4);
/* Make sure Serial buffers are empty */
FlushInQ (comport);
FlushOutQ (comport);
EventMask=LWRS_RXFLAG;
rs232Status2 = InstallComCallback (comport, EventMask, 0, 0xff,

SerialaCallback, 0);
}
else
{
SetBreakOnLibraryErrors (1);
MessagePopup("Error","This port do not exist.");
return 0;
}
SetBreakOnLibraryErrors (1);

/* Start a loop that will process events for this thread */
while (quitflag == 0)
{
ProcessSystemEvents ();
}
/* Close the open COM port before exiting */
rs232Status2 = CloseCom (comport);
return 0;
}


Thanks.
0 Kudos
Message 1 of 2
(2,457 Views)
You could try using PostDeferredCallToThread function to launch a callback in the thread that uses the com port. That function could take care of closing the current port and reopening another.

This is only a suggestion: I never have tried such an environment so I cannot guarantee that it works well for your needs, but I think it worth a try.


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 2
(2,447 Views)