norm.boessler hat geschrieben:
Igor, could you give an example for this. Would be greatly appreciated.
Norm.
I have made a little sample showing how the addressing overlap works. It includes a visualization for demonstration. Once you unzip it, you should be able to import it into a new project and run it in simulation mode.
When you define variables that overlap in the marker address area, in this example a string and an array of bytes, it is like a union in C. You can put a string into the string variable and then each character can be pulled out from the overlapping byte array.
In the program, I only access MyString but as soon as you copy a string into the MyString variable, the bytes of it are immediately available in the MyBArray array.
Here is the Text if you can't import the attached file.
VAR_GLOBAL(* Declarethestringandbytearrayatthesamelocationinmarkermemory *)(*Inpractice, thismaybeinaglobalvariablefileforaccessbyotherrouines*)
  MyString  AT%MW0:    STRING(80);
  MyBArray  AT%MW0:    ARRAY [0..79] OFBYTE;END_VARPROGRAMPLC_PRGVAR
  Init:        BOOL :=TRUE;
  MyBool:    BOOL;
  MyBoolOS:    R_TRIG;
  MyInt:      INT:=0;
  MyList:     ARRAY [0..4] OF STRING(80) :=
    'One String', 'Two String', 'Three String', 'Four String', 'Five String';END_VARIFInitTHEN
  MyString  :=MyList[MyInt];
  MyInt    :=MyInt+1;
  Init      :=FALSE;END_IFMyBoolOS(CLK:=MyBool);IFMyBoolOS.QTHEN
  MyString  :=MyList[MyInt];
  MyInt    :=MyInt+1;
  IFMyInt>4THENMyInt :=0; END_IFEND_IF
Great examples. I've learned much from this. Thanks to all who helped!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2010-03-03
Originally created by: HASAN SERKAN DURMU
Norm,
Can you please share lib that you used for coding your rs232 sample program since my sample program giveS an error which has mentioned about unknown type FB_PUT_RS232_CHAR and unknown type FB_GET_RS232_TX_BUF_COUNT.
i am looking forward your response,
Best Wishes,
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You can also use a pointer for this, similar to the M solution above but without the need to know the M address of the source string.
PROGRAMPLC_PRGVAR
  Text : STRING;
  Index: INT;
  Char : POINTERTOBYTE;END_VARText:='Hello world!';Char:=ADR(Text);FORIndex:=0TOLEN(Text)DO
  Send(Char^);
  (*Gotothenextcharacter*)
  Char:=Char+1;END_FOR;
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I want to send strings to the rs232 which needs to be 1 byte at a time.
The CoDeSys RS232 tutorial is missing the function code which would show me this so any assistance would be great.
Essentially I imagine something like:
s string;
b byte;
for i := 1 to len(s) do
b := string_to_byte(mid(s,1,i));
SendChar(b);
end_for;
I get error with the conversion from string to byte in this example.
Or perhaps a string converted to array of bytes and then iterate through the index to send each byte.
you can find examples for this code in the open source library OSCAT.LIB
to be downloaded from w www.oscat.de w
pls look at the modules buffer_to_string and string_to_buffer
Thanks Hugo. Is good.
For anyone else interested, below is what I did minus full error handling.
My function is arbitrarily set to handle up to 80 characters.
Calling function:
FUNCTION F_SEND_TEXT_RS232 : BOOL
VAR_INPUT
sText : STRING(80);
END_VAR
VAR
i : INT;
SendData: FB_PUT_RS232_CHAR;
fbGetRS232TxBufCount : FB_GET_RS232_TX_BUF_COUNT;
abText : ARRAY[0..80] OF BYTE;
iLength : INT;
END_VAR
F_STRING_TO_BYTE_ARRAY(sText,0,ADR(abText),SIZEOF(abText));
FOR i := 0 TO LEN(sText) DO
END_FOR;
F_SEND_TEXT_RS232 := TRUE;
Implementation of string to byte array:
FUNCTION F_STRING_TO_BYTE_ARRAY : BOOL
( Usage: F_STRING_TO_BYTE_ARRAY(str, pos, adr("array"),sizeof("array")); )
VAR_INPUT
END_VAR
VAR
END_VAR
ps := ADR(str);
end := MIN(pos + LEN(str) - 1,UINT_TO_INT(size));
FOR i := pos TO end DO
END_FOR;
F_STRING_TO_BYTE_ARRAY := TRUE;
Another one simple method is to declare the 'string' and the 'byte array' on the same direct address in 'M' memory.
Igor, could you give an example for this. Would be greatly appreciated.
Norm.
there is a simple solution to you're problem
declare a pointer byte array with the same size as you're string
byByte:BYTE;
sString:STRING(8 ); (a normal string has a size of 80 chars)
abyByte: pointer to POINTER TO ARRAY[0..7] OF BYTE;
abyByte:=ADR(sString); (if lengths do not match you can get real weird problems)
byByte:=abyByte^[i];
I have made a little sample showing how the addressing overlap works. It includes a visualization for demonstration. Once you unzip it, you should be able to import it into a new project and run it in simulation mode.
When you define variables that overlap in the marker address area, in this example a string and an array of bytes, it is like a union in C. You can put a string into the string variable and then each character can be pulled out from the overlapping byte array.
In the program, I only access MyString but as soon as you copy a string into the MyString variable, the bytes of it are immediately available in the MyBArray array.
Here is the Text if you can't import the attached file.
OVERLAP.zip [1.22 KiB]
Great examples. I've learned much from this. Thanks to all who helped!
Originally created by: HASAN SERKAN DURMU
Norm,
Can you please share lib that you used for coding your rs232 sample program since my sample program giveS an error which has mentioned about unknown type FB_PUT_RS232_CHAR and unknown type FB_GET_RS232_TX_BUF_COUNT.
i am looking forward your response,
Best Wishes,
You can also use a pointer for this, similar to the M solution above but without the need to know the M address of the source string.