Serin Help?

ChedderCheeser

Senior Member
Hi all, i am making a little project to learn how to use serin/out to make 2 or more picaxes communicate. Heres the reciever code:

Code:
init:
	setfreq m4
goto main


main:
	do
		serin 1,N2400,("ON", "OFF"),b1
		
		if b1 = "ON" Then
			high 4
		Else If b1 = "OFF" Then
			low 4
		End If	
	
	loop	
end
How can i like.. make this actually work lol? I tried... and i looked in the manual. I know the transmitter works.. here it is:

Code:
init:
	setfreq m4
goto main


main:
	do
		serout 0, N2400, ("ON")
		pause 500
		serout 0, N2400, ("OFF")
		pause 500
	loop	
end
what to do?
 
Last edited:

Jamster

Senior Member
From the transmitter you are sending "ON" and "OFF" but the receiver is waiting for "ON", "OFF" and then a value to fill b1, therefore the program will be still waiting at the serin.
 

lbenson

Senior Member
Also note that b1 can store a 1-byte value (0-255). "ON" is two bytes and "OFF" is three, so those values can never be equal to b1. Have another read of the manual, and check out some of the many threads bearing on your question.
 

Grogster

Senior Member
Binary notation of 1=on and 0=off would make your life much easier to code.

Also note lbenson's comment about b1...

Example: (NOT tested, but should work OK)

RECEIVER: (you don't need to setfreq m4 if that is what you are using, as they are 4MHz by default. You also don't need a goto main, when it is the next line of code anyway, so...)

Code:
main:
	do
		serin 1,N2400,("***"),b1 'Listen for qualifier, then input the ON/OFF byte
		
		if b1 = 1 Then
			high 4
		    Else
			low 4
		End If	
	
	loop	
end
TRANSMITTER CODE

Code:
main:
	do
                b1=1 'Set ON/OFF byte to ON
		serout 0, N2400, ("***",b1) 'Send qualifier and "ON" byte(binary 1)
		pause 500
                b1=0 'Set ON/OFF byte to OFF
		serout 0, N2400, ("***",b1) 'Send qualifier and "OFF" byte(binary 0)
		pause 500
	loop	
end
For simple ON/OFF stuff like this, I prefer this approach, as to turn something ON, the transmitter must be exact, whereas to turn it off, you can send any byte you like - it will not be equal to 1, so the code in the receiver will turn OFF when it receives anything other then a 1 for b1.

I can change that so you must be exact for the OFF command too, but you probably get the idea by now anyway... ;)
 
Last edited:

ChedderCheeser

Senior Member
Binary notation of 1=on and 0=off would make your life much easier to code.

Also note lbenson's comment about b1...

Example: (NOT tested, but should work OK)

RECEIVER: (you don't need to setfreq m4 if that is what you are using, as they are 4MHz by default. You also don't need a goto main, when it is the next line of code anyway, so...)

Code:
main:
	do
		serin 1,N2400,("***"),b1 'Listen for qualifier, then input the ON/OFF byte
		
		if b1 = 1 Then
			high 4
		    Else
			low 4
		End If	
	
	loop	
end
TRANSMITTER CODE

Code:
main:
	do
                b1=1 'Set ON/OFF byte to ON
		serout 0, N2400, ("***",b1) 'Send qualifier and "ON" byte(binary 1)
		pause 500
                b1=0 'Set ON/OFF byte to OFF
		serout 0, N2400, ("***",b1) 'Send qualifier and "OFF" byte(binary 0)
		pause 500
	loop	
end
For simple ON/OFF stuff like this, I prefer this approach, as to turn something ON, the transmitter must be exact, whereas to turn it off, you can send any byte you like - it will not be equal to 1, so the code in the receiver will turn OFF when it receives anything other then a 1 for b1.

I can change that so you must be exact for the OFF command too, but you probably get the idea by now anyway... ;)
So, can i use this for all kinds of communication? What if i need to send a string?
 

Grogster

Senior Member
Define "All types of communication"...
That could be anything, so the answer to that is probably not.
You need to decide what exactly you want to send, then work out a method from there.
If you are just wanting a way to exchange a few bytes from one chip to the other, then this is a perfectly acceptable method.

The above code example would be no good at all, for talking to an I2C device, for example - everything is relative. I have heard of people using IRIN and IROUT to directly connect PICAXE's together, but I still prefer plain old serial - that is a personal choice.

If the string is known, you can send something like serout 0,N2400,("***ON") including the qualifier, so yes, you can, but there is more code you have to write for the receiver.
You could then input the above with the likes of serin 1,N2400,("***"),b1,b2

The KISS approach always seems to work well for me most of the time! :D

REMEMBER - best to get it going simply FIRST - don't jump too far ahead, until you have the simple one going correctly, THEN expand on it. :)

With the above, you have to analyze b1 and b2 to find out if those bytes are actually what you want:

if b1="O" and b2="N" then switch_on

Using your three-byte ON/OFF concept, you have to ADD a byte to ON, so that you are always sending 3 bytes - example uses a dash:

serout 0,N2400,("***-ON") or serout 0,N2400,("***OFF")

then input it with:

serin 1,N2400,("***"),b1,b2,b3

and process it with:

if b2="O" and b3="N" then switch_on
if b1="O" and b2="F" and b3="F" then switch_off

With respect to ON, we just ignore b1(which is a dash) as we don't need it, but you DO have to send it so that -ON and OFF are both 3-bytes to make everything jive.
 
Last edited:

ChedderCheeser

Senior Member
Define "All types of communication"...
That could be anything, so the answer to that is probably not.
You need to decide what exactly you want to send, then work out a method from there.
If you are just wanting a way to exchange a few bytes from one chip to the other, then this is a perfectly acceptable method.

The above code example would be no good at all, for talking to an I2C device, for example - everything is relative. I have heard of people using IRIN and IROUT to directly connect PICAXE's together, but I still prefer plain old serial - that is a personal choice.

If the string is known, you can send something like serout 0,N2400,("***ON") including the qualifier, so yes, you can, but there is more code you have to write for the receiver.
You could then input the above with the likes of serin 1,N2400,("***"),b1,b2

The KISS approach always seems to work well for me most of the time! :D

REMEMBER - best to get it going simply FIRST - don't jump too far ahead, until you have the simple one going correctly, THEN expand on it. :)

With the above, you have to analyze b1 and b2 to find out if those bytes are actually what you want:

if b1="O" and b2="N" then switch_on

Using your three-byte ON/OFF concept, you have to ADD a byte to ON, so that you are always sending 3 bytes - example uses a dash:

serout 0,N2400,("***-ON") or serout 0,N2400,("***OFF")

then input it with:

serin 1,N2400,("***"),b1,b2,b3

and process it with:

if b2="O" and b3="N" then switch_on
if b1="O" and b2="F" and b3="F" then switch_off

With respect to ON, we just ignore b1(which is a dash) as we don't need it, but you DO have to send it so that -ON and OFF are both 3-bytes to make everything jive.
Thanks for that! It helps! Also.. this is probably the stupidest thing i have EVER said, but could you have a look at my Serial Graphic LCD Thread? I really need to figure that one out. That way, I can start my Picaxe Pc Project. :D
 

Grogster

Senior Member
I did look at that thread.
I'm afraid I don't know much about the graphical LCD you linked to, but I will have a wee look and get back to you...
 

westaust55

Moderator
could you have a look at my Serial Graphic LCD Thread? I really need to figure that one out. That way, I can start my Picaxe Pc Project. :D
@ Cheddar,

Maybe blunt, but you are not actually figuring any of these out for yourself. :eek:
There is a continual stream of questions and thread "bumps" seeking others to solve your problems.

On a more productive note and following on from what others have already posted,

For your transmission, you are sending fist a 2 bytes string "ON" followed by a three byte string "OFF" but not other following byte variable values.

For the receiver code, you are looking for a qualifier as "ON","OFF" and then a byte value which is to be placed in byte variable b0.

Firstly a qualifer which is the part within the brackets () of a serin command are characters to be received as a "flag" to indicate that the desired values follow.
Read page 205 in PICAXE manual 2 (currently V7.7)

Hence after the qualifiers are received your SERIN waits for a further byte to be received which will possibly be the "O" from "ON" on the next pass of the loop.

Then you try to test a multi-byte string with a one-byte variable (b0)

a byte variable can only hold one value from 0 to 255 which may equate to a single character such as "F".

That is why you will get a syntax error when you enter the receiver code into the PE and perform a test.
 
Last edited:
Top