LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to read continuously from rs232?

Solved!
Go to solution

Hello,

 

I am new to LabVIEW and my questions would be obvious for some for you. So please help if you know the solution.

 

This is for my school project. For your information, we need to program an Altera DE2 board to control the traffic lights (something like that) and at the same time, the board transmits the states of the lights to a computer with LabVIEW installed via RS-232 cable.

 

The rest of the project was going well except for the LabVIEW part because as I said earlier, I am new to LabVIEW.

 

We are using the 'Basic Serial Write and Read.vi' provided in the NI examples folder. The problem is that we don't know how to modify the vi in order to receive continuously the data coming form the DE2 board. Each time, the vi stops to read after it received 24 bytes of data.

 

Thus, the first question is:
1.) How to read continuously from the RS-232 port? (We tried to use 'while loop' but with no success, maybe we were doing it the wrong way.) If you know the solution, please shed some light for us in a detailed manner.

 

2). Secondly, once we received the 'string', how can we convert it into bits (0 and 1) so that we could be able to manipulate the bits, for example to turn on the LEDs which correspond to the bits.

 

Thanks.

0 Kudos
Message 1 of 14
(18,763 Views)

eddiechai2003 wrote:

Thus, the first question is:
1.) How to read continuously from the RS-232 port? (We tried to use 'while loop' but with no success, maybe we were doing it the wrong way.) If you know the solution, please shed some light for us in a detailed manner.


What did you try? Can you upload the code? A while loop is definitely what you should be using.

 


eddiechai2003 wrote:

2). Secondly, once we received the 'string', how can we convert it into bits (0 and 1) so that we could be able to manipulate the bits, for example to turn on the LEDs which correspond to the bits.


That depends on what this "string" is. Can you provide more information as to what it is? Is is a character? A hex value?

 

0 Kudos
Message 2 of 14
(18,755 Views)

Hello smercurio_fc,

 

We are using the example provided by NI, but we are adding a while loop for the read block as in the image.

Red_arrow

This doesn't work at all. But without the loop, the vi is able to read up to 24 bytes of data then it stops there.

 

For the 'string', it is just constituted of 8 bits of 0 and 1 sent continuously from the Altera DE2 board via RS-232.

 

The value of the bits represents the state of the lights we are controlling. For this purpose, we would like to be able to recreate the event on the computer by lighting up the appropriate LEDs to simulate the real situation.

 

I hope this clears thing up.

 

Thanks.

0 Kudos
Message 3 of 14
(18,753 Views)

That would only work IF the device continuously sends out data after receiving the command. Is this how it works? Or does it send out a response only after receiving a command? If this is the case, you would need to have the loop include the VISA Write as well. Also, the "write" and "read"buttons would be superfluous in your case and can be deleted.

 


For the 'string', it is just constituted of 8 bits of 0 and 1 sent continuously from the Altera DE2 board via RS-232.

 

The value of the bits represents the state of the lights we are controlling. For this purpose, we would like to be able to recreate the event on the computer by lighting up the appropriate LEDs to simulate the real situation.

 

I hope this clears thing up.


 

Not really. You still haven't indicated what the string is. Is it a single character? Or is it a sequence of characters made of up the characters "1" and "0"?

0 Kudos
Message 4 of 14
(18,739 Views)

1.) For now, we are only interested in receiving data from the device. Therefore, yes, the device continuously sends out data to its RS-232 port without waiting for any command from LabVIEW. But the block diagram above didn't work as expected. We received nothing at all after adding the while loop. Could you suggest a block diagram for us which works?

 

2.) Each data is made up of 8 bits.

 

To be clear, I give a concrete example. In our case, we have 2 traffic lights to deal with, they are the priority traffic light and the secondary traffic light.

 

For the priority traffic light (respectively secondary traffic light), we have the green, orange and red lights.

 

In total, we have 6 lights to control.  We name each light respectively PG (priority green), PO (priority orange), PR (priority red), SG (secondary green), SO (secondary orange), and SR (secondary red).

 

Besides that, we need 2 sensors. Similarly, we name them S1 and S2.

 

PG, PO, PR, SG, SO, SR, S1 and S2 can only have 2 values, 0 and 1.

 

0 means the light is turn off while 1 is the oppossite.

 

We need to send the state of these lights and sensors to LabVIEW. To do that, we concatenate these bits into a 'string' or a data of 8 bits (I don't know how to name it). For example, in this case, we send the data (S2&S1&PG&PO&PR&SG&SO&SR) sequentially over the RS-232 cable.

 

Since the state of the lights and the sensors changes over time, we need to continuously send out the data to LabVIEW in order to view it on the computer.

 

And again, I hope this time it will be clearer.

 

We want to know: is there a simlpe way for us to manipulate the data receive in LabVIEW as individual bit so that we can reconstruct the state of the lights on the computer? 

 

Thanks.

 

0 Kudos
Message 5 of 14
(18,733 Views)

Unfortunately, you still have not explained exactly what you will be reading over the serial port. You said:

To do that, we concatenate these bits into a 'string' or a data of 8 bits (I don't know how to name it). For example, in this case, we send the data (S2&S1&PG&PO&PR&SG&SO&SR) sequentially over the RS-232 cable.

What does this mean? The serial port reads bytes, not bits.  What do you mean by "concatenate the bits"? Does this mean you read a single byte that is the OR of 8 bits? Does it mean you read 8 bytes, with each byte being the character "1" or the character "0" to indicate on/off? Does it mean that you read 8 bytes with each byte being the values of 0 and 1?

 

It's trivial to manipulate the data in LabVIEW, but you have to know what you're getting in the first place.

0 Kudos
Message 6 of 14
(18,721 Views)

Assuming that your data is one byte comprised of ORing your 8 status bits together, this character may not be printable.  Do you still have the string in normal mode?  If so, you may wish to change it to HEX Display or '\' codes Display (right-click on the string and select the desired choice).  That will guarantee you will see the byte on the string.

 

When you say you only get 24 bytes without the while loop, does this change if you change the delay before read value?  If your device is sending data continuously, if you increase this value, you should see more data coming back.  If you change this delay and the amount of data you are getting back doesn't change, you have some other problems.

 

With your modified code, everything may be working correctly, but you cannot see it.  You have no delay in teh while loop, so it will be running as fast as possible.  It is highly likely that Bytes at Port will return 0 and your string will be cleared.  I would put a case structure around the VISA read so it doesn't do anything if there is no data at the serial port.  That way, you will not clear your string.  Also, move the wait into the while loop, so your while loop will read from the port, then wait a little bit before it does it again.  Right now, your CPU is being starved as the while loop is running as fast as it possibly can.

 

I think your code is probably working (assuming your device is sending data as you describe), but you are not seeing it.

Message 7 of 14
(18,717 Views)

Hello,

 

Sorry smercurio_fc, please excuse me for my bad explanation. The device is sending 1 byte of data (8 bits for the status of the lights and sensors) each time. And the device is sending this 1 byte of data constantly at a fixed interval. Therefore, LabVIEW should receive 1 byte of data continuously which contains the status of all the lights and sensors.

 

Matthew Kelton, I think you are getting the point. We use a 500 ms delay before read. After a short calculation, I found out that in 500 ms, LabVIEW should be able to receive around 30 bytes of data. And 24 bytes are very close to 30 bytes! So it seems logical that we should include the delay into the while loop. However, at this moment, I can't test the code since I don't have the device with me at home. We will continue working on this project maybe next Monday or Tuesday in school's lab. I hope you are right so that we can progress more on the project.

 

Meanwhile, once we received the 1 byte of data, what would be the most effective way to 'separate' the byte received into bits and 'turn' on the corresponding LEDs in LabVIEW which represent  the lights and the sensors?

 

Thanks.

P.S: If you need more clarification, just ask it. I know I am very bad at explaining things. Please forgive me.

0 Kudos
Message 8 of 14
(18,712 Views)
Solution
Accepted by topic author eddiechai2003

Instead of reading the bytes at port then reading that many  bytes (which could be zero), just do a VISA read for 1 bytes.  It will wait for that byte to come in.

 

To get a boolean array out of that byte, use this code.  It turns the characters into a byte array.  Takes the first and only element of that array, then converts that single U8 number to a byte array.

 

 

What country are you in?  I've never seen or heard of orange lights before.  Only yellow lights.

Message Edited by Ravens Fan on 06-06-2009 09:54 PM
Message 9 of 14
(18,693 Views)

Hello Ravens Fan,

 

I am from Malaysia and currently studying in France. Some people call it orange lights and some say yellow lights. For me, they are the same. And in this case, our professor calls it orange lights. Haha.

 

You say just do a VISA read of one byte, is that means reducing the delay before read time to allow only one byte to be read? or you mean other things?

 

Secondly, I am having hard time trying to find the exact function or symbol in your code.

 

1.) What is the pink rectangle stands for? What is its name? How it is connected to the rest of the block?

 

2.) Is the second symbol called "String To Byte Array"? This one I found it with ease.

 

3.) The third symbol I couldn't find it at all.

 

4.) The fourth one, is it called "Number To Boolean Array"?

 

5.) The last one, I have no luck at all.

 

Thanks.

0 Kudos
Message 10 of 14
(18,665 Views)