Jump to content

Application for reading multiple serial ports


Recommended Posts

I am building an application that needs to read and store (ascii) data from sensors at six serial ports, every minute. The state machine (inside a while loop) I use is divided in three cases:

initialization: the six serial ports are initialized (serial settings, timeout, end character etc.). A check is performed on the actual time to see whether a new minute has started. This case runs every second (wait time). If a new minute has started, the state machine turns to the read case. Otherwise the initialization is repeated.

read: a poll is carried out for each initialized serial port (VISA write). When a message is received (VISA read), or when the time out has elapsed, it is decoded by a separate subVI for each sensor and the necessary parameters are determined from the message. Furthermore, the received message is concatenated with a date/time stamp and stored in a text file. When no message is received for a certain serial port, only the time stamp is stored for that one. A check is carried out to see whether a new hour has started. If that is the case, the processing case is executed, otherwise the program returns to initialization.

processing: hourly parameters are derived from the 60 one minute values, and stored afterwards. The program now returns to initialization.

The problem is mostly in the second (read) case. I don't actually know what is the best way to poll (and read) multiple serial ports in the same VI. Currently, I have put them all six below each other so the processes are carried out parallel and the timing is depending on the last one to finish. I think this is not ideal and it could be done easier and more efficiently. Moreover, in this way the CPU usage is very high. Every time a new minute triggers the state machine to execute the processes in the read case, it increases to 100% for the complete polling time. However, polling them in a certain sequence is not really an option I think, since I prefer to poll the sensors synchronously, if possible.

Anyone with a good idea or example?

Thanks in advance.

Link to comment

QUOTE (SharkM @ May 26 2008, 05:27 AM)

The problem is mostly in the second (read) case. I don't actually know what is the best way to poll (and read) multiple serial ports in the same VI. Currently, I have put them all six below each other so the processes are carried out parallel and the timing is depending on the last one to finish. I think this is not ideal and it could be done easier and more efficiently.

It'll be a lot easier to help you if you post your code so we can see what you're doing.

PS: HH?

Link to comment

QUOTE (Yen @ May 26 2008, 01:22 PM)

I think you're judging too fast. Even if it was a HH (which I'm guessing it isn't), the question is perfectly legitimate and formed properly.

I'm not judging (that's why it's a PS and there's a "?" after it). I just figured the formatting of the post suggested a HH.

Link to comment

This really isn't a homework assignment.

I was just wondering whether there is any documentation/example available on reading from multiple serial ports in a parallel process (the same while loop), since it seems to make the CPU running at 100%. Of course the processes can be put after one another, but then there will be some synchronization problems. Is there any smart way to do this?

I will post a screenshot when the occasion arises.

Link to comment

Its hard to suggest a strategy without knowing a bit more. A sample or picture of your code would help.

Why are you initializing the interfaces after every read?

Are the serial devices all the same type?

Do the serial devices include some sort of termination character with each message?

Maybe some of these links will help:

The Effect of Asynchronous vs Synchronous VISA Write and Read Functions

Reading a serial port based on events

Link to comment

Thank you for the links. I think the 100% CPU usage is coming from the fact that the VISA read is continuously waiting for all the bytes to arrive. I should omit this.

I already got rid of the initialization vi after every read, and now execute this one before the loop. The serial devices are all different.

Two of the serial devices have indeed an termination character included (and I use it), the other two generate messages of a specific length (3000-4000 bytes, transfer time about 2.5 seconds).

Link to comment

Here are some thoughts.

De-couple the serial ports I/O from the message traffic they carry. For all active serial ports check the number of bytes waiting at each port and read those from the serial buffer. Queue up the read data to seperate queue for each that feed some parsing code. THe Parsing code shoudl check the framing and valiidtiy of the message in the context of any previously read data and then use the results as if it came right from the port.

So let the serial support code just do serial and lett other code handle the data.

If each serial port has to support command response traffic, then six clones of serial support that use a design pattern similar to Jim Kring's Queued Message Handler with Response Notification would get you a long way.

Ben

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.