I am doing Project for my Final year Term work
Where I want to send digital data to Arduino -ATMega-328 Controller.
My Data will be in the form like this in Matlab
a=0001 b=0010 c=0011 d=0100 e=0101 f=0111
Data can be transmitted with the help of USB Port. So question is how to transmit data over USb from MATLAB
If the interface to your controller is serial over USB, you can simply open the device port like any normal file, and then write to it:
datastring = 'a=0001 b=0010 c=0011 d=0100 e=0101 f=0111'
fileID = fopen(portname,'w'); // open port with write permissions
fprintf(fileId,'%s',datastring); // write text to port
If this is not the case, you may want to try here or here.
Related
I have a Spektrum Radio transmitter, along with its receiver. What I am currently trying to do is by using microPython and a Raspberry Pi Pico, I want to read the data coming into the receiver, and convert that into servo commands. All I know is that the protocol used by the transmitter/receiver is DSMX. How can I go about doing this? I only need to receive, I don't need to transfer any data back from the Raspberry Pi Pico.
I'm using Thonny, and all I've done is try to use the UART module and ustruct module and create a variable using that
uart = UART(1, baudrate = 115200)
data = uart.read()
header,id,data,error_checking,trailer = ustruct.unpack('>BBHHB',data)
When trying to run this, I get thrown the error
TypeError: object with buffer protocol required
I didn't expect anything, as I don't really know what I'm doing.
Any help would be really appreciated.
You're getting that TypeError exception because your call to uart.read() is returning None (meaning that there was no data available on the serial port). I don't know anything about the DSMX protocol, but to avoid that error in your code you probably want something like:
format = 'BBHHB'
required_size = ustruct.calcsize(format)
if uart.any() >= required_size:
data = uart.read(required_size)
header,id,data,error_checking,trailer = ustruct.unpack(f'>{format}',data)
...and the above probably needs to live in some sort of loop.
I want to connect my Arduino to App Designer by using the "drop down" list. This is what my app looks like
First, I am looking for if there is any serial com. system. And I am writing them to Drop Down.
p = instrhwinfo('serial');
app.SerialPortsDropDown.Items = p.AvailableSerialPorts;
After this I have planned to read the serial port that is shown in the Drop Down and write it to serialport()
app.a = serialport(app.SerialPortsDropDown.value,9600);
Unfortunately these lines did not work. The error message I got:
Error using serialport (line 116)
Unable to connect to the serialport device at port 'COM9'. Verify that
a device is connected to the port, the port is not in use, and all
serialport input arguments and parameter values are supported by the
device.
So, the first two lines of code work. I am able to see COM9 (the com my arduino connected) in the drop-down list. This shows there is a serial port at COM9. But when it comes to reading it with app.a = serialport(app.SerialPortsDropDown.value,9600); it gives error.
How can I connect a serial port via the MATLAB App-designer?
app.a = serialport(app.SerialPortsDropDown.value,9600);
This is a wrong way of connecting Arduino to MATLAB. This declaration does not let us use Arduino functions such as 'writeDigitalPin, writePWMDutyCycle'.,
As I mentioned in the comments, it is still important to clear the port first and connect the serial port.
Lastly, the true way to declare Arduino to be able to use its functions as in the following:
app.a = arduino(app.SerialPortsDropDown.value, 'Tag of your arduino card');
I would like to read a block of data from my Arduino Mega (and also from an Arduino Micro in another project) with my Raspberry Pi via I2C. The code has to be in Perl because it's sort of a plug-in for my Home-Automation-Server.
I'm using the Device::SMBus interface and the connection works, I'm able to write and read single Bytes. I can even use writeBlockData with register address 0x00. I randomly discovererd that this address works.
But when I want to readBlockData, no register-address seems to work.
Does anyone know the correct register-address, or is that not even the problem that causes errors?
Thanks in advance
First off, which register(s) are you wanting to read? Here's an example using my RPi::I2C software (it should be exceptionally similar with the distribution you're using), along with a sketch that has a bunch of pseudo-registers configured for reading/writing.
First, the Perl code. It reads two bytes (the output of an analogRead() of pin A0 which is set up as register 80), then bit-shifts the two bytes into a 16-bit integer to get the full 0-1023 value of the pin:
use warnings;
use strict;
use RPi::I2C;
my $arduino_addr = 0x04;
my $arduino = RPi::I2C->new($arduino_addr);
my #bytes = $arduino->read_block(2, 80);
my $a0_value = ($bytes[0] << 8) | $bytes[1];
print "$a0_value\n";
Here's a full-blown Arduino sketch you can review that sets up a half dozen or so pseudo-registers, and when each register is specified, the Arduino writes or reads the appropriate data. If no register is specified, it operates on 0x00 register.
The I2C on the Arduino always does an onReceive() call before it does the onRequest() (when using Wire), so I set up a global variable reg to hold the register value, which I populate in the onReceive() interrupt, which is then used in the onRequest() call to send you the data at the pseudo-register you've specified.
The sketch itself doesn't really do anything useful, I just presented it as an example. It's actually part of my automated unit test platform for my RPi::WiringPi distribution.
I have a Verilog code simulated and synthesized on ISE design toolkit. I've got an FPGA spartan 6 device which is to be used for the implementation. But there is a problem with the device (probably a power issue) which makes the device unavailable in any of the COM ports when I connected it to my PC. So I want to check whether my Matlab code which I made for serial communication through the device does the desired job. So I need a method to test serial communication via any of the COM ports without connecting a serial com device to the PC. Is there any such method that I can Tx Rx serial data from Matlab to COM ports? Any software or any other method would be highly appreciated :)
I found a way to test Matlab serial communication using virtual serial ports.
Download "Freeware Virtual COM Ports Emulator" from: http://freevirtualserialports.com/
I installed it in Windows 10, and it's working (as trial).
Add a pair of two serial ports:
Execute the following Matlab code sample to verify it's working:
s3 = serial('COM3','BaudRate',115200);
s4 = serial('COM4','BaudRate',115200);
fopen(s3);
fopen(s4);
fwrite(s3, uint8([1, 2, 3, 4, 5]));
%fprintf(s3, '12345');
pause(0.1);
RxBuf = fread(s4, 5)
fclose(s3);
delete(s3);
clear s3
fclose(s4);
delete(s4);
clear s4
The output is:
RxBuf =
1
2
3
4
5
Bypassing the problem "it only stays for a single test session".
There is a problem when creating a pair of virtual ports using the software, it only stays for a single test session.
I guess it's a problem with the COM port emulation software.
The following solution, is not a good practice (and not a true solution).
Declare the serial object as global, keeping the object persistent.
Create the serial object only if it's not created.
Don't delete and don't clear the serial object.
See the following code sample:
global s3 s4
if isempty(s3)
s3 = serial('COM3','BaudRate',115200);
end
if isempty(s4)
s4 = serial('COM4','BaudRate',115200);
end
fopen(s3);
fopen(s4);
fwrite(s3, uint8([1, 2, 3, 4, 5]));
pause(0.1);
RxBuf = fread(s4, 5)
fclose(s3);
%delete(s3);
%clear s3
fclose(s4);
%delete(s4);
%clear s4
You can also look for a better virtual COM port software.
As Rotem suggested, if you need to communicate via serial line between 2 program of your PC you need a virtual COM port emulator.
It seems you are running on Windows OS so I would recommend a completely free emulator (not a trial one). For Windows I use com0com Null-modem emulator (from SourceForge).
In the example below I will show how to communicate with "another" device so Matlab will not handle both side of the communication. The other device will be simulated by a simple terminal. For windows I use RealTerm: Serial/TCP Terminal (also from SourceForge).
Setup:
Execute the setup of both program with all default options. by default com0com will create a virtual pair COM3/COM4 but if these port already exist on your system the program may assign other numbers. Check the numbers before you run the example. (it will also create a CNCA0/CNCB0 pair but you can ignore this one for now).
For RealTerm, once installed (don't forget to activate the server registration at the end of the setup, it should be ticked by default though), it will look like below. Keep all default options, just set the port number and the baud rate if they need to be changed.
Test MATLAB -> Terminal
You are ready to send Ascii characters or binary values from MATLAB to your device. The animation below shows you an example of both option:
you can click on the picture to see it full size. It is running in loop so you may want to wait until it restart from the beginning.
Test Terminal -> MATLAB
Below animation shows you how to test the communication in the other way:
Don't forget to tick [CR] [LF] on RealTerm when you send Ascii characters and want to use the '%s' format specifier on MATLAB, as it needs these characters to detect the end of the string.
Note:
If you have another terminal program that you are more used too, it
will work the same.
If the RealTerm option does not suit you, or if you want to handle
both sides of communication from Matlab, then you can use the code
provided by Rotem in his first answer. Just install com0com but
ignore all the RealTerm part.
I have two laptops are connected via a USB cable, the USB on one end is attached to a USB to serial adapter. I need to send information from one laptop (Mac) to the other laptop (PC) through Matlab.
I know how to use fopen to use serial ports in Matlab to send these "markers" to the second laptop. However, I am unsure on how to read them at the receiving end.
I currently have the following code where the serial connection is opened:
% connect to serial port to send markers
portID = '/dev/tty.USA28X145P2.2';
global markerID
[markerID, err] = fopen(portID, 'w');
if ~isempty(err)
error('An error was returned whilst connecting to the serial port
to send markers. The error was:\n\t"%s"', err);
end
My Matlab script then calls a function that uses fwrite to write the information that I need to the object that was opened using fopen.
Now I am unsure how to "receive" these in Matlab on the other laptop. Do I need to use fscanf?
I am quite new to this so I'm not sure how to proceed. Any suggestions would be appreciated!
Thanks