Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

Problem while reading data from arduino

Problem while reading data from arduino

Hi,
I am trying to communicate with arduino through Serial port using Modelica_DeviceDrivers package. I'm successfully sending the data (sample code below).

Code:


model arduino
  parameter String Device_name = "COM5";
  import Modelica_DeviceDrivers.Communication.SerialPort;
  import Modelica_DeviceDrivers.Utilities.Types.SerialBaudRate;
  import Modelica_DeviceDrivers.Utilities.Types.ByteOrder;
  import Modelica_DeviceDrivers.Communication.SerialPort_;
  import Modelica_DeviceDrivers.Packaging.SerialPackager;
  import Modelica_DeviceDrivers.Packaging.SerialPackager_;
  parameter SerialBaudRate baud = SerialBaudRate.B9600;
  parameter ByteOrder byteorder = ByteOrder.BE;
  SerialPort OpenPort = SerialPort.constructor(Device_name, 16 * 1024, 0, 0, baud);
  SerialPackager pkg = SerialPackager.constructor(4608);
  parameter Integer S1[1] = {50};
  algorithm
  SerialPackager_.setPos(pkg, 0);
  SerialPackager_.addInteger(pkg, S1, byteorder);
  SerialPort_.sendTo(OpenPort, pkg, 4608); 
end arduino;

But I'm not able to receive data from arduino. (See the code below).

Code:


model arduinoread
  parameter String Device_name = "COM5";
  import Modelica_DeviceDrivers.Communication.SerialPort;
  import Modelica_DeviceDrivers.Utilities.Types.SerialBaudRate;
  import Modelica_DeviceDrivers.Utilities.Types.ByteOrder;
  import Modelica_DeviceDrivers.Communication.SerialPort_;
  import Modelica_DeviceDrivers.Packaging.SerialPackager;
  import Modelica_DeviceDrivers.Packaging.SerialPackager_;
  parameter SerialBaudRate baud = SerialBaudRate.B9600;
  parameter ByteOrder byteorder = ByteOrder.LE;
  SerialPort readPort = SerialPort.constructor(Device_name, 16 * 1024, 0, 1, baud);
  SerialPackager pkg = SerialPackager.constructor(4608);
  //SerialPackager pkg1 = SerialPackager.constructor(4608);
  //parameter Integer S1[1] = {50};
  //Real n;
  Integer I[16];
  algorithm
  SerialPackager_.setPos(pkg, 0);
  SerialPort_.read(readPort,pkg);
  //n=SerialPort_.getReceivedBytes(readPort);
  I:=SerialPackager_.getInteger(pkg,16,byteorder);
end arduinoread;

Can anyone help me regarding this?

There are 0 guests and 0 other users also viewing this topic
You are here: