hi, i'm writing serial port listener(monitor) to my ARM microcontroler , using VISA to listen to com port and print data in the textbox. but in the same time i want to use another GUI element and another functions.
here some code explanation:
here i'm open thread for task:
int CVICALLBACK SW_ON_OFF (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { char *value; int val; char params[32]; switch (event) { case EVENT_COMMIT: GetCtrlVal (panel, PANEL_BINARYSWITCH, &val); if(val == 1) { CmtScheduleThreadPoolFunction(DEFAULT_THREAD_POOL_HANDLE, ThreadFunction, NULL, &mainThreadID); CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, mainThreadID, OPT_TP_PROCESS_EVENTS_WHILE_WAITING); } else { CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, mainThreadID); ResetTextBox (panelHandle, PANEL_TEXTBOX, ""); ResetTextBox (panelHandle, PANEL_TEXTBOX_STATUS,""); executeCOMCommand("CLOSE", 1); } break; } return 0; }
double executeCOMCommand(char *commandString, int deviceNumber) { ViSession defaultRM; ViReal64 measurement = 0; ViUInt32 num_bytes; ViUInt32 RTCount; ViBoolean packetOK; int result, pp = 0; char dirName[4096], pathName[4096]; int i, currentIndex, verbal = 0; //verbal - the returned measurement is in words int *deviceNumberTosend = malloc(sizeof(int)); char adressString[1024], Databuf[1024], meas[1024], debugMSG[1024]; char dataToSend[4], err[4096], *packetdata; int status; char *command = strtok(commandString, " "); if(deviceNumber<0) { MessagePopup("Internal Error", "Window"); return -1; } //Run coresponding thread *deviceNumberTosend = deviceNumber; //init anyway sprintf(adressString, "%s", UUTAddr); viOpenDefaultRM (&defaultRM); result = viOpen(defaultRM,(ViRsrc)adressString, VI_NULL ,VI_NULL, &ssp); if(result!=0) return -1; //init of ssp protocol viSetAttribute (ssp, VI_ATTR_TERMCHAR_EN, VI_TRUE); //disable termination char viSetAttribute (ssp, VI_ATTR_TERMCHAR, 0x0A); viSetAttribute (ssp, VI_ATTR_ASRL_END_IN, VI_ASRL_END_LAST_BIT); viSetAttribute (ssp, VI_ATTR_ASRL_BAUD, 38400); viSetAttribute (ssp, VI_ATTR_ASRL_PARITY, VI_ASRL_PAR_NONE); viSetAttribute (ssp, VI_ATTR_TMO_VALUE, 25000); //TMO 15 seconds viFlush (ssp, VI_ASRL_IN_BUF_DISCARD); Sleep(1); if (!strcmp(command,"CLOSE")) //Reading { viFlush (ssp, VI_WRITE_BUF); viFlush (ssp, VI_READ_BUF); viClose(ssp); return 0; } if (!strcmp(command,"LISTEN")) //Reading { status = viScanf(ssp, "%t", &Databuf); InsertTextBoxLine(panelHandle, PANEL_TEXTBOX, debugTextBoxLine++, Databuf); //*************** Auto Scroll *********************** SetCtrlAttribute(panelHandle, PANEL_TEXTBOX, ATTR_FIRST_VISIBLE_LINE, debugTextBoxLine); //**************************************************** return 0; } viFlush (ssp, VI_WRITE_BUF); viFlush (ssp, VI_READ_BUF); viClose(ssp); return 0; }
and thread function is: this function listen to com port and writes data to textbox
int CVICALLBACK ThreadFunction(void *functionData) { while(!done) { executeCOMCommand("LISTEN", 1); } return 0; }
when i press quit button
int CVICALLBACK Quit (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: done = 1; executeCOMCommand("CLOSE", 1); QuitUserInterface (0); break; } return 0; }
the program hangs and give me general protection error , it's still check the THreadFunction and do executeCOMCommand("LISTEN", 1).
do i wrote it correctly for multithreading or i must to use something else. how i can quit program in write way.
thanks