#1 2017-01-17 20:05:02

soxso2011
Member
From: India
Registered: 2015-11-25
Posts: 37

Serial communication via com port issues

I want to interface serial communication via com ports but the error occurs , I am using this code. Error snapshot attached.
1.jpg


 /*******************************************************
*                                                      *
*   REXLANG example - data exchange via serial line    *
*                                                      *
*   Receiver station                                   *
*                                                      *
*   (c) REX Controls, www.rexcontrols.com              *
*                                                      *
*******************************************************/

#define COM_DEVICE_FNAME 10  // serial device is defined by the fname parameter of the REXLANG block (e.g. /dev/ttyS0 in Linux or COM1: in Windows)

#define COM_BAUDRATE 9600 //baudrate, e.g. 9600, 19200, 57600, 115200

#define COM_PARITY_NONE 0  //no parity
#define COM_PARITY_ODD  1  //odd parity
#define COM_PARITY_EVEN 2  //even parity

#define BUFFER_SIZE  50    //maximum number of bytes to send

//assigning variables to outputs, these variables are WRITE-ONLY
long output(0) signal0;    //integer number received from the sender
long output(1) signal1;    //integer number received from the sender
double output(2) signal2;  //real number received from the sender
double output(3) signal3;  //real number received from the sender
long output(14) receivedBytes; //number of received bytes
long output(15) handle;    //handle of the serial device

long output(10) dat0;      //the first byte of the received data
long output(11) dat1;      //the second byte of the received data
long output(12) dat2;      //the third byte of the received data
long output(13) dat3;      //the fourth byte of the received data

//declaration of variables
long hCom;                 //communication handle
long buffer[BUFFER_SIZE];  //buffer for incoming data
long dataCnt;              //number of bytes sent
long convData[2];          //array for data conversions

/* Function for conversion of 2 numbers of type Long representing a decimal 
number in the double format according to IEEE 754 to a decimal number. 
Little-endian format is used. */
double LongAsDouble(long val[])
{
	double lbase=((double)(val[1]&0xFFFFF))/((double)0x00100000)+(val[0]&0x7FFFFFFF)*pow(2.0,-52.0);
	long lexp=(val[1]>>20)&0x7FF;
	if(val[0]&0x80000000)
		lbase+=pow(2.0,-21.0);
	if(lexp==0)
		return 0.0;
	if(lexp==0x7FF)
	{	
    //deal with NaN and Inf here if necessary
		return 1.0e60; //substitute value
	}
	lbase=(lbase+1.0)*pow(2.0,(double)(lexp-1023));
	return (val[1]&0x80000000)!=0? -lbase : lbase;
}

/* Initialization of the REXLANG algorithm */
// the init procedure is executed once when the REXLANG function block initializes
long init(void)
{
  hCom = -1;
  dataCnt = 0;
	return 0;
}

/* The body of REXLANG algorithm */
// the main procedure is executed once in each sampling period
long main(void)
{
  //long i;
  if (hCom<0)
  {
    hCom = Open(COM_DEVICE_FNAME,COM_BAUDRATE,COM_PARITY_NONE);  //opening serial device
  }
  else 
  {
    //receive the data
    dataCnt = Recv(hCom,buffer,BUFFER_SIZE); //receive data, max number of bytes = BUFFER_SIZE

    //the first signal is of type long, therefore 4 bytes
    signal0 = buffer[0] | buffer[1]<<8 | buffer[2]<<16 | buffer[3]<<24;
    dat0 = buffer[0]; //publishing received data (for debugging)
    dat1 = buffer[1]; //publishing received data (for debugging)
    dat2 = buffer[2]; //publishing received data (for debugging)
    dat3 = buffer[3]; //publishing received data (for debugging)
    
    //the second signal is binary, therefore 1 byte
    signal1 = buffer[4];
    
    //the third signal is of type double, therefore 8 bytes
    convData[0] = buffer[5] | buffer[6]<<8 | buffer[7]<<16 | buffer[8]<<24;
    convData[1] = buffer[9] | buffer[10]<<8 | buffer[11]<<16 | buffer[12]<<24;
    signal2 = LongAsDouble(convData);
    
    //the fourth signal is of type double, therefore 8 bytes
    convData[0] = buffer[13] | buffer[14]<<8 | buffer[15]<<16 | buffer[16]<<24;
    convData[1] = buffer[17] | buffer[18]<<8 | buffer[19]<<16 | buffer[20]<<24;
    signal3 = LongAsDouble(convData);
  }  
  
  //publishing the serial communication handle through output signal (for debugging)
  handle = hCom;
  //and also the number of received bytes
  receivedBytes = dataCnt;
  return 0;
}

/* Closing the REXLANG algorithm */
//the exit procedure is executed once when the task is correctly terminated
// (system shutdown, downloading new control algorithm, etc.)
long exit(void)
{
	if(hCom>=0) Close(hCom);
  return 0;
}

Offline

#2 2017-01-19 10:14:57

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 371

Re: Serial communication via com port issues

Hi soxso2011,

the example you are using was made for Rex version 2.10.8 and can be compiled without any errors in that version. Is there any specific reason for you to use Rex version 2.10.7?
I would recommend you to upgrade to current stable Rex version 2.50.1 - for changes see https://www.rexcontrols.com/development-history.

Please, note that in the current version the Open() function in REXLANG was divided to specific functions such as OpenCom(), OpenTcp(), OpenFile(), etc. - see documentation: https://www.rexcontrols.com/media/2.50. … f#page=356

Regards, Tomas

Offline

#3 2017-03-08 16:03:34

soxso2011
Member
From: India
Registered: 2015-11-25
Posts: 37

Re: Serial communication via com port issues

Thank for the reply.
Is it possible to combine this program with a parsing algorithm so that we can read a data from UDP/Realcom and parse it and then export using OPC .I want to design this complete system to read flame scanner frequency and integrate it to DCS system in 600MW power plant.

Offline

#4 2017-03-13 10:30:10

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 371

Re: Serial communication via com port issues

Hi soxso2011,

it is possible to combine Serial communication with UDP communication in one REX project.
For UDP communication see UDP Data exchange example - https://github.com/rexcontrols/REXexamp … r_Receiver

For more details about OPC implementation in REX see documentation - https://www.rexcontrols.com/media/2.50. … er_ENG.pdf

Our team can also help you with the customised implementation according to your needs - just contact us.

Regards, Tomas

Offline

Board footer

Powered by FluxBB