LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Continually reading rs-232 port

Hello, Im new to LabWindows and I was trying to continually read data using rs-232. We are trying to do this with an interrupt so I was using InstallComCallback function. I was modifying the serial.cws given in the LabWindows libraries. The problem is that it doesn't seem to be calling the interupt function Event_Char_Detect_Func1. The following attachment is what i have been working on so far.

0 Kudos
Message 1 of 2
(2,706 Views)

As far as I can understand your code needs some cleanup: you are opening the port once in the main function and a second time in the thread function; additionally, at program end you are closing port 2 which you never opened. Finally, you have put some MessagePopup inside the threaded function: I understant it is for debugging purposes but it is bad practice as it will block the loop and you should avoid user interaction in threads that way. It is better to flash a led on the main panel to inform the user of thread activity.

 

Since you want to receive a fixed lenght frame, you probably don't need the com callback: you could set a proper timeout with SetComTime after opening the port and insert a ComRd for 10 characters in the loop in threaded function; you will need to properly handle the timeout on read but this can greatly simplify your code as it will reside entirely in the thread loop, which will result in:

while (active) {
  error = ComRd ();
  if (error == -99) {   // Timeout during read
  }
  else if (error < 0) {  // RS232 error
  }
  // here add the code to handle received message

  // optionally add a small Sleep () or better SyncWait ()
  // to adapt to device activity
}

 

 * Edit * I have realized now that you are new to CVI. If this is the case, I suggest you to start with a simpler structure, which could be to use a timer to handle serial communications: in the timer callback you can test if 10 characters are available at the port with GetInQLen () and read if true.



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,697 Views)