Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

Matlab RS232 COM port can not read data!

1 view (last 30 days)
michael woodcock
michael woodcock on 18 Jul 2019
Closed: michael woodcock on 18 Jul 2019
Hey guys I'm trying to get the basics down for matlab serial communication but I'm having trouble. Here is where I'm at with the serial hardware, without involving matlab:
  1. arduino communicating to TTL/rs232 usb converter
  2. confirmed the above setup works. opened termite terminal viewer and used the arduion terminal monitor and I was easily able to send data from com port 9 to 8
  3. also passed the "echo test"
Where I'm having trouble is reading the data from the serial device in MATLAB. I've tried to many things, over and over again. I just started from scratch, here's the matlab code:
clear
clc
%Inializing com port
%this seems to initialize serial: s=serial('COM8','BaudRate',9600,'Terminator','CR')
%serial='COM8'
s=serial('COM8')
fopen(s)
% fprintf(serial,'*IDN') this would just open com 8 to print stuff too
%&fscanf(s) reads data from serial port
%serial port opject in this case: fopen(serial-COM8)
%serial name: Serial-COM8
%Port Name: COM8
fprintf(s,['ch:%d scale:%d'],[1 20e-3],'sync')
Here's the arduino code:
/*
* Hello World!
*
* This is the Hello World! for Arduino.
* It shows how to send data to the computer
*/
void setup() // run once, when the sketch starts
{
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() // run over and over again
{
Serial.println("Hello world!"); // prints hello with ending line break
delay(1000);
}
I should be able to read "Hello world" from the arduino on COM8, but I simply can't.
OK guys I found an example code here:
and it works. I'm suprized at the format. Do We really have to open and close the port,? Also I was kindof unaware of teh query command,
my last question is how do we send data to serial port?
Here's the example code I found that worked to read it:
% Create a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', 'COM3', 'Tag', '');
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('COM8');
else
fclose(obj1);
obj1 = obj1(1)
end
% Connect to instrument object, obj1.
fopen(obj1);
% Communicating with instrument object, obj1.
data1 = query(obj1, '*IDN?');
% Disconnect from instrument object, obj1.
fclose(obj1);
% Clean up all objects.
delete(obj1);
fprintf(data1)

Answers (0)

This question is closed.

Products


Release

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!