Guide to Serin, Serout, bytes and words

rod mcmahon

New Member
Hi Guys
I am having a problem with the fundamental understanding of serial communications, bytes, words and serin/read/write/serout.
So as I see it:-
serin 7,N2400,(“Go”) is really a conditional statement, the code will not jump to the next line unless it receives “Go” on the serial input pin, there is no storing of the variable to a memory location.
If I look at the example in the Basic help file in the programmer
Main: for b0 = 1 to 63
serin 6, N2400,bi
Write b0,b1 ‘write value into b1
Next b0
If we look at the write statement then the explanation is given as Write, location, data, data
Then the above write statement says that read variable b1 is placed into the byte place holder b0 = 1 to 63 depending on loop number. If I am correct then the comment to the above write statement is wrong. It should say ‘write b1 value to b0 position’. Yes or No?

Now lets look at bytes and words. Say I can transmit optional words say red, yellow, blue from one picaxe to the other. How do I send and how do I receive. I can send serout, 6,N2400,”RED” etc but how do I receive this? Do I have to send each letter separately and recombine at the receiving end ? How would I do this?

How many bytes is a letter? How many bytes is a word. I see that I can use the write statement using a word variable Write location, data, data WORD wordvariable, but I do not understand how to address this to the serial input and if I use it how many bytes are used. If using a picaxe 08 then this becomes critical methinks.
I need a toddlers guide.
 

Dippy

Moderator
You don't need a toddler's guide, but you need to:-
A) Get back to some basics (no pun intended).
B) Get each issue sorted in turn and try not to confuse things.

"serin 7,N2400,(“Go”) is really a conditional statement, the code will not jump to the next line unless it receives “Go” on the serial input pin, there is no storing of the variable to a memory location."
- yes , so a bit silly if you want to pick up data, yes?

From the MANUAL:-
Qualifiers are used to specify a ‘marker’ byte or sequence. The command
serin 1,N2400,(“ABC”),b1
requires to receive the string “ABC” before the next byte read is put into byte b1
Without qualifiers
serin 1,N2400,b1
the first byte received will be put into b1 regardless.

"If I look at the example in the Basic help file in the programmer
Main: for b0 = 1 to 63
serin 6, N2400,bi
Write b0,b1 ‘write value into b1
Next b0"

- I think the comment should be moved to the previous line.
The Write line should have a comment like "write b1 to location b0 in EEPROM"
- you've won a coconut!!

For serout, 6,N2400,”RED”
your serial in could be Serin <pin>,<baud> , b0,b1,b2
But, beware.
If you wish to make a general statement like:-
Serin <pin>,<baud> , b0,b1,b2,b3,b4,b5 on your receiving end then you must MAKE sure that th transmitting end 'pads' any output to 6 bytes or else it'll get stuck.
E.g. serout, 6,N2400,”REDddd”

Remember, on the RECEIVING PICAXE the Serin will ge stuck until it receives the stated number of bytes (or characters). Some people call it a 'blocking' command.

Or on some PICAXES the ability to read to scratchpad is FAR better.

Each character is a byte. "RED" is 3 bytes. And is the same as 82,69,68 in ASCII.
serout, 6,N2400,”RED” = serout, 6,N2400,82,69,68

A variable type Word is 2 bytes. Have you read about maximum sized numbers in variable type? Have you read about bits?

Once hippy has arisen from the coffin, he can give you a nice tutorial. Sadly, I have my own work to do now. If there any typoes above blame IE7.
 

hippy

Ex-Staff (retired)
:)

Dippy covered most of it. How best to send multiple letters ? The answer is; best not to. Send a single byte which represents what you want to send. It's not easy to deal with variable length communications with PICAXE serial.

Code:
' Transmit
Symbol NONE  = "N"
Symbol RED   = "R"
Symbol GREEN = "G"
Symbol BLUE  = "B"
Do
  b0 = NONE
  If pin1 = 1 Then : b0 = RED   : End If
  If pin2 = 1 Then : b0 = GREEN : End If
  If Pin3 = 1 Then : b0 = BLUE  : End If
  SerOut TX_PIN, TX_BAUD( "COLOUR", b0 )
  Pause 500
Loop
Code:
' Receive
Symbol NONE = "N"
Symbol RED = "R"
Symbol GREEN = "G"
Symbol BLUE = "B"
Do
  SerIn RX_PIN, RX_BAUD, ( "COLOUR" ), b0
  Select Case b0
    Case NONE  : SerTxd( "None",CR,LF )
    Case RED   : SerTxd( "Red",CR,LF )
    Case GREEN : SerTxd( "Green",CR,LF )
    Case BLUE  : SerTxd( "Blue",CR,LF )
    Case Else   : SerTxd( "Unknown",CR,LF )
  End Select
Loop
 
Top