This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

How to set communication between Matlab and Tiva via USB

Other Parts Discussed in Thread: ENERGIA

Hi everyone!


First, this is my first post on the forum and programming a micro-controller. I'm working on a independent school project using the Tiva TM4C123G and Matlab 2015b on Ubuntu 15.04. I have tried to get Matlab to communicate with the board but it is only unidirectional. I mean the board can read the data I send from Matlab but Matlab can't read the data send from the board. At the moment of using fscanf(s,'%d') (s being the serial port object) Matlab returns the following:


Warning: Unsuccessful read: A timeout occurred before the Terminator was reached..


Nevertheless,  the board still reads the data sent from Matlab. I programmed the board using Energia with the following code:

#define LED_G GREEN_LED
#define LED_B BLUE_LED
#define LED_R RED_LED
int temp;

int matlabData;  // integer sent from Matlab

void setup()
{
  // put your setup code here, to run once:
  pinMode(LED_G, OUTPUT);
  pinMode(LED_B, OUTPUT);
  pinMode(LED_R, OUTPUT);
  Serial.begin(9600);
  delay(100);
}

void loop()
{
  // put your main code here, to run repeatedly:
  while (Serial.available() == 0)
  {
  }
  if(Serial.available()>0)
  {
     matlabData = Serial.read();
    
      if(matlabData == 1)
     {
         digitalWrite(LED_G, HIGH);
         digitalWrite(LED_B, LOW);
         digitalWrite(LED_R, LOW);
         temp = 1;
         Serial.println(temp);
         delay(10);
     }
     else if(matlabData == 2)
     {
         digitalWrite(LED_B, HIGH);
         digitalWrite(LED_G, LOW);
         digitalWrite(LED_R, LOW);
         temp = 2;
         Serial.println(temp);
         delay(10);
     }
     else if(matlabData == 3)
     {
         digitalWrite(LED_R, HIGH);
         digitalWrite(LED_B, LOW);
         digitalWrite(LED_G, LOW);
         temp = 3;
         Serial.println(temp);
         delay(10);
     }
  }
  else
  {
     digitalWrite(LED_B, LOW);
     digitalWrite(LED_G, LOW);
     digitalWrite(LED_R, LOW); 
  }
}

The code is just basic stuff to play with the on-board LED and test the communication.

On Matlab's side I wrote:

To set the communication:

function[obj,flag] = setupSerial(comPort) flag = 1; % Initialize Serial object obj = serial(comPort); set(obj,'DataBits',8); set(obj,'StopBits',1); set(obj,'BaudRate',9600); set(obj,'Parity','none'); % set(obj,'InputBufferSize',1000); fopen(obj); fscanf(obj,'%u'); end

and the main:

clear all;
close all;
clc;
port = '/dev/ttyACM0'
if (~exist('serialFlag','var'))
    [s,flag] = setupSerial(port);
end

answer  = 1;
while answer
     answer = input('0,1,2,3: ');
     fprintf(s,char(answer));
     output = fscanf(s,'%d')
end

closeSerial;


closeSerial just closes and remove the port.  I have tried for a few days but I can't figure out why it doesn't work.  It would be great if someone has an idea how to solve this. Thank you very much in advance. :)

 

  • There's a university student here, "Luis Afonso" who has published a blog which deals w/TM4C and energia: 

    sites.google.com/.../home
     
    Matlab & energia - aimed to ease - may well yield the opposite in your endeavor...   (i.e. use of Matlab & energia may not have been anticipated by system designers)

    Suspect there's more (here) detailing Matlab & this vendor's API (in C) as this has pre-existed energia & sees far more use...   Your "search" of this forum should prove of value...

    Classic troubleshooting of such issues (usually) involves your attaching your scope - and probing the key signals for, "presence, level & timing" - does it not?   We've not been so advised...

  • I wouldn't be surprised if cb1 is correct but as a simple thing to check:

    Do Matlab and Energia agree on end-of-line terminations? It is possible Matlab is waiting for something that never arrives.

    Robert
  • Well said, friend Robert.

    Such "marriage" between unwilling/unequal participants (Matlab & energia) may be (tad less) than, "Match made in heaven."    (suspect more (shotgun) than desire (or logic) induced...)

    Posters must learn to recognize such "marriages of convenience" which too often (always?) fail to (mesh) in ways one seeks/hopes...

  • Thank you for answering so quickly. Probably Matlab is waiting for some ending signal when it uses fscanf(). I used fgets() instead and it works to read single float, string and integer... I have to wait to get some sensors to connect to the Tiva and see if it still works.

  • Hi John, 

    There are few questions I have for you:

    - Did you tried serial transmit/write using energia and send the serial data to comport. Are you able to read it? I used the same code that you wrote in MATLAB which essentially opens up the comport and monitors the serial data sent by the TIVA TM4C123 launchPad. I could read the serial data.

    - As I see you use serial receive function to read the data on TIVA microcontroller, how are you receiving the data at the first place? Are you using another UART port to receive the data and send it to UART Port 0 of microcontroller? or Are you sending data from the PC using serial adapter connected to the Micro-controller?

    Thanks,

    Peeyush

  • Very neat points you raise, my friend. From both of your (considered) writings - my sense is that you'll (both) benefit from a "gentle" (i.e. "toe in the water") read/review of the UART section w/in vendor's powerful API.

    There's a thick & detailed manual, "Peripheral Driver User's Guide" (or similar) which provides a well detailed narrative describing (and amplifying) most API functions. Yes this represents a "shift" from "no-energya" but is justified by the flexibility, vastly expanded example base, and far greater capabilities - long resident w/in the API.

  • Hi Peeyush!
    The Tiva board is only connected (USB debug side) to the Linux computer via an USB cable. I can't access to the data send from the µC with Matlab using scanf() but if I use gets() it works. What code did you use in the µC to get scanf() to work? It is my first time programming a microcontroller, so I really don't know about UARTs.
  • I guess I've got to read more about those things. Any good book to start?
  • Long ago - T.I. produced an (outstanding) group of tech books - "UNDERSTANDING (Topic) series" iirc. "MCUs" in your case. Radio Shack carried these for awhile - should be on-line - Google should reveal - very readable - nicely diagrammed.

    You may also search for, "Robert's Bookshelf."   He's very skilled poster here - generously has created a highly useful reading list and shared...
     
    What version of Matlab are you using?  (suspect student copy)  I recall Matlab having a number of tutorials and/or training devices - all should prove useful...

  • Hi John,

    I am developer for MATLAB/Simulink blocks which are going to support Energia. This is going to be published soon on MATLAB File exchange community. I used my Simulink blocks to send the serial data from Micro-controller and then used the same MATLAB code that you have written to read the comport. As per the MathWorks documentation fscanf functionality operates as:

    Read the content of file or object.

    I understand in MATLAB, you have created a serial object on specified comport using 'serial' function and reading it. Now from the question on this post my interpretation is you are looking to monitor the data sent by microcontroller to the PC. In order to do this you will have to transmit the serial data from Microcontroler rather than reading it. However in case if you use serial read functions at TIVA end, you should be receiving this data from somewhere. That is why I had this query for you how are you receiving the data onto Microcontroller. The MATLAB code you have written as such is doing what any serial data monitor software would do like 'Device monitor Studio' software. We are not specifically sending the data from MATLAB in your code rather I would say you are reading it. Here is something you can try:

    Send the serial data using other UARTs of TIVA to the UART0 corresponding to serial.read(). (Serial1.read() for UART1, Serial2.read() for UART2 ref. Energia website serial lib ) and use this data to write logic for blinking LEDs. Now how MATLAB would come to this scenario is if you use MATLAB to send data to particular UART(UART1,UART2) of microcontroller which is indeed transmitting data to UART0 that you would want to read using serial.read().

    Thanks,

    Peeyush

  • Thanks, I have a look at those. Yes, I'm using student copy of Matlab 2015b. The Tiva is supported by Matlab but only on Windows not on Linux.

  • Hi Peeyush!
    If I understood correctly: I wrote the code in the µC to get the incoming signal from UART1 and send on UART0. But I don't know how to set Matlab to read from UART0 and send to UART1. Does the serial() function has some kind of flag to know which UART it has to use or do I have to create an other serial object or maybe a new port like ttyACM0?
    Just for explaining what I am trying to accomplish: my school project is to communicate with at least 3 sensors (which I haven't received yet) that would continuously send data to the Tiva board which would be then forwarding it to Matlab for calculation and graph with command buttons. When that works I will move the signal processing to the µC leaving only the graphic representation and command to Matlab.
  • Hi John,

    I am pretty sure that MatLab works by COMs so you need 2 different ports to use both serials. Still wouldn't a solution with just 1 UART be better?

    I think I made something with a MCU with matlab but I'm not sure I used Energia.
    Anyhow, if you are using Energia you should try always this. Search if anyone with a arduino did it.

    This is core functions of Energia/Arduino. The serial configs will be the same. Try searching for "Arduino with Matlab" if you can't find anything with Energia. I would say you will have not my problems in getting Matlab<->MCU working with it.

    Of course Energia is great and all, but you probably don't even know how powerful the MCU can be if you don't use those functions. I advise you this if you want to go lower level than Energia to get that extra power and features. Keep using Energia IDE, but start trying to do bits with the API Tivaware.
    You can start doing small and simple bits with Tivaware and the more complicated ones with Energia. With time you start using Tivaware more and more as you learn.
  • Hi John,

    I second to the opinion of Luis. You should use one comport and send messages from MATLAB to only one UART of TIVA. Energia is Arduino look like IDE. I understand you would like to read the sensor data in mirocontroller later and right now you are replacing sensors with MATLAB code to check if data sent by MATLAB is read by TIVA or not? Please look at the below example where excatly has been done for Arduino:

     

     

  • May a THIRD, "Vote w/Luis" be duly noted?

    My belief - o.p. may not realize that UARTs are designed as "Bi-Directional" (i.e. TWO WAY) Communication Channels.   Minus that recognition - poster adds the complexity of Second (unneeded) UART!

    Review of his MCU's UART reveals (both) UART_RX (the receiver pin) and UART_TX (transmitter pin).   Similar pins/functions are SURE to exist w/in Matlab hardware.

    As always - signal levels MUST be matched - never/ever exceeded!   The MCU's UART pins are 3V3 (3.3 volt) rated - cannot tolerate RS232 voltage levels!  (i.e. instant death/destruction!)

    Unstated (by my quick scan) is the required UART signal/voltage LEVELS @ Matlab.   If other than 3V3 - a line driver must be employed to protect poster's MCU pins.   School/Instructor appears not (fully) engaged.   (to be kind)

  • matlab is just a software so the voltage levels only depend on the hardware.

    If a usb to serial converter for 3.3v serial is used (like in uart0) then there shouldnt be any problem

  • Hi Luis,

    I "sorta" knew about Matlab - yet the HW has wide variation - does it not?    I doubt that o.p. had the full recognition:

    • of the bi-directional nature of UARTs (thus he employed two - one would do)
    • of the related Matlab hardware - and the absolute necessity of insuring that incorrect signal levels do NOT arrive @ MCU

    I'm (& many others) are "no fan" of employing the LPad's (only) debug/program interface for attachment to an external serial channel - for (other) than debug/program purposes.   Attachment to a "non-debug dedicated UART" occurs here "endlessly" (as you know) is always avoided by forum mgmt - and lurks again - right here - right now!

  • Thank you all for your quick answers. I set the µC to only use UART0 for the communication (serial.println() and serial.read()) like it was at the beginning and for some unexplained reason Matlab is now communicating with fscanf() without complaining like before. I can't explain why it now works.
    I don't know when I will receive all the material that I need to continue my project.. But, hopefully it will be soon. After I get the sensors, I'll try again and tell how it goes.
  • Good that!

    May I suggest that you "SAVE" your project - right now - in its exact WORKING State! Change nothing - save it to a safe place - so that you may recover, "just in case." (not to ask, "how I know?")

    After having done such - then (and only then) you may modify (your copied) program & play to heart's content.