Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 06:25 29 Nov 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : MM mono Serial Problems II

     Page 1 of 2    
Author Message
Nixie
Regular Member

Joined: 19/02/2013
Location: Australia
Posts: 66
Posted: 10:44pm 22 Mar 2014
Copy link to clipboard 
Print this post

I guess this is the sequel (Ha Ha) to Grogsters 'Serial Problems', which I have followed with interest. But can anyone assist with this steam-driven version of the code (Thanks TassyJim :-) for your original version.)
The Serial Comms work fine between to Mono MMs.
My problem is that I have added a couple of "Menu Options", of which only one will work when I first run the program. I've tried all sorts of things but to no avail.
A "menu selection" e.g. sending a "1" + <CR> only works the first time the program is run. Thanks, Nic.

100 Open "COM2:19200" As #1
110 Cls: Print "Ready to Rx..."
120 If Loc(#1)=0 Then 120
130 By$=Input$(1,#1)
140 Msg$=Msg$+By$

150 If Msg$="1"+Chr$(13) Then 400
155 If Msg$="2"+Chr$(13) Then 500

200 If By$=Chr$(13) Then Print "Rx: ";Msg$: Msg$=""
350 GoTo 120
' ----------------------------
400 Print "Menu selection 1"
405 Msg$=""
410 GoTo 120

500 Print "Menu Selection 2"
505 Msg$=""
510 GoTo 120
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 10:59pm 22 Mar 2014
Copy link to clipboard 
Print this post

How many bytes are you trying to receive?
Just the one?

If you are only sending ONE byte to loop to a menu option, then adding bytes to msg$ will cause you problems.

Perhaps:

By$=INPUT$(1,#1)
If By$="1" then....
If By$="2" then....

Edited by Grogster 2014-03-24
Smoke makes things work. When the smoke gets out, it stops!
 
Nixie
Regular Member

Joined: 19/02/2013
Location: Australia
Posts: 66
Posted: 11:24pm 22 Mar 2014
Copy link to clipboard 
Print this post

Hi Grogster,
I need to receive variable sizes of msgs; so a variety in the number of bytes.
A number "1" plus a <CR> ...isn't that two bytes?
I need to recognise the <CR> as the end of transmission from the other end.
Eventually I want to set this up to use two radios to transmit and receive instead of cable between two MMs.
Thanks, Nic
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 11:44pm 22 Mar 2014
Copy link to clipboard 
Print this post

Yes, a "1" plus a <cr> is two bytes.

Do you need the <cr> on the end of the string or not?

How about:

DO
BY$=INPUT$(1,#1)
If BY$=CHR$(13) THEN EXIT
MSG$=MSG$+BY$
LOOP

If MSG$="1" then....
If MSG$="2" then....


This will only input till it sees a <cr>, and the <cr> will NOT be added to MSG$
There is an easy way around that, if you need the <cr> on the end of MSG$.

In your orignal code, lines 150 and 155, you are asking MMBasic that if the wanted character PLUS a <cr> is true, then do whatever. As this is never true, this is the source of your problems.
Edited by Grogster 2014-03-24
Smoke makes things work. When the smoke gets out, it stops!
 
Nixie
Regular Member

Joined: 19/02/2013
Location: Australia
Posts: 66
Posted: 11:57pm 22 Mar 2014
Copy link to clipboard 
Print this post

Yes, I do need a <CR> at the end of the string.
I tried using LEN to determine the length of the string in msg$; and followed it with the MID$ .....x,1 to determine if the last byte was a chr$(13) (a <CR>)..... and that worked ok. But it still doesn't answer or cure the original problem.
Perplexed, Nic.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 12:03am 23 Mar 2014
Copy link to clipboard 
Print this post

OK, lets assume a serial input string of: "ABCDEF<cr>"

Using this code:

DO
BY$=INPUT$(1,#1)
MSG$=MSG$+BY$
LOOP until BY$=CHR$(13)


This will input bytes continuously from the serial port, and add them to MSG$, UNTIL the byte read from the serial port buffer is <cr>, at which point, the loop will exit.

Using the example above, when the loop exits, it will exit with MSG$="ABCDEF<cr>"

Any number of bytes can be in the buffer, and <cr> will be used as an "End-of-message" marker, and also added to the MSG$ string.

Generally speaking, <cr> is not needed as part of the processing routine, rather an end of message marker, as you have indicated, and I too use it this way. Having inputted the message itself, you can essentially discard the <cr> UNLESS you need it, because you are going to forward the message to something else, such as a terminal or other external device, which will usually also use <cr> as the end-of-message marker.Edited by Grogster 2014-03-24
Smoke makes things work. When the smoke gets out, it stops!
 
Nixie
Regular Member

Joined: 19/02/2013
Location: Australia
Posts: 66
Posted: 12:10am 23 Mar 2014
Copy link to clipboard 
Print this post

Ok. Thanks. I will go and try some things based on your suggestions. They are appreciated Grogster...especially on a Sunday night when I am feeling somewhat "tyre d and run-down" (hah de ha!)
Cheers, Nic.
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 12:14am 23 Mar 2014
Copy link to clipboard 
Print this post

  Nixie said   Ok. Thanks. I will go and try some things based on your suggestions. They are appreciated Grogster...especially on a Sunday night when I am feeling somewhat "tyre d and run-down" (hah de ha!)
Cheers, Nic.


Hi-de-hi! (ho-de-ho!)

You are welcome. I recently learnt this myself, so if I can help you in any way, I would feel happy that I have given back to the forums, which have given ME so much.

The code examples I have given you, are a prime example of why NO line numbers can REALLY help you.... I know you obviously like line numbers - so did I - but dropping them in favour of "Structured programming" really opens up much more flexibility in terms of whatever you are trying to do.

Honestly.....

Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 12:25am 23 Mar 2014
Copy link to clipboard 
Print this post

Perhaps you need a serial message generator?

I made one of these, and it makes testing so much easier, if you can generate "Fake" messages with which to test with...



This is based on a PICAXE 14M2, and you can program it to output any message you want, on any of the 8 buttons - I have the coupled to the Colour Maximite at the moment, and I can send it any one of 8 messages any time I press a button - also useful for sending multiple messages, and seeing how the MM serial buffer deals with it.(very well, actually!)

Smoke makes things work. When the smoke gets out, it stops!
 
Nixie
Regular Member

Joined: 19/02/2013
Location: Australia
Posts: 66
Posted: 11:32am 23 Mar 2014
Copy link to clipboard 
Print this post

The code examples I have given you, are a prime example of why NO line numbers can REALLY help you.... I know you obviously like line numbers - so did I - but dropping them in favour of "Structured programming" really opens up much more flexibility in terms of whatever you are trying to do.

Your point is well-made. I'm stuck in stem driven code, can you recommend any particular text book? You are right...I need to work with structured code I have let things slip somewhat!

As for your message machine, that is great! (It's time I also learnt how to PIC program) would you share how you built your test message generator?

Many thanks, Nic
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2817
Posted: 11:37am 23 Mar 2014
Copy link to clipboard 
Print this post

Hi Grogster,

Nice little gadget you have there although Geoff would be very disappointed that it isn't based on a MicroMite!

So is it your own design? If so I would like to include the concept in a collection of 'MicroMite projects' I am compiling.

Let me know . . .

Regards,

WW


For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 12:56pm 23 Mar 2014
Copy link to clipboard 
Print this post

  Nixie said   Your point is well-made. I'm stuck in stem driven code, can you recommend any particular text book? You are right...I need to work with structured code I have let things slip somewhat!

As for your message machine, that is great! (It's time I also learnt how to PIC program) would you share how you built your test message generator?

Many thanks, Nic


I am not trying to force you down the "No line numbers" path, but perhaps I could encourage you to walk with me and others down this path - we'll all help you if you trip on a rock or something!

I HATED the idea of dropping my much loved line numbers from the likes of Atari Basic, as I essentally learned BASIC on my old Atari 800XL back in the 80's, and it had line-numbers, and no structured loops etc, so I was loathe to leave it in favour of this new fangled line-less coding rubbish.

But my examples above are just one example of what you can do easily with a structured loop, which you can't do easily, with line numbers.
Smoke makes things work. When the smoke gets out, it stops!
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 01:01pm 23 Mar 2014
Copy link to clipboard 
Print this post

@ Nixie & WhiteWizzard & anyone else interested:

Yes, I designed and built the serial message generator myself a few years ago. I used PICAXE, as that was what I was into at the time. A MicroMite version would be easy to build though, and would be the chip of choice had I to build it again now. I would also replace all the throuh-hole parts with SMD - using the SOIC MicroMite chip, and 1206 SMD resistors etc.

I have OFTEN found, in my playing around with serial, that it is really handy, if you can just press a button to send a "Fake" message while you are trying to perfect your code. You could always use a laptop and serial program to do the same thing, but this is much smaller. I also have my laptop hooked to the other MM serial port, as when I press a button, the MM process the message, and then outputs a corresponding message to a pager transmitter on the other port, so at the moment, I am using the laptop and terminal software as a pager transmitter mock-up, to make sure the correct data is both going in and coming out.

I have been able to test my message system on the MM, by pressing ALL 8 buttons one after another in quick succession, to fill up the MM serial buffer, then watch what the MM and how my code deals with it - nice easy way to build a message queue, if you are testing message queuing, which I am. To say nothing of the fact that with a laptop instead, you have to type the message, having to just push a button is much quicker.

I can upload gerbers and PICAXE code, if anyone is interested in making their own one.
Edited by Grogster 2014-03-24
Smoke makes things work. When the smoke gets out, it stops!
 
Nixie
Regular Member

Joined: 19/02/2013
Location: Australia
Posts: 66
Posted: 11:49pm 23 Mar 2014
Copy link to clipboard 
Print this post

ARRRGAH! still can't get it to work. Same result as my line number code above....Grrrr!

Open "COM2:19200" As #1
Do
Do
By$=Input$(1,#1)
If By$=Chr$(13) Then EXIT
MSG$=MSG$+By$
Loop
If MSG$="1" Then Print" Choice 1"
If MSG$="2" Then Print "Choice 2"
Print MSG$
MSG$=""
Loop

Have I got the code above ok please?
 
Nixie
Regular Member

Joined: 19/02/2013
Location: Australia
Posts: 66
Posted: 11:51pm 23 Mar 2014
Copy link to clipboard 
Print this post

Sorry seems to have lost the formatting on posting the request....gulp!
Thanks, Nic.
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2817
Posted: 12:23am 24 Mar 2014
Copy link to clipboard 
Print this post

Hi Nic,

A few suggestions to try:

Try placing a Print MSG$ immediately after your first 'Loop' command (and before your 'If MSG$="1"' parser).
Then see what is printed when you run the program.
You may find there is a CR & LF character being transmitted and the LF character is throwing things out.

Also, is the first character (or two) of each message always a numeric digit(s)? If so then you could use either LEFT$ or VAL command to extract number (the latter would strip any 'strange' preceding characters.

Try the ASC(string$) command which should return the first character's ASCII code of the message. If this isn't what you expect then you should be given a big clue as to what your issue is.

If possible, and just for process of elimination, reduce baud rate to as slow a speed as possible both ends. See if this helps just in case its a loop execution timing issue.

Let us know how you get on . . .

Regards,

Phil


For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
Nixie
Regular Member

Joined: 19/02/2013
Location: Australia
Posts: 66
Posted: 12:52am 24 Mar 2014
Copy link to clipboard 
Print this post

Hi WW,
Thanks for your suggestions. I will try things tomorrow and get back then...

Good point about the LF, I must revisit the LEN and MID$ commands. II think you could be correct...will try the ASC 'test' too!

I have already tried a speed of 300, no changes. (I'm rather tired today, I live on a remote cattle property in Western Victoria and cut a couple of tons of firewood ready for winter....zzzzz)

Actually I thunk YOU ARE CORRECT ABOUT THE LF....thinking more about it I noticed an anomaly when I tried some test runs communicating between the two MMs; and noticed the spacing between lines increased after the first msg rxd! So pretty sure this is the case....Thank you!
Nic
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2817
Posted: 02:13am 24 Mar 2014
Copy link to clipboard 
Print this post

Nic,

From what you say about the line spacing in your tests then I am 99.999% certain the LF is your issue.

Simply check for Chr$(10) to detect LF and take necessary action.

There are many choices of what action to take so up to you how you code this: i.e. ignore LF by jumping to start of first loop whenever Chr$(10) is detected.

Let me know if you still have problems . . .

(What temperature is your so-called winter?)


For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
BobD

Guru

Joined: 07/12/2011
Location: Australia
Posts: 935
Posted: 07:22am 24 Mar 2014
Copy link to clipboard 
Print this post

  WhiteWizzard said   (What temperature is your so-called winter?)


Phil,
Australia is a large country and has a large range of climate variation. I live in a similar area to Nic although I could be as much as 450 km from him. You can use this site Weatherzone to find weather and climate in OZ. Statistics and climate are down the bottom of the page once you select a location. If you want to see graphical then click the Full Climatology link in the stats heading.

We often hit zero Celsius in winter but it can also be much warmer. 18 is not unknown for a winter maximum. We never have snow. Our maximum peaked out around 46 degrees this summer. See more detail here. Pick a town or select Climate on the dropdown menu for a town. I'm in one of these. Nic is probably in this area but could be north or south of it.

This site will give you some free info but they like free registration (they're harmless).
Bob
 
Grogster

Admin Group

Joined: 31/12/2012
Location: New Zealand
Posts: 9308
Posted: 11:27am 24 Mar 2014
Copy link to clipboard 
Print this post

Welcome to the wonderful new world of line-less code!
It WILL come to make sense to you with practise - honestly.
Keep at it, and we're all here to help you on your journey.

With structured programming, it is a good idea to indent each loop or routine thus:


Open "COM2:19200" As #1
Do
Do
By$=Input$(1,#1)
If By$=Chr$(13) Then EXIT
MSG$=MSG$+By$
Loop
If MSG$="1" Then Print" Choice 1"
If MSG$="2" Then Print "Choice 2"
Print MSG$
MSG$=""
Loop


This just makes it MUCH easier to read. If you happen to be using Jim's fantastic MMEdit, you will find all your code colourized and each time you use TAB to indent the loops etc, a little dotted vertical line will be drawn on the screen linking the top and bottom of the code - very cute, and makes it so easy to see exactly where loops start and stop.

Try this code as a test:


Open "COM2:19200" As #1
Do
Do
By$=Input$(1,#1)
MSG$=MSG$+By$
Loop until By$=CHR$(13) 'Change this to 10 if you think LF is the character you need
Print MSG$,LEN(MSG$)
If MSG$="1" + CHR$(??) Then Print" Choice 1" 'Change ?? for 13 or 10 for <cr> or <lf>
If MSG$="2" + CHR$(??) Then Print "Choice 2" 'Same as above...
MSG$=""
Loop


This code incorporates WW's idea of printing MSG$ BEFORE subjecting it to the testing - an exceptionally good idea, and one I use often for debugging purposes - you can remove that line, once things are working the way you want.

I also changed your input loop to LOOP UNTIL, as we talked about, as mentioned you wanted <cr> (perhaps now a <lf>?) on the end of your string. But if you don't actually need it that way, it is easier to ignore the <cr> or <lf> when you are doing your IF/THEN.

Keep at it - you are doing fine!Edited by Grogster 2014-03-25
Smoke makes things work. When the smoke gets out, it stops!
 
     Page 1 of 2    
Print this page
© JAQ Software 2024