chWrite (Function)

Top  Previous  Next

Architecture:

X32 / NX32 / NX32L

Device support:

ALL

Firmware version:

1.00 / 1.00.00


The "chWrite()" function will write a message to the specified channel.

If the number of messages currently in the channel has reached the maximum as specified with chInit(), the calling thread will be blocked due to waiting for another thread to read a message by using the chRead() function. Optionally, a timeout for the write operation can be specified.

 

Also see chRead() and the section on Using Multithreading.

 

 

Special note on writing strings to a channel:

A STRING cannot be written directly to a channel but must be converted into pure memory with the strToMemory() function before being written to a channel. When this message is received by the chRead() function, it can be converted back to a string again by using the strFromMemory() function.

 

 

Input:

ch : CHANNEL

The channel to write the message to.                

 

msg : PTR

Pointer to buffer where the message is to be stored.

 

len : INT

The length of the message.

 

timeout : INT (default: -1)

The time, in ms, to wait before giving up when the channel is full.

Use -1 to wait forever. This is the default.

 

Returns: INT

-3

Memory allocation error.

-1

Channel not initialized.

0

Timeout.

>0

Number of bytes written to channel. This will always be the same as len.

 

Declaration:

FUNCTION chWrite : INT;
VAR_INPUT
  ch       : CHANNEL;
  msg     : PTR;
  len     : INT;
  timeout : INT := -1;
END_VAR;

 

 

Example:

INCLUDE rtcu.inc
 
VAR_INPUT
  al_1 : BOOL R_EDGE;
  al_2 : BOOL R_EDGE;
END_VAR;
 
VAR
  chalarm : CHANNEL;
END_VAR;
 
//SMS thread
THREAD_BLOCK smsalarm;
VAR
  alarmno : SINT;
END_VAR;
 
WHILE TRUE DO
  chRead(ch:=chalarm,msg:=ADDR(alarmno),lenmax:=SIZEOF(alarmno));
  gsmSendSMS(phonenumber := "22448899", message := strFormat(format:="Alarm. Door \1", v1:=alarmno));
END_WHILE;
 
END_THREAD_BLOCK;
 
PROGRAM test;
VAR
  smshandler : smsalarm;
  alarm     : sint;
END_VAR;
 
//main thread
chalarm := chInit(msgMax:=10);
smshandler();
gsmPower(power := TRUE);
BEGIN
  IF al_1 THEN
     alarm:=1;
     chWrite(ch:=chalarm,msg:=ADDR(alarm),len:=SIZEOF(alarm));
  END_IF;
  IF al_2 THEN
     alarm:=2;
     chWrite(ch:=chalarm,msg:=ADDR(alarm),len:=SIZEOF(alarm));
  END_IF;
END;
 
END_PROGRAM;