LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

COM Port Read and Write

Solved!
Go to solution

 Attached VI, in a While loop running 4 sub VIs... Data read from COM Port; Analysis of the Data, Preparing Response Packet, And Sending Response to the COM Port... It is not responding (I think it is not reading some data at regular intervals) to some commands some times... Can Anyone suggest me alternate ways to program this, I thought about using Queue and using 2 while loops(Data reading and parsing in one loops, analysis and Com Port write in the 2nd loop).. But My class Data carries the Com Port number; Don't know how I share this between 2 parallel loops for Queue.

 

Regards,

Chandra

 

 

0 Kudos
Message 1 of 13
(4,050 Views)

Can you provide the missing class and VIs?  Its difficult to see what is going on without them.

0 Kudos
Message 2 of 13
(4,035 Views)

Here. I could not copy the whole class; Inside the loop it is getting COM port number from the class data...

0 Kudos
Message 3 of 13
(4,028 Views)

You need to attach the missing VI's if you need more specific assistance but here are some generic Pointer:

 

1. First of all you need to add a delay to all while loops. If not you will drive up CPU usage which will suffocate tasks done by labview under the hood.

2. Use a functional global to do all the interactions with the serial Port, this way you don't need to worry about passing the Port number to different locations.

3. Since it seems that you need to wait for message from the serial port, modify it and respond then you might want to use two loops. Just in the case that you get messages faster than you can modify and response. If modifications is very simple this might be an over kill. But use two loops and one Queue. One loop only checks the Serial Line and if there is a complete message then it adds the message to the Queue. The second loop checks the Queue and if there is a existing Message it grabs the message modifies it and response back using the above functional global and deletes the message from the Queue.

 

If you provide more information regarding the Serial communication then I can help you put the Serial Driver together. For example is there a termination character. If not are the messages a certain length. Then you can also get fancy and add automatic recovery and error handling if you are using this in a professional environment.

 

0 Kudos
Message 4 of 13
(4,026 Views)

Thanks for your guidance. I modified the VI to have 2 while loops with Queue; It seems to work but not 100%, perhaps I am not putting correct delay value,  How do I decide the Delay value?

 

COM data that comes in is Byte Data

X10 X02 X01 X07 X52 X00 X00 X00 X00 X00 X2d X69 X48 X85 X10 X03

 

 

X10 X02 X02 X08 X67 X00 X00 X00 X00 X02 X00 X01 Xf2 X0a X57 X09 X10 X03

 

X10 X02 X02 X07 X51 X00 X00 X00 X00 X00 X25 X72 X3d Xc8 X10 X03

 

I do Byte Parsing to decode the incoming request and prepare a response Byte Packet before sending it to the COM Port

 

Attached is the updated code; could you please review and advise on how I can further improve this.

Thankyou so much for your help.

 

 

 

0 Kudos
Message 5 of 13
(4,016 Views)

The delays will depend on how frequently you expect to receive a message.

Look at your code you are trying to read from the serial port in two different loops, parallel to one another. This is never good.

You need to give a little more high level details. What triggers these data to be sent. Can new data be sent before you respond to the previous message.

Also are there any Termination Character, it is better if you can terminate a read function by a termination character instead of reading a byte at a time. Also are there any confirmation messages when you write to the port.

 

You have a couple of fundamental issues in your serial communication but I need the above information before I can give you my opinion on what is the best route forward.

0 Kudos
Message 6 of 13
(4,011 Views)

Incoming Data comes Constantly, No Termination Characters;  end of the Request Data has certain bytes (0x10 and 0x03).. Byte Parsing done to remove DLE bytes and also get correct start and end address bytes... The reading part is working fine...

 

One otherside sends the Request it waits for the response for 1min; if it does not receive any response within the 1min, it timesout and sends a new command....

 

How do I make sure Queue does not have any stale data?

0 Kudos
Message 7 of 13
(3,984 Views)

So, we are still missing a lot of the code, but I think I can respond to some of what df86 said. 

 

It is not clear to me why you need a functional global for COM interaction.  The original code goes a step beyond FG and uses objects which done well can be a better way of doing this (I have a Serial Object that is the superclass for a lot of my devices that does this).  It looks to me that it could be better encapsulated but that is another discussion.

 

Regarding the delay - when I don't know the termination characters, I use a wait for bytes function that loops for some predetermined time, checking every x ms for new bytes, such that I don't get deadlock.  If I don't get data back in a reasobable time (as determined by the application) then I error out and handle that as necessary to the application (one way would be to reset the connection). But, here it looks like you do in fact have a termination character (0x03?) - you can use this via the termination char bits:

 

another snippet.png

 

In this case, the termination char will be either x01 or x03 (it is not clear to me which it is from your description below).

 

As df86 said above, you don't want to access the port on different loops - this can cause collisions and create all kinds of problems).  If you do have two loops, you want to process data retrieved from the communication in one loop and do your communication in the other loop.  The queue will have no data if you have not put data in it so stale data is not an issue.  Another option for processing the data is to pipeline it - that is, in the same loop, while you are retrieving data you can process the previously retrieved set of data by passing the previous communication set via shift registers.  However, this may not be necessary as I am guessing that is relatively quick compared to the actual serial communication and the response is dependent on the data received.  But, maybe I am misinterpreting what is happening here.

 

Hope this helps.  Cheers, Matt

0 Kudos
Message 8 of 13
(3,966 Views)
Solution
Accepted by topic author Velpula

My Original Code with Objects is working perfectly with delay in the while loop; Thanks for pointing me in that direction...

 

 

0 Kudos
Message 9 of 13
(3,954 Views)

Glad to hear. there are numerous ways of coding and done well, it becomes a matter of preference which route to take. Just don't forget to add delays to every while loop, even if they are inside other cases. this is very important if you are performing any updates to property nodes or accessing communication ports. In another one of your subVI you had a loop without a delay. Make sure you add a delay in there.

 

Good luck

0 Kudos
Message 10 of 13
(3,949 Views)