-1

I've been hitting against a wall for over a week now. My current project is to use a Teensy 3.6 to send data over an SPI bus to a chip which is effective going to be a signal generator. I've made a GUI in Matlab to control this function generator in 'real-time' by sending over values such as frequency and amplitude. For my initial case, I just want to output a sine wave. to do this I am using the built in math function sin and just getting the amplitude and frequency from Matlab. However, when I send over my data I'm getting nothing on the output, but when I hard program in the values, it works just fine. From this I assume I am not sending the data over correctly OR not converting it correctly upon receiving it.

My order of operations are:

  1. send my variable to the Teensy 3.6 using fwrite in Matlab

    serialID = serial('COM5','BaudRate',115200);                    
    fopen(serialID);                           
    val = 2.55;
    fwrite(serialID, val, 'double');
    
  2. receiving the data on the Teensy 3.6 using Serial.read()

    byte temp[8];
    
    for (int c = 0; c<8; c++){
        temp[c] =Serial.read();
    }
    
  3. converting my byte array to a double using a union

    double byteToDouble(byte DoubleInBytes[8]) {
        //Receives 8 bytes values and return them into a double value
    
        union {
           double   Out;                                                      
           byte   In[8];                                                      
        } U ;
    
        for (int c = 0; c < 8; c++) {
            U.In[c] = DoubleInBytes[c];
        }
    
        return U.Out;
    }
    

I'm at a loss as to what I might be doing wrong. I've tried to use the serial monitor to monitor variable states but I can't use that while Matlab is using the COM port.

Any helpful tips would be appreciated.

  • 1
    Have you verified byte-order? And read when there is data available, i.e. check Serial.available(). – Mikael Patel Feb 21 at 18:53
  • Yes, they are both little endian. And yes, I used Serial.available to see if data is available. I'm currently reading in the data and sending back some debugging text to Matlab to see where I am in the code. When I hard code the numbers in I actually still read in data but don't use it to get into the if(Serial.available()) portion of the code. – Ryne Feb 21 at 20:08
  • You mean if (Serial.available() >= 8). – Edgar Bonet Feb 21 at 20:09
  • I did write it wrong, but not >= 8 but rather if(Serial.available() > 0) I just want to know something is ready to be pulled before starting. – Ryne Feb 22 at 20:23
0

I got a bit more traction on this issue on the Arduino forums and was able to answer it over there. I'm posting the link here to complete this post with a working answer. Hope it helps!

https://forum.arduino.cc/index.php?topic=598595.new#new

Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.