LabVIEW Interface for Arduino Discussions

cancel
Showing results for 
Search instead for 
Did you mean: 

spi_sendReceive() bug?

In LabVIEWInterface.ino (pde), in function spi_sendReceive():

      // Send SPI Byte
      Serial.print(SPI.transfer(command[i+6]));    

... 

Send ASCII symbol to serial

Should be?

Binary data send?

      // Send SPI Byte
      Serial.write(SPI.transfer(command[i+6]));    

... 

0 Kudos
Message 1 of 9
(5,493 Views)

I think that may be correct.  Maybe that is why the SPI VIs that I'm trying to make for people don't work . . .

However, Serial.print() is used in 10 places in LIFA v2.1.1.69.  Ardiuno documenation says that Serial.print() shouldn't work at all:

http://arduino.cc/en/Serial/Print

Programming Tips

As of version 1.0, serial transmission is asynchronous; Serial.print() will return before any characters are transmitted.



I'd imagine there would be more issues for those who use Arduino 1.0 or greater.

0 Kudos
Message 2 of 9
(3,916 Views)

Asynchronous doesn't mean it would not work, only that the data is stored in some buffer and then the function returns. A seperate task/thread/interrupt routine or whatever there is on the Arduino firmware will then send out the data in the background while your program can go on.

This could be a problem if your code relies on the data being sent out when print() returns, but should work otherwise.

Rolf Kalbermatter
My Blog
0 Kudos
Message 3 of 9
(3,916 Views)

Oh.  When it says it returns before it transmits anything that implies that it never sends anything.  It doesn't explain it very well if it works lik you explain it.

0 Kudos
Message 4 of 9
(3,916 Views)

It actually does! The term asynchronous implies more or less exactly what I explained, so the rest of the sentence should be interpreted in that context.

Rolf Kalbermatter
My Blog
0 Kudos
Message 5 of 9
(3,916 Views)

Oh . . now that I think of it, that does make sense.

0 Kudos
Message 6 of 9
(3,916 Views)

Hi there,

does that mean it would be wise to change all apearances of "serial.print" to "serial.write" in order to make the current LIFA Arduino 1.0.1 compatible?

Best regards,

Jan

0 Kudos
Message 7 of 9
(3,916 Views)

You shouldn't need to do that.

0 Kudos
Message 8 of 9
(3,916 Views)

I agree that the use of Serial.print() may be causing issues for some, not related to the use of buffering, but for the reason Chupakabra discussed. My LIFA_Base was compiled on Arduino 1.0.3, and the Serial.print() implementation in spi_sendReceive() sends the value of the byte received over SPI as a series of ASCII characters instead of the single byte that LabVIEW is expecting.

For example, if the binary value 255 is received over SPI the Arduino function sends "255" in ASCII, a series of three bytes equal to: 50, 53, 53. LabVIEW is expect a packet from the Arduino with one binary character per byte received over SPI.

Modifying the LabVIEWInterface file as Chupakabra suggested looks to be the correct fix, and is working flawlessly in my implementation. While I haven't tested it, I suspect the analogReadPort() will also have issues, and should be modified to use Serial.write(). The Serial.print("sync") in processCommand() is correct.

0 Kudos
Message 9 of 9
(3,916 Views)