Page 1 of 1

Triggering an event when specific string received via RS232

Posted: Tue Sep 22, 2009 1:08 pm
by ste5442
Hi All,

I have been using FC V4 for a few days now but cannot figure out how to trigger some even when i receive a specific string over RS232 (from Hyperterminal).
I can receive characters from Hyperterminal and dislpay them on the eblocks LCD and I can also send characters and strings to Hyperterminal from the eblocks. Basically a 'hello world' from the eblocks system and also a 'hello world' from Hyperterminal to the eblocks system! This at least proves my hardware is connected ok.

What i need to be able to do is enter, say, "turnlightson" in Hyperterminal then press the 'enter' key and have the eblocks turn on an LED when it receives this "turnlightson" string. It will turn the LED off when I send "turnlightsoff" from Hyperterminal to the eblocks.

Hopefully any help you guys give will not depend on any flow-control (RTS/CTS) since I cannot seem to get these to work either!

Any ideas would be appreciated!

Thanks.

Re: Triggering an event when specific string received via RS232

Posted: Wed Sep 23, 2009 8:24 am
by Benj
Hello

The way to acheive this would be to use a string comparison or a series of them if you require muliple commands. The string comparison gives you a value of 0 if the strings are identical so you can then use a decision box to see if the variable is 0. If it is then you have received your command.

Alternativly you can do the control more on a character basis.

Eg:

A - Switch On Lights
B - Switch Off Lights

Then all you have to do is receive a single byte from the RS232 and then use a decision icon or a switch icon to check if you have received the supported characters.

decision: incoming = 'A'
yes: switch on lights
no: do nothing

Re: Triggering an event when specific string received via RS232

Posted: Wed Sep 23, 2009 12:11 pm
by ste5442
Benj wrote:Hello

The way to acheive this would be to use a string comparison or a series of them if you require muliple commands. The string comparison gives you a value of 0 if the strings are identical so you can then use a decision box to see if the variable is 0. If it is then you have received your command.

Alternativly you can do the control more on a character basis.

Eg:

A - Switch On Lights
B - Switch Off Lights

Then all you have to do is receive a single byte from the RS232 and then use a decision icon or a switch icon to check if you have received the supported characters.

decision: incoming = 'A'
yes: switch on lights
no: do nothing

Thanks Ben - do you have any working examples of string compare working over the uart to get me going?

Re: Triggering an event when specific string received via RS232

Posted: Thu Sep 24, 2009 8:06 pm
by ste5442
Hi Ben,

Sorry but I am still struggling with this one - I have tried a number of options:

1. Reading the input from the serial port and converting to a char and then comparing this char to another in a decision box.
2. Doing a string compare (details seem sketchy on how to use string-compare though).
3. Converting the string to a float (I also tried int) and comparing it to a value for decision.

If you could give a working example of simply listening to the serial port for a string (say "lightson" and "lightsoff" followed by the 'enter' key) and then turning an LED on/off based on the string it would be extremely helpful. I am currently at the point where I know the HW works fine (comms to PC and receiving from PC over serial) but I just cannot get over that first step of the string based decision making! :| Once I know how it works I will be up and running!

If you can help me out it really would be greatly appreciated.

The idea is:
  • Open Hyperterminal.
    Type "lightson"
    Press ENTER
    An LED on the EB004 will illuminate (or a message will appear on the EB005 LCD - maybe something like "lights are on").
    Type "lightsoff"
    Press ENTER
    The LED illuminated earlier will turn off (or a message will appear on the EB005 LCD - maybe something like "lights are off").
Thanks!

Re: Triggering an event when specific string received via RS232

Posted: Fri Sep 25, 2009 9:15 am
by Benj
Hello

This example should get you started.

Re: Triggering an event when specific string received via RS232

Posted: Fri Sep 25, 2009 10:24 am
by ste5442
Many thanks Ben - I will give it a try later and let you know how things work!
After looking at your file it has become apparent that I would not have stumbled upon the solution myself.

Thanks!
S

Re: Triggering an event when specific string received via RS232

Posted: Fri Sep 25, 2009 11:47 am
by Benj
Hi Steve

I think its just something that comes with experience, Let me know how you get on.

You might want to improve the CollectCommand macro by adding the following to the while loop statement.

Instead of
in_byte <> 13

Use
in_byte <> 13 && idx < 20

This way if you fill the contents of your string variable and do not receive a carriage return then the function ends and does not start modifying undefined sections of memory.

You could also make the string variable larger using the variable manager. This would allow you to use bigger commands but I think 20 characters should be more then enough.

Re: Triggering an event when specific string received via RS232

Posted: Fri Sep 25, 2009 11:56 am
by Benj
In response to your other attempts.

1. Reading the input from the serial port and converting to a char and then comparing this char to another in a decision box.

This would work for a single ASCII character but would get complicated quickly for more then one character at a time.

eg
Decision: in_byte = 'a'

2. Doing a string compare (details seem sketchy on how to use string-compare though).

String comparisons are ok and this is the method I chose however each string you use for comparisons will consume memory. The smaller your command strings the more you will be able to fit into memory.

3. Converting the string to a float (I also tried int) and comparing it to a value for decision.

String & Float conversion functions only work when the string contains an Ascii representation of a number.

eg "1.34" would convert into a float value of 1.34, "abc" would not and you would get a float value of 0.

Re: Triggering an event when specific string received via RS232

Posted: Tue Sep 29, 2009 6:33 am
by ste5442
ste5442 wrote:Many thanks Ben - I will give it a try later and let you know how things work!
After looking at your file it has become apparent that I would not have stumbled upon the solution myself.

Thanks!
S
Hi Ben - It didnt work for some reason. It programmed ok but there was no change on PortD when the command was entered into Hyperterminal.
The baud rate and flow control is setup correctly and 'enter' was pressed after the 'lightson' text.
Did you give the code a try on HW or was it just simulated?

S

Re: Triggering an event when specific string received via RS232

Posted: Tue Sep 29, 2009 9:03 am
by Benj
Hello

I have just tested the program on hardware and it is working correctly. The output is on PortA bit0 in my example. I have slightly modified the program so you can see the characters as you type in hyperterminal and you get a line feed and carriage return when you enter a command.

If you are changing the target device from a 16F88 then remember that you will also need to reassign the chip configuration to allow the device to run.

Re: Triggering an event when specific string received via RS232

Posted: Tue Sep 29, 2009 10:10 pm
by ste5442
Hi Ben,

I gave the new code a try but it still does not work all the time?
I forget to mention but I am using the EB039 USB<>UART converter block. I have also swapped my 18F4520 for a 16F88 (the one which came with the eblocks) so I have identical HW to you.
In the interests of shutting me up (!) can you let me know you Hyperterminal settings (standard and advanced settings) and also the settings for the EB039 HW and driver (within device manager). Also can you confirm the driver which I should be using for the USB<>UART block?
This seems to be one hell of a stumbling block for me :-)

Thanks again!

Steven.

PS> Flowcode 4 will sometimes not autodetect (or program) the inserted chip when a second USB cable is plugged into the EB039 USB<>UART converter - strange I know but all is fixed when I remove the cable.

Re: Triggering an event when specific string received via RS232

Posted: Wed Sep 30, 2009 8:56 am
by Benj
Hello Steven

My hyperterminal settings use a baud rate of 9600 with 1 start bit and 1 stop bit and no flow control. All the other settings in Hyperterminal are set to default. Ie no local echo of characters etc.

The drivers and the datasheet for the USB232 board can be found here.
http://www.matrixmultimedia.com/eblock_ ... .php#EB039

I will look into the EB006 USB problem with the USB232 also plugged in and see if I can replicate your problem.

One thing to remember when using hyperterminal with serial emulators is to always disconnect the connection in Hyperterminal before you remove the USB232 board from the PC. This will allow you to reopen the connection when the USB232 board is plugged back in without further messing around.

Re: Triggering an event when specific string received via RS232

Posted: Mon Oct 05, 2009 1:07 pm
by ste5442
Hi Ben, me again!

Still not working and would like to know some more info.

1. Can you let me know, for the 16F88, which jumper settings for the flow control I need to use on the USB<>serial board (EB039-00-1). If i am not using flow control do i need to connect RTS/CTS together?
2. Can I trouble you for a screen grab of the settings for the COM port - the settings changeable in Device Manager? If you left these as defauls can you let me know what the defaults are?
3. Can you doubly confirm that it is working on your HW?
4. If there is a particular order to do things ie. remove USB cable, open hyperterminal, connect etc. please let me know.#
5. Are you typing the 'lightson' and 'lightsoff' characters into hyperterminal and then pressing enter or do you cut/paste or some other method?

I can confirm that when the program runs there are characters echoed back to HT as you suggest (there is also the CR and LF when i press enter) - unfortunately there is no action from the LED when the correct string is sent? Its not a damaged LED because I can manipulate it with other programs and the USB<>Serial board definately does something because the characters are echoed back from the software you wrote.

This is very odd - it must be some settings problem but I dont know where?!

There are settings for Hyperterminal, the USB232 COM port (in device manager) and also the USB232 E-Block under 'Universal Serial Bus Controllers' in device manager.

This is officially a 'cry for help' :-(

Thanks!

PS: Has anyone else been able to do this kind of thing before?

Re: Triggering an event when specific string received via RS232

Posted: Fri Oct 09, 2009 11:33 am
by ste5442
Hi benj - if you can provide any assistance it would be appreciated!

Many thanks,

S

Re: Triggering an event when specific string received via RS232

Posted: Fri Oct 09, 2009 1:03 pm
by Benj
Hello

Sorry for the delay in replying.

In answer to your questions.

1) The flow control pins are allocated via the numbered jumper positions. Number 1 allocates the CTS pin to bit 5 and RTS pin to bit 0 of the port and number 2 allocates the CTS pin to bit 5 and RTS pin to bit 4 of the port. Number 3 allows any of the pins to be used via the wire patch system.

2) This article post demonstrates the default COM port settins for hyperterminal. YOu should not have to do anything to the device manager settings to allow this to work.

http://www.matrixmultimedia.com/mmforum ... =26&t=4793

3) Yes this was definetly working on my hardwarem though I will give it another go to confirm.

4) There is an order to get hyperterminal working with a USB but this is pretty basic. Pretty much connect the board and let the USB enumerate and then connect up hyperterminal. If the board happens to become unplugged then you will need to restart hyperterminal and reconnect the USB device.

5) I was typing the commands by hand.

As a troubleshooting aid do you have an LCD E-Block so you can print out and confirm the strings that the system is receiving from the PC.

Re: Triggering an event when specific string received via RS232

Posted: Fri Oct 09, 2009 1:31 pm
by ste5442
Thanks Benj - i did try printing the 'in_string' to the LCD and it did not look as expected. As i remember the first character was missing but i will need to confirm.
I do believe that this should be a simple task and I now understand your code - it just doesnt work for me! :-)
If you get a chance can you let me know what the settings need to be within device manager (when you double click the COM port) - i think i fudged them a bit out of desperation!
Also, my hyperterminal settings are set to 'autodetect' the encoding type. Should i set this to ANSI?

Sorry for the trouble but once cracked i can get moving again!

S.

Re: Triggering an event when specific string received via RS232

Posted: Fri Oct 09, 2009 1:39 pm
by Benj
Hello

Ok I had another go here using a 16F877A for troubleshooting purposes and I have managed to find the cause of the problem. This was that the string variable was not being terminated with a null byte or 0 wehen we had finished appending the incoming bytes to it. I have now added this to the program and it is now behaiving a lot better. Looks like your hardware was set up correctly all along.

Re: Triggering an event when specific string received via RS232

Posted: Fri Oct 09, 2009 1:41 pm
by Benj
Sorry that was my version for the 16F877A. Here is the updated 16F88 version.

Re: Triggering an event when specific string received via RS232

Posted: Fri Oct 09, 2009 1:45 pm
by ste5442
Benj, your a genius ;-)
I will give this a try later and send you a big sloppy kiss if this works out! Watch this space......

Re: Triggering an event when specific string received via RS232

Posted: Fri Oct 09, 2009 1:49 pm
by ste5442
Oh, before I get cracking with this - is there a chance it will not work with an 18F4520 or 4550?
I will of course try it on the 16F88 first.

S

Re: Triggering an event when specific string received via RS232

Posted: Fri Oct 09, 2009 1:57 pm
by Benj
Hello Steve

There is always a chance, but I think that any problems you do have with alternative processors will simply be hardware complications.

From the top of my head a 18F4550 should use the USB232 board on portc with the jumpers set to C and 1 or C and 2. The number does't really matter unless you want flow control.

Let me know if you have any problems and I will try not to leave you hanging again.

Re: Triggering an event when specific string received via RS232

Posted: Sat Oct 10, 2009 6:46 pm
by ste5442
Hi Benj....It works on the 16F88 very well but no joy with the 18F4520 which is my ideal part to use :-(

I will investigate further!

S

Re: Triggering an event when specific string received via RS232

Posted: Sun Oct 11, 2009 6:14 pm
by ste5442
Hi Benj,

I now have the code working for the majority of the time and now understand it. I have discovered some problems however - these are with Flowcode not being able to get the chip ID when the USB232 E-Block is connected to a USB port as well as the programmer. As soon as the USB232 USB lead is pulled from the USB port Flowcode can ID the chip - probably some kind of back powering thing going on. The RS232 code works fine with a 16F88 but does not work with an 18F4520 - something is different with the USART and I do not know what?

Many thanks for your help. I will post the two problems above as seperate topics.

Cheers!