Labview <==> serial <==> Arduino Duemi

I´m trying to establish serial communications between the Arduino Duemilenova and Labview.

I managed to do this partially.

YES there is data communication. :slight_smile:

NO it´s not what I expected it to be :-/

Is there anyone here who managed to get this done the it´s supposed to be… :-?

There have been previous posts that suggested to get the data from labview to the arduino. Load the dimmer sketch ét voila…
This is not exactly true. Labview sends out HEX. This is interpreted by the Arduino in ASCII. So some conversion is needed imho.

I tried, but did not completely succeed.
Anyone who can jump in here ::slight_smile:

Here´s the Arduino Sketch I´m using

/*
Serial  Loopback with LED Dimmer
  */

const int LED = 9;      // the pin that the LED is attached to


void setup()
{
  // initialize the serial communication:
  Serial.begin(9600);
  // initialize the ledPin as an output:
  pinMode(LED, OUTPUT);
}


void loop()
{
  //The computer sends output in HEX format.
  //In Read2HEXtoDEC listen to the serial port for HEX data 
  //read the data and convert it to DEC values.
  //Then use the decimal value to operate a LED dimmer.
  
  // check if data has been sent from the computer:
  byte DECval;
  byte brightness;

  
  if (Serial.available())
   {     
    brightness = Read2HEXtoDEC();
    analogWrite(LED, brightness);
    // send the HEXarray to the serial output
    Serial.println(brightness, HEX);
   }
}





//========================================================
//===================   Functions   ======================
//========================================================




byte Read2HEXtoDEC()     // Reads a 2 byte HEX from the serial port and converts it to DEC

{    
byte Pos1;
byte Pos2;
byte dump;

Pos2 = Serial.read();      //Read the HEX characters {Pos1,Pos2}
Pos1 = Serial.read();      //
Serial.read();             //Read and dump the next character = /n

// Now convert the HEX to DEC

byte DECval = 0;

if(Pos2 <= 57)                             // Convert Pos2 from 16 base to 10 base
   {
    DECval = DECval + (Pos2-48);
   }
   else
   {
    DECval = DECval + (Pos2-55);
   }

if(Pos1 <= 57)                             // Convert Pos1 from 16 base to 10 base
   {
    DECval = DECval + 16*(Pos1-48);
   }
   else
   {
    DECval = DECval +  16*(Pos1-55);
   }

return DECval;
}

Regards,

Erwin

Pos1 and Pos2 are bytes. One represents the least significant byte of an integer, and the other represents the most significant byte. You can’t just add them together.

You need to have Read2HEXtoDEC return an int. The int is created by shifting one byte 8 bits to the left and adding the other byte to create the integer.

Which byte gets shifted? I don’t know. Pick one or the other. A simple test will tell you whether you picked the right one.

Have LabView send an integer - say 787. If the int created by Read2HEXtoDEC is 787, you picked the right byte to shift left. If not, shift the other byte and try again.

You really should ensure at least three bytes of data are available before calling Read2HEXtoDEC...

  if ( Serial.available() >= 3 )

You really should ensure at least three bytes of data are available before calling Read2HEXtoDEC...

Read2HEXtoDEC only reads 2 bytes. Why is it necessary to wait for 3 bytes to be available? I'd assume that that was a typo, but you spelled out three, so I don't think so.

From the original post…

Pos2 = Serial.read();      //Read the HEX characters {Pos1,Pos2}
Pos1 = Serial.read();      //
Serial.read();             //Read and dump the next character = /n

I missed that third read, since the value wasn't stored. I apologize for questioning your advice.

Hey guys,

Thanks for all your good advices!
I tinkered around a bit with the stuff and came up to a fine working piece of code on both sides.

Funny thing is that my ASCII to HEX decoder works fine without bit shifting. :slight_smile:
Lucky me, since I’m still a newbie in C this was easier programming for me. :-[

Ofcourse this is only some prototyping for a larger project involving more data transmission. I came up with the idea to send a start code. ARTX (ARduino TX → transmission) this tells Labview there will be some incomming data. When i split the string in labview after the ARTX code, it can handle the incomming data perfectly!

Ofcourse this might need some more code pimping on the long run, :-/ but for now I’ve build my first most expensive LED dimmer, using a laptop and an Arduino as a dimmer for an LED.
;D

E.

PS A good help on monitor the serial data being send between the computer and the Arduino was the following freeware program.
http://www.serial-port-monitor.com/

/*
Serial  Loopback with LED Dimmer
  */

const int LED = 9;      // the pin that the LED is attached to


void setup()
{
  // initialize the serial communication:
  Serial.begin(115200);
  // initialize the ledPin as an output:
  pinMode(LED, OUTPUT);
}


void loop()
{
  //The computer sends output in HEX format.
  //In Read2HEXtoDEC listen to the serial port for HEX data 
  //read the data and convert it to DEC values.
  //Then use the decimal value to operate a LED dimmer.
  
  // check if data has been sent from the computer:
  int DECval;
  int brightness;

  
  if (Serial.available() >= 2 )
   {     
    brightness = Read2HEXtoDEC();
    analogWrite(LED, brightness);
    // send the HEXarray to the serial output
    Serial.print("ARTX");
    Serial.print(brightness, HEX);
    }
}





//========================================================
//===================   Functions   ======================
//========================================================




int Read2HEXtoDEC()     // Reads a 2 byte HEX from the serial port and converts it to DEC
                        // The incomming data stream is build up as follows
                        // Example:  15[TAB]06[TAB]A3[TAB]FF[TAB]00[TAB]... etc.

{    
char Pos1;
char Pos2;

Pos1 = Serial.read();      //Read the HEX characters {Pos1,Pos2}
Pos2 = Serial.read();      //

// Now convert the HEX to DEC

int DECval = 0;

if(Pos2 <= 57)                             // Convert Pos2 from 16 base to 10 base
   {
    DECval = DECval + (Pos2-48);
   }
   else
   {
    DECval = DECval + (Pos2-55);
   }

if(Pos1 <= 57)                             // Convert Pos1 from 16 base to 10 base
   {
    DECval = DECval + 16*(Pos1-48);
   }
   else
   {
    DECval = DECval +  16*(Pos1-55);
   }

return DECval;
}

Hi Ambuman, How are you sending your HEX data from LabVIEW? Using your code, I'm not sure how to send a "dimmer" value from LabVIEW to the Arduino.

My goal is to try and read output Serial values from the Arduino and plot them as a function of time in LabVIEW. Using the example LabVIEW VI called "Basic Serial Write and Read" I can see a snippet of output values but if I run the LV VI continuously, the Arduino completely bogs down and doesn't display anything of interest.

Thanks for your help! Chris