|
Forum Index : Microcontroller and PC projects : SerialRX Cfunction
| Author | Message | ||||
| viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
Working on a project and ran out of com ports on a '170. One 5v uController sending serial data to the MicroMite at 19200 baud on pin 15. SerialRX Cfunction to the rescue, but I had a hair loosing problem, and then a question of use. Here is my test code... CPU 48 Zz = 0 do do r = SerialRx(15, 19200, Reply$, 500, 20, chr$(13)) loop while r <> 3 If Reply$ = ("up" + chr$(13)) then Zz = Zz + 1 else if Reply$ = ("down" + chr$(13)) then Zz = Zz - 1 End if Print "Zz = ";Zz loop CFunction SerialRx 00000008 40024800 00442021 40024800 0044102b 1440fffd 00000000 03e00008 00000000 27bdffb8 afb00020 3c109d00 8e020000 afb20028 8cb20000 8c420000 00129040 0052001b 024001f4 afb40030 3c141062 26944dd3 8ce70000 8e030010 afb10024 00808821 8c840000 afbf0044 afb7003c afb60038 afb50034 afb3002c afbe0040 00c0a821 24050002 00003021 8fb60058 8fb3005c 00009012 00540019 2652fffb 00001010 000211c2 0060f809 7047a002 8e240000 8e020024 0040f809 00002821 0040b821 8e020028 0040f809 8e240000 afa20010 00001021 40824800 8fa20010 241e0001 00121842 005ef004 afa30018 afa00014 8ee20000 005e1024 50400014 8fa40018 40024800 0054102b 1440fff9 8fbf0044 24040001 00002821 00801021 00a01821 8fbe0040 8fb7003c 8fb60038 8fb50034 8fb40030 8fb3002c 8fb20028 8fb10024 8fb00020 03e00008 27bd0048 0411FFAB 00000000 8ee20000 03c21024 1440ffe4 00008021 00008821 0411FFA4 02402021 8ee20000 8fa30010 00621006 30420001 02021004 02228825 26100001 24020008 1602fff5 323100ff 02402021 0411FF97 00000000 8ee20000 03c21024 1040ffd0 8fa30014 24630001 02a31021 afa30014 a0510000 12c00005 a2a30000 8ec20000 0062102b 10400017 24040002 12600011 8fa30014 82650000 18a0000f 286200ff 82620001 12220011 00001021 10000005 24420001 80830001 1071000d 24040003 24420001 0045182a 1460fffa 02622021 8fa30014 286200ff 1440ffb1 00000000 24040002 10000003 00002821 24040003 00002821 8fbf0044 00801021 00a01821 8fbe0040 8fb7003c 8fb60038 8fb50034 8fb40030 8fb3002c 8fb20028 8fb10024 8fb00020 03e00008 27bd0048 End CFunction I kept getting this error... [6] r = SerialRx(15, 19200, Reply$, 500, 20, Chr$(13)) Error: Cannot find REPLY Just for the sake of it, I added Reply$ = "" to the beginning of the code and it finally worked... CPU 48 Zz = 0 Reply$ = "" '<<<<<<<< ADD THIS, AND IT WORKS! do do r = SerialRx(15, 19200, Reply$, 500, 20, chr$(13)) loop while r <> 3 If Reply$ = ("up" + chr$(13)) then Zz = Zz + 1 else if Reply$ = ("down" + chr$(13)) then Zz = Zz - 1 End if Print "Zz = ";Zz loop CFunction SerialRx 00000008 40024800 00442021 40024800 0044102b 1440fffd 00000000 03e00008 00000000 27bdffb8 afb00020 3c109d00 8e020000 afb20028 8cb20000 8c420000 00129040 0052001b 024001f4 afb40030 3c141062 26944dd3 8ce70000 8e030010 afb10024 00808821 8c840000 afbf0044 afb7003c afb60038 afb50034 afb3002c afbe0040 00c0a821 24050002 00003021 8fb60058 8fb3005c 00009012 00540019 2652fffb 00001010 000211c2 0060f809 7047a002 8e240000 8e020024 0040f809 00002821 0040b821 8e020028 0040f809 8e240000 afa20010 00001021 40824800 8fa20010 241e0001 00121842 005ef004 afa30018 afa00014 8ee20000 005e1024 50400014 8fa40018 40024800 0054102b 1440fff9 8fbf0044 24040001 00002821 00801021 00a01821 8fbe0040 8fb7003c 8fb60038 8fb50034 8fb40030 8fb3002c 8fb20028 8fb10024 8fb00020 03e00008 27bd0048 0411FFAB 00000000 8ee20000 03c21024 1440ffe4 00008021 00008821 0411FFA4 02402021 8ee20000 8fa30010 00621006 30420001 02021004 02228825 26100001 24020008 1602fff5 323100ff 02402021 0411FF97 00000000 8ee20000 03c21024 1040ffd0 8fa30014 24630001 02a31021 afa30014 a0510000 12c00005 a2a30000 8ec20000 0062102b 10400017 24040002 12600011 8fa30014 82650000 18a0000f 286200ff 82620001 12220011 00001021 10000005 24420001 80830001 1071000d 24040003 24420001 0045182a 1460fffa 02622021 8fa30014 286200ff 1440ffb1 00000000 24040002 10000003 00002821 24040003 00002821 8fbf0044 00801021 00a01821 8fbe0040 8fb7003c 8fb60038 8fb50034 8fb40030 8fb3002c 8fb20028 8fb10024 8fb00020 03e00008 27bd0048 End CFunction I don't know if this is a bug or not. It seems that you have to "initialize" the string prior to using the CFunction. Also didn't try to DIM it first. If this is how it needs to work, then it would help to put this in the RxSerial Cfunction PDF for future reference and greater hair retention. I could also be doing something completely wrong with the usage, not sure... Also, the PDF says... "Can terminate the receive function based on the number of characters received, a timeout or ONE OR MORE TERMINATING CHARACTERS." In the code above, I am using a chr$(13) to terminate the function and that works very well resulting in r = 3 to terminate the loop, however, I would like to use 2 or more different characters added to the end of the received string to terminate the function. What is the RxSerial syntax to use more than one terminating characters? |
||||
| disco4now Guru Joined: 18/12/2014 Location: AustraliaPosts: 1044 |
looks like the last parameter is a string and all characters in it will be tested. So just concatenate the various last characters in to a string. eg. CHR$(13)+":"+";" etc Parameters The SerialTx function (created by adding the above code) takes four to six parameters: r = SerialTx( pin, baud-rate, string$, timeout, maxchars, termchars) Where p in is the input I/O pin's number baud-rate is the desired receive speed string$ is the string where the received characters will be stored. timeout is the maximum time (in milliseconds) to wait before returning with whatever has been received. maxchars is the number of characters to wait for (optional) termchars is a string and capture will be terminated if a character received matches any character in the string (optional). F4 H7FotSF4xGT |
||||
Grogster![]() Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9753 |
I encourage you to use OPTION EXPLICIT in your code or any experiments. It will catch an error like this. ![]() The reason Reply$="" works, is that without 'option explicit' in your code, the interpreter automatically creates a string with that name for you when it runs that line. If you used OPTION EXPLICIT in your code and did NOT have the Reply="" line, the code would fall over when you try to call the SerialRX routine, with a message about Reply$ not being defined. ![]() EDIT: Error: Cannot find REPLY sounds like an error coming from the Cfunction wanting to put data into a string, but there is no such string in existence(it cannot find it) so it just stops at that point. [Quote=Jim]Also, the PDF says... "Can terminate the receive function based on the number of characters received, a timeout or ONE OR MORE TERMINATING CHARACTERS." In the code above, I am using a chr$(13) to terminate the function and that works very well resulting in r = 3 to terminate the loop, however, I would like to use 2 or more different characters added to the end of the received string to terminate the function. What is the RxSerial syntax to use more than one terminating characters?[/Quote] I don't think you can. The optional terminate characters can be one or more, but the function will test for ANY one of those characters to terminate. It won't try to match up a pattern of bytes. So if your termchars string was "!@#$", then the function will terminate if the next byte received is ! or @ or # or $, not a combination of those. I might be wrong. Geoff or Peter will correct me if they read this. Smoke makes things work. When the smoke gets out, it stops! |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4133 |
It's a bug! Using Reply$ without first declaring or defining it ought to be OK, because its first use should mean it gets auto-declared (with no OPTION EXPLICIT). However, apparently it isn't. Maybe it's a bug not worth fixing? John |
||||
| viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
It may not warrant fixing, as it does work very well. Just thought it should be mentioned in the PDF so that a new user of the function won't have to go crazy later when he can't get it to work properly. I kept thinking I was using it wrong and spent a good amount of time on it, almost giving up, but really had no choice as I needed a way to add a serial connection. |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
This behaviour is not really a bug because it was designed to act this way. My original thinking was that because data is often passed to/from a CFunction via variables it would not be a good idea to let the variable be automatically created. But I don't think that I thought it fully through. As viscomjim showed, it is logical to expect this and what harm could it do anyway? I will experiment with removing the error generating code and if it looks OK remove it permanently for the next release. Geoff Geoff Graham - http://geoffg.net |
||||
| viscomjim Guru Joined: 08/01/2014 Location: United StatesPosts: 925 |
Thanks for the answers. Geoff, what is the final verdict on the terminating characters? Can this be a concatenated string like Disco suggests, where all of the string (in order) has to be received, or is it ANY ONE of several terminating characters listed (and not sure how they would be listed) like Grogster suggests? BTW, the serial TX and RX are heaven sent. I have a project that uses both COM1 and COM2, but need extra ports for serial peripherals. The Cfunctions make life grand in my little world. A big thanks!!!! |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4133 |
It looks definitely as the doc and grogs say - i.e. any of the chars, not a sequence of them. If you want a sequence you could program it with a bit of extra logic. John |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
Yes, it is any one of the terminating characters... so Grogs is correct. I suppose you could rewrite the CFunction to look for a sequence of characters but SerialRx was designed to provide a simple solution. If you want to get sophisticated it would be much better to use COM 1 & 2 which are buffered. Geoff Graham - http://geoffg.net |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |