Unable Send Data From Matlab To DSPIC 30f4011 - matlab

I am facing some problems while trying to send data from Matlab To DSPIC30f4011 through USB to UART Converter. Here is my code:
function comparison()
global x1 y1
s=serial('COM3');
set(s,'BaudRate',9600);
fopen(s);
fprintf(s,'2');
out=fscanf(s);
fclose(s);
But my circuit board is unable to receive the data. How can I solve this problem?

Receive:
I always had problems receiving data from a serial port, so I used a mex file GetSerialData.cpp, but right now I can't figure were came from.
Send:
To send data correctly it is really important that you got the right baud rate and COM-port. You could check the com-port in your windows device manager. Mine shows up in the (COM & LPT) group as USB Serial Port (COM7).
The baud rate depends on your DSPIC30F4011 device, which you probably also have to configure.

Related

Simulink and arduino serial communication

I am running a code on arduino which works fine in arduinoide, I want to get those values in simulink for real time using serial connection.
I am burnig a program in arduino and want to access both send and receive function of serial monitor, on simulink. I want these to plot graphs in real time and run PID algorithm using simulink.
But for some odd reason, simulink values are either not updating(in external mode) or fluctuating some odd values(in normal mode). Any help.
If you are doing a communication between the Arduino and Simulink there might be some problems in your connection. I'm assuming you are using the Serial Send and Serial Receive block to do the communication.
I did a complete tutorial how to connect both platforms in my Github page.
By your description I can think of this problems:
Simulink is not updating?
If your Simulink is not updating probably because it is waiting Arduino to send some serial data, but is not receiving anything. Some possible causes of this problem might be:
Wrong Serial baud rate
Wrong data type (i.e. If you are sending Arduino float you have to receive a single in Simulink.)
Wrong data size
Different step time (Remember to use the same step time in Simulink and Arduino)
Simulink receiving odd values?
If Simulink is updating but showing odd values, the communication between both might be damaged.
Desynchronization of the communication - Try to use a Header and a Terminator in the Serial Send and Serial Receive block and remember to set this in your Arduino code.
Different step time - Make sure both application are sending and receiving at same rate.
Verify what you are sending - You can check what exactly you are sending to the serial with a scope, remember that in the Serial Send block the input signal must be a byte. If you are using a single or double remember to cast it to byte with a Byte Pack block.

How to Control first port of USRP N210 to use it when i need using matlab coding?

I’m working on a radar project using USRP N210 with UBX-40 daughterboard using matlab. I need a code to control the first port of the usrp, because the first port can be used as tx/rx. Such as in tx part, I can turn ON transmitting and OFF Receiving & vice versa. So I can transmit and receive from same antenna. Any Help!!
I haven't used matlab to interface with the USRP but I assume it uses the C API.
Here you can see all the parameters to configure the USRP. https://kb.ettus.com/Getting_Started_with_UHD_and_C%2B%2B
When you pass "TX/RX" to the antenna parameter it will automatically select the port TX/RX port for you. It is not recommended to use the same antenna, rather use the RX antenna to receive and TX/RX antenna to transmit. If you are not required to receive the signal you transmit, then you can switch between TX and RX on the same antenna with some sort of control but I still wouldn't recommend doing that :)

Matlab bluetooth connection using dongle. Need continuous data to be read

I am sending accelerometer data via bluetooth into Matlab where I will process it and build a GUI.
I am currently working on getting the bluetooth data into Matlab. I'm using a dongle which gets data into port COM18. This is the code I'm using:
s= serial ('COM18');
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
fopen(s);
When I start from scratch (disconnect and connect bluetooth and dongle, reopen matlab etc) The port successfully opens and allows me to read data. However, if I close and then try to open, it will give me an error:
Error using serial/fopen (Line 72)
Open failed: Port COM 18 is not available. Available ports: COM6, COM7, COM19.
use INSTRFIND to determine if other instrument objects are connected
to the requested device.
I know that my device isn't connected to anything else. So I then have to disconnect my bluetooth, dongle, and restart matlab.
Is there a more efficient way to do this?
Also, I am able to get values from my accelerometer into Matlab, but I don't know how to make them continuous. Each time 512 bytes are sent and if my ValuesReceived exceeds 12000, I once again have to restart my bluetooth device to get more values. I've tried flushoutput, but it hasn't worked. Any ideas on how to get continuous data into Matlab so I can process it in my GUI?
I figured out the problem. In order to not get this error anymore I simply had to follow the sequence
fclose(s);
delete(s);
clear s;
and then when I do
s= serial ('COM18');
set(s,'DataBits',8);
set(s,'StopBits',1);
set(s,'BaudRate',9600);
set(s,'Parity','none');
fopen(s);
I am no longer getting the error. It was just the order of operation to close the serial.

PC receives wrong data using high baudrate of usart

i wanted to use 4Mb baud rate of stm32f103 usart. how can i check that data received in PC are correct? I used hyper terminal but in its setting there is no 4Mb baud rate and when i run my code i receive wrong characters.but in low baud rates like 115200b data received correctly.
If the transmitter and receiver are not sending at the same speed, the receiver will erroneously read the data. Each byte has a start bit that synchronizes the receiver, and the remaining bits are determined by time.
Typical PC RS-232 serial ports only go up to 115200 bps. It is likely that your PC can't handle the 4 Mbps rate. I would recommend using 115200 or lower speed.
If you are communicating between devices and need higher speed, and just using the PC to debug, you could change the speed for debug purposes and set it faster once your comms are working. Alternatively, you could use a logic analyzer - This could be tedious to do manually, but some may have functions to read serial data.
If you have two of the stm32f19 modules, connect them using the USART at 4Mb, at then send a block of data with a checksum (or even a hardcoded block that you can compare). On the receiving unit, either confirm the checksum, or compare the data to see if the link works.

Call Arduino SPI functions with MATLAB

I have successfully connected MATLAB with my Arduino. So far, I have only sent simple tasks to the Arduino such as digitalWrite and such.
The code I have been using is as follows:
%-- connect to the board
a = arduino('COM9')
%-- specify pin mode
a.pinMode(9,'output');
%-- write 0 (off) to pin 9
a.digitalWrite(9,0);
%-- dummy variable
on = false;
%-- simple loop to make LED flash 5 times
for m in 1:5
if on
a.digitalWrite(9,0); % turn LED off
on = false;
else
a.digitalWrite(9,1); % turn LED on
on = true;
end
%-- close session
delete(a)
Now that this basic test successfully passed, I wanted to get the SPI Arduino library to work with MATLAB. Is it possible to call a function from the Arduino SPI library in my MATLAB code? Specifically, I want to get SPI.begin(); and SPI.end(); to work from MATLAB, but a.SPI.begin() is not working. Is there some step I am missing?
To get the SPI library into an Arduino program, one must use #include <SPI.h>, but how can we make sure MATLAB knows all of the functions available in the SPI library? Hopefully it is not a problem that the Arduino SPI Library code is written in a different language than what MATLAB files are written in.
References:
MATLAB Support Package for Arduino (aka ArduinoIO Package)
Arduino SPI Library
The library "ArduinoIO" does not support SPI.
That library is just a serial port listener, and every matlab/arduino instruction send a code by serial, that is readed in the sketch on the arduino, translated in the corresponding arduino's instruction, and then executed.
You can create your own block that send some your-choise-spi-command, you'll also have to edit the arduino's sketch to execure the corrisponding SPI command. But you'll have to understand how the library works, change it's code, and so on.
It is way more faster (in execution speed, as serial comunication really is slow, and coding time) to code a "specialized" arduino sketch, that send back to Serial just the value you need, and then read serial and to pc-side computation.
To communicate with SPI device using Matlab support package you can use the following code:
a = arduino();
Spi_Device = spidev(a, 'D5'); % D5 is the pin number that you want to use for chip select
writeRead(Spi_Device,[hex2dec('00'), 100]); % 100 is the value that you want to send to the device
% When you done clear the spi object
clear Spi_Device
The Legacy MATLAB and Simulink Support for Arduino is no longer supported. I would recommend using the MATLAB Support Package for Arduino Hardware as that has built-in support for basic SPI communication.
There are getting started type of examples that comes with the support package and one of them shows how to use SPI.
Disclaimer: Even though I work for MathWorks, these posts are based on my experience with the software as a user. For actual Technical Support, please contact Mathworks' TS.