RS232 Receive Variable

Forum for problems or queries regarding Flowcode Comms Components. Eg LIN, I2C, SPI, RS232, CAN, IrDA etc

Moderators: Benj, Mods

Post Reply
Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

RS232 Receive Variable

Post by Ondra »

Good day all. I would like to get an example of receiving into an array variable via the RS232 component. Looking through the forum I didn't find anything.

Ondra

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: RS232 Receive Variable

Post by Steve »

Each member of an array can be treated as a separate variable. So, if you had an array "MyArr", you would use return values similar to "MyArr[0]", "MyArr[5]", and "MyArr[Idx]".

The last one shows that the index to an array can be a variable, so if the RS232 connection were sending 10 bytes of information, you could have a program similar to the following:

Code: Select all

Idx = 0

Loop 10 times

  MyArr[Idx] = ReceiveRS232Byte()

  Idx = Idx + 1

End Loop

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Looks good. Thanks Steve.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Question, How would you handle something like this. I have a device I'm communicating with. The device will be transmitting result codes back to the PIC, based on the commands I send it. I need to make decisions based on what I receive, which can be anywhere from 2 to 65 characters. How should I go about handling the loop. Do I loop for 65 the maximum characters. Now the system I'm communicating with, sends a carriage return after each result code response. I could use that, but how would I check for the carriage return in the loop? Any Ideas?

To add to this there are a total of 137 possible result codes. There are only 3 responses I will use to handle all possible result codes. Is there a better way than using If statements to handle the received results code data?

Ondra

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: RS232 Receive Variable

Post by Steve »

A carriage return is ASCII code 13, so you could check for this (use a loop with something like this: "While InChar <> 13"). But note that sometimes, the terminating characters for a line are CR (13) and LF (10).

Your other questions are difficult to answer without knowing the system you are interfacing with.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

The unit I'm working with is a dial up modem. The Responses are typical for most modems.
I'll check on whether or not it sends a LF. So that I'm prepared in the event that it does, how would you include the LF in the loop statement or do you.

Ondra

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: RS232 Receive Variable

Post by Steve »

If you get both a CR and LF, you could ignore all LFs received and just react to the reception of a CR.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Thanks for your help today Steve.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Oh., by the way, did you have any suggestion concerning my last question? "there are a total of 137 possible result codes. There are only 3 responses I will use to handle all possible result codes. Is there a better way than using If statements to handle the received result codes?" I replied to your request for the device I am controlling. "The unit I'm working with is a dial up modem." The codes are standard modem return codes. I don't need to handle all the result codes, it just seems that using a bunch of decision Icons seems a bit much. Is there a better approach.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RS232 Receive Variable

Post by Benj »

Hello Ondra

You can just use decision icons to look for the incoming codes you are going to use. Eg if it is not a valid code then simply skip the result and scan for the next.
Or you can apply an AND or OR mask to the data to reduce the number of possible inputs.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Thanks Benj
steve wrote:A carriage return is ASCII code 13, so you could check for this (use a loop with something like this: "While InChar <> 13"). But note that sometimes, the terminating characters for a line are CR (13) and LF (10).
Your other questions are difficult to answer without knowing the system you are interfacing with.
In the suggestion above if I use "while InChar<>13" as my loop control, would the loop process save the ASCII 13 in the variable then stop or would the loop be stopped before the ASCII 13 is written to the variable. And if it is written to the variable could you give me an example of the process I would need to end up with just the response code I am checking for.

Also Ben
Benj wrote: Or you can apply an AND or OR mask to the data to reduce the number of possible inputs.
"AND and OR mask" do you have any examples and or where could I find more information on using this technique

Ondra

User avatar
Steve
Matrix Staff
Posts: 3418
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: RS232 Receive Variable

Post by Steve »

Something like this would make sure the response is saved to the array without the CR and LF characters:

Code: Select all

Idx = 0
InChar = RS232Receive()
Loop While InChar <> 13    <--- check at start of loop
  If (InChar <> 10) && (InChar <> 255) && (Idx < MyMaxArrayLength)
    MyArray[Idx] = InChar
    Idx = Idx + 1
  End If
  InChar = RS232Receive()
End Loop
Note the check for Idx being less than the size of the array - this ensures that we do not fill the array beyond its maximum size.

Or you could check at the end of the loop using code similar to this:

Code: Select all

Idx = 0
Loop
  InChar = RS232Receive()
  If (InChar <> 10) && (InChar <> 13) && (InChar <> 255) && (Idx < MyMaxArrayLength)
    MyArray[Idx] = InChar
    Idx = Idx + 1
  End If
While InChar <> 13    <--- check at end of loop
Note also that in all of this, your "MyArray" variable could actually be a string - because your input is delimited with a CR, then I assume you are receiving ASCII text. A string variable can be treated as an array of bytes within a calculation icon, but can also be used within string icons.

If you were using a string instead of a normal array, you would need to terminate the string with the value zero. To do this, put "InChar[Idx] = 0" immediately after the loop in either of the examples above.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RS232 Receive Variable

Post by Benj »

Hello Ondra
n the suggestion above if I use "while InChar<>13" as my loop control, would the loop process save the ASCII 13 in the variable then stop or would the loop be stopped before the ASCII 13 is written to the variable.
Actually neither. The loop process only tests the variable at the start or the end of the loop. This is configured in the loop icon properties.
"AND and OR mask" do you have any examples and or where could I find more information on using this technique
This is a very common technique in embedded systems.

byte1 is unknown so = XXXXXXXX

Here are examples of AND and OR statements. You can see how the statement manipulates the variable to fit into specific criteria.

byte1 XXXXXXXX AND 11001100 = XX00XX00
byte1 XXXXXXXX OR 11001100 = 11XX11XX

I think this is covered by Flowcourse but if not then there will be plenty of info regarding this on the web.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Thanks so much, Steve and Benj for the thoroughness of your answers. I'm sure this topic will be very helpful to the newbies like myself.

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Hi Steve and Benj, to take this further I have created a string array, how do I convert it to a quote String only i.e change from
MyArrayVar[H]
MyArrayVar[E]
MyArrayVar[L]
MyArrayVar[L]
MyArrayVar[O]

to "HELLO"
Thanks in advance.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RS232 Receive Variable

Post by Benj »

Hello Ondra

The String can be assigned by using a string manipulation icon and the following.

MyArrayVar = "Hello"

Alternatively you can use a calculation icon with the following,

MyArrayVar[0] = 'H'
MyArrayVar[1] = 'e'
MyArrayVar[2] = 'l'
MyArrayVar[3] = 'l'
MyArrayVar[4] = 'o'
MyArrayVar[5] = 0

The final value of zero is the termination character to signify the end of the string. This termination character is used for passing strings to functions eg for the LCD and as shown below.

Then if you wanted to transmit the string say over the RS232 then you can just use the string name MyArrayVar as the parameter.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Thanks Benj. I didn't ask the question quite clearly. What I want to know is how can I turn this: -
MyArrayVar[0] = 'H'
MyArrayVar[1] = 'e'
MyArrayVar[2] = 'l'
MyArrayVar[3] = 'l'
-MyArrayVar[4] = 'o'
MyArrayVar[5] = 0

Into this "Hello"

You see what I would like to do is read in through the RS232 than check what's in the variable. Something like this: -

If MyArrayVar = Hello
do something.
end If

Can I do this.

--------------------------------------------------------------------

What I don't want to do, is this.

If MyArrayVar[0] = 'H'
MyArrayVar[1] = 'e'
MyArrayVar[2] = 'l'
MyArrayVar[3] = 'l'
MyArrayVar[4] = 'o'
MyArrayVar[5] = 0
do something

end If

Also are you saying that I can send out throught the RS232 component MyArrayVar without indexing it out? Just send out RS232(0)::SendRS232Char(MyarrayVar) and Hello would be sent out, just as if I sent RS232(0)::SendRS232Char("Hello")?


Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RS232 Receive Variable

Post by Benj »

Hello Ondra

TestString[0] is referencing a byte from the string.
TestString is referencing the entire string.

You can use the string compare function available in the string manipulation icon.

Here is an example where comparison is a byte variable and TestString is a string variable or array.

comparison = Compare$(TestString, "Hello", 1)

The comparison variable will be equal to 0 if the strings are identical.

Here is a fragment of the help file for the string manipulation compare function.
-----------------------------------------------------------------------------------------------

Compare$(string1, string2, compare_type)

Compares the two strings, parameters 1 and 2, and returns a BYTE value corresponding to the following results:

0 = strings are identical
1 = string1>string2
255 = string2>string1

The 3rd parameter, compare_type, determines whether or not the check is case sensitive. values for compare_type are:

0 = case sensitive
1 = case insensitive.
Examples

Str1 = "ABC"
Str2 = "abc"

RetVal = Compare$(Str1, Str2, 0)

RetVal is now 255 as Str2 is later in the ASCII sequence.

RetVal = Compare$(Str1, Str2, 1)

RetVal is now 0 as when case insensitive the two strings are identical.

Str2 = Str1
RetVal = Compare$(Str1, Str2, 0)

RetVal is now 0 as both strings are identical.

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Got it Benj. Thanks

Ondra

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

steve wrote:Your other questions are difficult to answer without knowing the system you are interfacing with.
Ondra wrote:The unit I'm working with is a dial up modem. The Responses are typical for most modems.
I'll check on whether or not it sends a LF. So that I'm prepared in the event that it does, how would you include the LF in the loop statement or do you.
Hi Steve, from earlier in this conversation I stated in response to your request, the device I'm working with is a modem.
Well, I went to your learning center and checked out the mobile telephony course. It looks like it can help steer me in the right direction. I would like to know, can I get the worked example .fcf files for the course? Thanks in advance for your help.

Ondra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: RS232 Receive Variable

Post by Benj »

Hello Ondra

Here are the example files from the mobile telephony course.
Attachments
Examples.zip
(19.93 KiB) Downloaded 434 times

Ondra
Posts: 325
Joined: Wed Aug 29, 2007 7:33 pm
Been thanked: 2 times
Contact:

Re: RS232 Receive Variable

Post by Ondra »

Thanks Benj.

Ondra

Post Reply