Name:
hserin
Syntax:

For X2 parts

HSERIN spaddress, count {,(qualifier)}

HSERIN [timeout, address], spaddress, count {,(qualifier)}

Qualifier - is an optional single variable/constant (0-255) which must bereceived before subsequent bytes can be received and stored in scratchpad

Spaddress - is the first scratchpad address where bytes are to be received

Count - is the number of bytes to receive

Timeout - is an optional variables/constants which sets the timeout period inmilliseconds

Address - is a label which specifies where to go if a timeout occurs.

For M2 parts

HSERIN var

Var - is a variable to receive the data byte.

Description:

Serial input via the hardware serial input pin (format 8 data, no parity, 1 stop).

The hserin command is used to receive serial data from the fixed hardware serial input pin of the microcontroller. It cannot generally be used with the serial download input pin - use the serrxd command in this case.

Baud rate is defined by the hsersetup command, which must be issued before this command can be used.

Users familiar with the serin command will note the hserin command has a completely different format. This is because the hserin command supports much higher baud rates than serin, and so is unable to process received bytes 'on the fly' (e.g. by changing ASCII into binary, as with the serin # prefix), as there is insufficient time for this processing to occur before the next hserin byte is received (at high baud rates). Therefore the raw data is simply saved in the memory and the user program must then process the raw data when all the bytes have been received.

Note that on X2 parts you may prefer to background receive the serial data into the scratchpad (hence not requiring use of this command at all) - see the hsersetup command for more details (hserin only accepts data when the command is being processed - background receive accepts data all the time).

On M2 parts the hserin command is used to transfer background received bytes into a variable. Up to two bytes can be 'background received' at any time during the PICAXE program (not just when the hserin command is processing) and are temporarily stored in a 2 deep FIFO buffer. Any more than two bytes are lost. Therefore on M2 parts the hserin command is non-blocking, it always processes immediately. If there is received data in the internal buffer the first byte is copied into the variable, if not the variable is left unaltered and the program continues on the next line. If two bytes are expected in the buffer it is necessary to use two separate hserin commands to retrieve both bytes.

Applies To:
08M2, 14M2, 18M2, 20M2, 28X1, 28X2, 40X1, 40X2
See Also:
Related Create:
Share:
Print:

Example for X2 parts

Echo 4 serial bytes at a time at 19200 baud

Code Example:
	hsersetup B19200_16, %00			; baud 19200 at 16MHz

main:	hserin [1000,main],0,4				; receive 4 bytes into sp
	ptr = 0						; reset sp pointer
	hserout 0,(@ptrinc,@ptrinc,@ptrinc,@ptr)	; echo out
	goto main					; loop
Copy Code Submit an Example

Example for M2 parts

Echo 1 serial byte at a time at 9600 baud

Code Example:
	hsersetup B9600_4, %00	; baud 9600 at 4MHz

main:	w1 = $FFFF		; set up a non-valid value
	hserin w1		; receive 1 byte into w1
	if w1 <> $FFFF then	; if a byte was received
	  hserout 0,(w1)	; echo it back out
	end if
	goto main		; loop
Copy Code Submit an Example

Submit Your Own Code!

You must be logged in to submit code examples. Login now.

Comments

  • Posted by thomasfoltz

    I have noticed the example above shows a word value used for hserin. Doesn't the M2 version of hserin only work with byte values?

  • Posted by hippy

    In Example 2; the word variable is used to receive a byte value, with its most significant byte being used to indicate when a byte has been received.

  • Login to leave a comment.