RS-232 Code sample needed

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

RS-232 Code sample needed

Post by Ron »

Hi,

Could someone provide me with an example of how to send and receive data via RS232. I am a newbie but have figured out how to do almost everything I need to do for initial testing except ASCII TX & RX.

I need a way to have a PC talk to the PIC, using straight flowcode only.

Basically,

PIC will wait forever for PC to transmit CR and LF

The send ACK and ENQ back to PC to tell it to send 4 bytes

byte 1 should be tested as ASCII char STX
byte 2 should be tested for value from 0 - 255
byte 3 should be tested for value from 0 - 255
Byte 4 should be tested as ASCII char ETX

Also, what is usually done with the time out parameter.

I know it is a great deal to ask. I am hoping someone might be willing to help.

This is not everything I need but it gives me enough that I thiink I might be able to figure the rest out.

Thanks

Ron

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:

Post by Benj »

Hi Ron

First add the RS232 component into your flowchart.

Then you can use the hardware macros to do the following.

Send char

The value you send can be a variable, an element of a string, an entire string or a fixed constant.

this can take the form of 0 - 255.
To enter ASCII characters you can either use the ASCII equivalent number or use character constants.

'A'
"A"
"ABCD"

If you put a string into the send input then it will send every character in the string.

See the help file on strings for help with the available functions.

Receive char

Use a variable to store the returned data. The returned data will be an 8 bit number between 0 and 255. If the function timed out before anything was received then this returned value will be 255.

This can take the form of a single byte which is stored into an array or output straight away.

It can also take the form of an element of an string
string[0]
string[count]

You should time out fairly often to make sure that no errors occurred on the bus. To do this set a time out of between 50 and 100.

To monitor incoming messages you can build a flowchart similar to the following.

While 1
{
var1 = Receive Char
if (var1 < 255)
{
//process char
}
}

Hope this helps

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Post by Ron »

Hi,

I found the properties box of the RS232 component.

What is the difference between send Characters and send BYTES?

Is 19200 a reasonable baud rate for a 2 meter cable?

How does the Windows PC COM port need to be set up?

What is a good way to detect errors, i suspect that at 19200 I would not see any but error detection is a must. Simply echoing back what was sent does not seem very good in my mind.

Thanks

Ron

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:

Post by Benj »

Hi,

You replied to my post about RS232 sample code needed.....

I have never done any RS232 communications before. I see a box called ECHO and another called error % in the RS232 component properties box.

What do these do?

I will be communicating to a MS Windows based GUI Interface.

If the PIC sends information to the computer GUI I want the GUI to send an echo back to me so I can confirm the GUI received what I sent.

If the computer GUI sends information to the PIC I want the PIC to send an echo back to the computer so it can confirm the PIC received what the GUI sent.

Do I need to write flowchart logic to send the echo after I have received the data from the computer?

Do I need to write flowchart logic to receive the echo after I have transmitted the data from the PIC?

I want a certain level of error checking to be done, but do not have enough experience to write any kind of CRC error checking logic.



Is the RS232 macro set up so that all I do is wait for the expected value to arrive in the receive buffer and to simply place the byte I want to send in the send buffer? The macro will handle the TX and RX echo and re-transmission automatically? If the macro is set up this way how do I determine if there is a problem with to high an error % and close the communications channel? Is this the timeout setting you mentioned in your reply to my post? You mention 50 to 100, is this in milliseconds?

Do I have to "check" the echo box in order to have the macro handle the echoing back and forth between the PIC and the computer?

I am hoping that you can see what I am trying to determine and can offer some suggestions, I visited your web site and was very impressed.

I think I understand the basics of the RS232 component macro that you posted in your reply to my post, I just need to determine how to handle a low level of error checking.

Thank you for your time, I appreciate it.

Ron

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:

Post by Benj »

Hi Ron

Pasted Your message into the forum to provide help for anyone else in the same position.

A char and a byte both stand for an 8 bit number.

The char is more commonly associated with ASCII but they are both essentially the same thing.

I would use a baud rate of 9600 as this is a nice speed for the PICmicro to handle. A speed of 19200 would be ok to begin with but may lead to problems further down the line. Once multiple commands must be received etc.

To perform error detection you can either Echo the data (located in the RS232 component properties) or you can return a parity bit or checksum (eg number of logic ones or zeros in the 8 bit number). I think there is a forum topic regarding calculating parity. The computer should be able to auto echo also. With error checking you want to send and resend until the data is returned back 100% correct or the checksum is correct. Even one bit out can cause quite drastic problems depending on your application.

The %error in the RS232 property page is the error in baud rate based on the desired baud rate and crystal speed. As long as this percentage is pretty low you should get a fairly accurate connection.

A good way to get everything working correctly is to use Windows hyperterminal. I think if you paste "hypertrm" without speech marks into the Start -> Run then it should open.

You will have to set up the connection to match your hardware.
EG
Baud 9600
Hardware Flow Control Off
Echo on
Etc

Hope this helps.

Ron
Posts: 225
Joined: Wed Apr 11, 2007 6:15 pm
Has thanked: 2 times
Contact:

Post by Ron »

Hi,

Thanks for getting back to me so quickly, the service is great here. I lost 2 weeks due to flowcode distributor issues and the only time I can make it up is to work long hours on the weekend.

Does "checking" the echo mode box in the RS232 properties automatically generate the "flowcode" to echo the received data back to the computer?

HOW/when does Flowcode know to place the received character/byte in the into the receive buffer?

It seems to me that once an echo has been confirmed to be correct then the source device must send "something" to the receiving device so the receiving device knows that the NEXT character/byte is now being sent is NEW and not a re-transmit of the past charater/byte.

Does the source device return a code to the destination device once the echo has been confirmed "GOOD"?

Something like

0 (zero/zed) is true/good echo

non- 0 (zero/zed) is bad echo

Thanks

Ron

PINTO
Flowcode v5 User
Posts: 71
Joined: Fri Nov 03, 2006 2:28 pm
Location: RSA
Been thanked: 1 time
Contact:

Post by PINTO »

Hi Ron,

Check the posting on page 2 of the forum for a heading "RS232 Parity Bits"
I think this information will help you quite a bit.


Pinto

Post Reply