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. :)