Serial Communication POLL issue
Show older comments
I have a question about serial polling using MATLAB. I have a C# Code (below) which works fine. I am trying to create a corresponding code in MATLAB to do the same task. What I have created doesn't seem to work. What am I doing wrong?
C# Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO.Ports;
using System.Windows.Forms;
using System.Data;
namespace HypobaricChamberMonitor
{
abstract class Vaisala
{
public abstract string GetConditions(string address);
public abstract bool PortOpen();
public abstract void CloseSP();
}
class PRT : Vaisala
{
int baudrate = 4800;
int datawidth = 7;
StopBits sb = StopBits.One;
Parity pty = Parity.Even;
string newline = "\r";
int timeout = 1500;
SerialPort sp;
string conditions;
private bool portOpen = false;
//string address;
public PRT(string comport, string address)
{
sp = new SerialPort(comport, baudrate, pty, datawidth, sb);
sp.ReadTimeout = timeout;
//sp.DtrEnable = true;
//sp.RtsEnable = true;
sp.NewLine = newline;
try
{
sp.Open();
portOpen = true;
}
catch { portOpen = false; MessageBox.Show("Serial Port failed to open.", "Warning", MessageBoxButtons.OK); };
try
{
sp.WriteLine("SMODE POLL");
sp.WriteLine("UNIT P MMHG");
conditions = sp.ReadLine();
sp.WriteLine("OPEN " + address);
sp.WriteLine("SEND " + address);
conditions = sp.ReadLine();
if (!conditions.Contains("P"))
conditions = sp.ReadLine();
}
catch { conditions = "P = 000.0 mmHg T= 00.0 'C RH= 00.0 %RH"; }
}
public override string GetConditions(string address)
{
string conditions;
sp.WriteLine("SEND " + address);
conditions = sp.ReadLine();
if (!conditions.Contains("P"))
try
{
conditions = sp.ReadLine();
}
catch { conditions = "P = 000.0 mmHg T= 00.0 'C RH= 00.0 %RH"; };
return conditions;
}
public override bool PortOpen()
{
return portOpen;
}
public override void CloseSP()
{
if (sp.IsOpen)
sp.Close();
}
}
}
MATLAB Code:
s = serial('COM9');
set (s, 'BaudRate', 4800, 'DataBits', 7, 'Parity', 'Even', 'StopBits', 1);
fopen(s);
fprintf(s, 'SMODE POLL');
fprintf(s, 'UNIT P MMHG');
pause(2);
fprintf(s,'OPEN');
fprintf(s,'SEND');
pause(1);
Data = fscanf(s)
fclose(s);
clear s
In the MATLAB Code, I am just trying to Open COM Port, Get Data, and Close the Port. Thank you.
Answers (0)
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!