Send data to Serial Port in MATLAB

Hello friends, hope you all are having fun and enjoying life. In today's post we are gonna see how to send data to serial port in MATLAB. Its a requested tutorial, asked by a follower and after giving him the code, I thought to share it on our blog so that others could also get benefit from it. We have discussed serial port many times and have seen how to communicate with it using different software but we haven't yet discussed how to send data to serial port in MATLAB. So, in today's post I am gonna share the complete code for sending data to serial port in MATLAB.

Serial port is most common way of communication, we can send or receive data using serial port. Normally, in engineering projects there's a need to send or receive data from microcontrollers to computer and in such projects, we used serial communication as its easy and quite quick in communication.

Send data to Serial Port in MATLAB

  • Its a quite simple project in which I am gonna send character over the serial port in MATLAB.
  • In order to do so first of all, I am gonna create an object and assign it to serial port object in MATLAB.
  • After that I am gonna set the properties of that serial port object.
  • After setting the properties, what we need to do is simple start our serial port object.
  • Once its started, now you can send any character via that serial port object.
  • After sending your desired characters, now what you need to do is simply close the serial port.
  • Closing the serial port is very essential in MATLAB because if its left open then you can't open it again in MATLAB and you need to restart your computer so be careful.
  • Here's the simplest code for sending the data:
  • Code 1:
tep=serial('COM1', 'BaudRate', 9600); fopen(tep); fprintf(tep,'a'); fclose(tep);
  • Now you can see, its too simple, we just set the com port with which we want to communicate and after that we gave the bud rate.
  • In the next line, we open our serial port object.
  • Now as our serial port is open, we can send any character to it. In this code, I am sending 'a' to serial port in MATLAB.
  • And finally I closed the serial port, which is very necessary as otherwise when you run this code again, it will start giving error.
  • Here's a bit explanatory code and much more flexible as you can change any property of Serial Port in MATLAB, you want to change.
  • Here's the code:
  • Code 2:
clc clear all close all disp(' Welcome to TEP!!!'); disp(' '); disp(' www.TheEngineeringProjects.com'); disp(' '); tep=serial('COM1'); % assign serial port object set(tep, 'BaudRate', 9600); % set BaudRate to 9600 set(tep, 'Parity', 'none'); % set Parity Bit to None set(tep, 'DataBits', 8); % set DataBits to 8 set(tep, 'StopBit', 1); % set StopBit to 1 %display the properties of serial port object in MATLAB Window disp(get(tep,{'Type','Name','Port','BaudRate','Parity','DataBits','StopBits'})); fopen(tep); % Open Serial Port Object fprintf(tep,'a'); %Print character 'a' to the serial port disp('Charater sent to Serial Port is "a".'); fclose(tep); %Close Serial Port Object
  • The code is quite self explanatory plus I have also added the comments in the code which will help you in understanding the code but still if you have any problem, then ask in comments.
  • Here's a screen shot of the above code:
  • Wen you run this code, you will get a below response in your MATLAB window:
  • Till now, we have seen how to send a single character defined in the m file of MATLAB, now let's make it a bit complex and send user defined data.
  • Now before sending the data, we will first ask the user to enter the data, he wants to send to serial port. Tis data could be a single character or could also be a combination of characters.
  • In order to do so, we are gonna use Input command in MATLAB and code is as follows:
  • Code 3:
clc clear all close all disp(' Welcome to TEP!!!'); disp(' '); disp(' www.TheEngineeringProjects.com'); disp(' '); tep=serial('COM1'); % assign serial port object set(tep, 'BaudRate', 9600); % set BaudRate to 9600 set(tep, 'Parity', 'none'); % set Parity Bit to None set(tep, 'DataBits', 8); % set DataBits to 8 set(tep, 'StopBit', 1); % set StopBit to 1 %display the properties of serial port object in MATLAB Window disp(get(tep,{'Type','Name','Port','BaudRate','Parity','DataBits','StopBits'})); fopen(tep); % Open Serial Port Object data = input('Enter character: ', 's'); %Ask user to Enter character fprintf(tep,data); %Print character 'a' to the serial port disp('Charater sent to Serial Port is:'); disp(data); fclose(tep); %Close Serial Port Object
  • The screenshot of code is as follows: (I am adding the screen shot of code because its colored and thus helps better in understanding the code)
  • Now when you run this m file, you will get results as shown in below figure and now you can see I have sent my blog url via serial port in Matlab.
  • I think we have played enough with sending data via serial port in MATLAB, now you can send any data via serial port in MATLAB, for example you can also create an infinite loop and keep on sending data to serial port.
  • That's all for today, in the coming post I will show how to receive data via serial terminal in MATLAB, so stay tuned and also subscribe us via email to get these exciting tutorials straight in your mailbox. Take care. :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir