LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

sending data over rs232

Hi All

 

I have a PIC16F887 MCU which reads the temperature and humidity value from sensor  SHT21 and sending to LV to display graphically.

 

Both temperature and humidity values are each 2 bytes

and split into hi and low bytes and sending over rs232.

Then the LV compines the 2 bytes and displays.

 

Everything is working fine but sometime the LV compines 2 bytes in reverse order as in the below code which results wrong display.

 

 

         //Temperature

 

          if (UART1_Tx_Idle()==1) {              //send temperature value to LV

          UART1_Write(Hi(Ta_res));             //example 00

          UART1_Write(lo(Ta_res));              //example A9

          }                                                  //which equals to 00A9 (decimal 169) but some times LV is taking as A900 which is wrong
          delay_ms(100);

 

I have tested for only temperature.I want to send humidity value as well. How do i send both temperature and humidity values to LV to display properly.

 

          //Humidity

 

          if (UART1_Tx_Idle()==1) {           //send Humidity value to LV

          UART1_Write(Hi(Rh_res));   

          UART1_Write(lo(Rh_res)); 

          }

         delay_ms(100);

 

 

Please someone advise me.

 

Regards

 

Neo

0 Kudos
Message 1 of 8
(3,569 Views)

If you open the serial port at just the wrong time, then you could wind up catching the data in the middle of the message so you get the 2nd byte of the older message and then the first byte of the newer message.  What you need to do then is figure out a way to detect that happened, and read just 1 byte and throw it away to get back in sync.

 

If you want to read 2 pieces of data, you are better off creating a protocol where you know exactly what is being sent in what order.  Perhaps prepend the Temperature bytes with a T and the humidty bytes with an H.  That way you can check that your bytes are okay if the T and H are in the correct spot.  It won't 100% guarantee the message is correct, but it will definitely tell you if the message is wrong. 

0 Kudos
Message 2 of 8
(3,558 Views)

This question has come up many times. The read is completely asynchronous to the device doing the write. You have no mechanism for synching. One common way is to append a prefix or termination character to the data being written (making sure to eliminate that character from the data itself). You've got the VISA Read configured to use a termination character (default of 0xA or LF) and you are ignoring the warning you get when it is not detected.

0 Kudos
Message 3 of 8
(3,557 Views)

Hi

 

Thank you for your reply. I am new to LV. I would appreciate if you can briefly explain and give a working VI for this application.

 

Regards

 

Neo

0 Kudos
Message 4 of 8
(3,550 Views)
Say you are sending 4 bytes with a term character. Set the number of bytes to read to something higher. Do a read. If the actual number read is less than 4, discard. Your next read will be synched.
0 Kudos
Message 5 of 8
(3,543 Views)

We are here to help people to solve their problems, not "give working VI's".  We can't do your work for you.

 

We were all new to LabVIEW at one time and learned by doing.

 

You need to define what protocol you want to use.  Perhaps instead of sending binary data, you sent it as ASCII bytes.  That would let you use the termination character to determine when a message ends.  For example if you sent Txxx.xxHxxx.xxLF, then you you could separate out your temperature from your humidity and know when you got an entire message.

0 Kudos
Message 6 of 8
(3,541 Views)

Hi

 

I have managed to send the temperature data with prefix 'T'

  

char [4];

          tx[0] ='T';    

          tx[1] =Hi(Ta_res);

          tx[2] =Lo(Ta_res);

          tx[3] = 0xA;  //line feed

          

          if (UART1_Tx_Idle()==1) {

          UART1_Write_Text(tx); 

          }

 

I am getting the value '013C 541C 500A ' for temperature 74 degree celcius in the read buffer.(example)

 

Can you please advise me how to take out the data from the buffer and output to display in LV.

 

Regards

 

Noe

0 Kudos
Message 7 of 8
(3,508 Views)

Hey Noe,

If you are looking for some good examples of performing serial reads and writes in LabVIEW check in the Example Finder under Help->Find Examples.  In that folder hierarchy if you go to Hardware Input and Output -> Serial there are examples there of reading data from the buffer and outputting it to the screen.  If you need help parsing strings of data you receive you can find examples for that in the Example Finder under Fundamentals->Strings Please post back if you have any additional questions.

Kevin Fort
Principal Software Engineer
NI
0 Kudos
Message 8 of 8
(3,463 Views)