serint1: 
	clr	ES1    		        ; Disable further serial interrupts on UART2.
        push    ar2
	push    psw
	push    acc
	push    b
	push    DPL
	push    DPH
	clr	RI1
#ifdef SERINT_MONITOR
	mov     DPTR,#stop_drive+serintmon_pin
	mov     a,#1
	movx    @DPTR,a                 ; Output to ON coil every time we get an interrupt.
#endif  // SERINT_MONITOR
#ifdef LOOP_BACK
#ifndef LOOPBACK_ALWAYS
        mov	DPTR,#keyboard+loopback_enable_pin
        movx    a,@DPTR
        anl     a,#loopback_enable_bit
        jz      ser1_1a                 ; If loop-back enable pin is high,
#endif  // LOOPBACK_ALWAYS
        mov     a,SBUF1                 ; read the byte,
	setb	tr1_enab                ; turn on driver,
	mov     r2,#0
	djnz    r2,$                    ; Wait for 1/12us x 4 per cycle x 2 cycles x 256 = 0.17ms
	mov	SBUF1,a                 ; re-transmit the byte,
	jnb	TI1,$                   ; wait for transmit complete bit (no timeout),
	clr	TI1
	mov     r2,#0
	djnz    r2,$                    ; Wait for 1/12us x 4 per cycle x 2 cycles x 256 = 0.17ms
        jmp     ser1_1                  ; and do nothing else.
ser1_1a:
#endif  // LOOP_BACK
	mov	a,SBUF1
        jb      transmit_flag,ser1_1    ; Ignore further characters while transmit flag is set (i.e. until the 
	                                ; data - either remote or main - has been successfuly transmitted to the organ and acknowledged).
	                                ; This is simply because interrupting the send would cause a long delay which would 
	                                ; probably cause the receiving organ to time out. 
	cjne	a,#0FCh,ser1_1
	mov	DPTR,#keyboard2
	jmp	ser1_2
ser1_1:	
	clr	tr1_enab                ; Anything other than start marker, turn off driver.
	pop     DPH
	pop     DPL
	pop     b
	pop     acc
        pop     psw
	pop     ar2
	setb	ES1                     ; Re-enable interrupt.
	reti
ser1_2:                                 ; FCh (start marker).
	jnb	RI1,$                   ; Wait for byte (no timeout).
	clr	RI1
	mov	a,SBUF1
;	cjne	a,#0FAh,byte_update
	cjne	a,#0FAh,ser1_4          ; RSB100527 This change is because the code triggered by FBh seems to be unused, and dangerous.
	jmp	ser1_5
byte_update:
	cjne	a,#0FBh,ser1_4
	jmp     ser1_6
ser1_4:	call	send_error1
	jmp	ser1_1
ser1_6:
	mov	r2,#notes_64            ; FBh
again:
	jnb	RI1,$
	clr	RI1
	mov	a,SBUF1
	movx	@DPTR,a
	inc	DPTR
	djnz	r2,again
ser1_5:                                 ; FAh (byte marker).
	jnb	RI1,$                   ; Wait for byte (no timeout).
	clr	RI1
	mov	a,SBUF1
	cjne	a,#0FDh,ser1_7
	jmp	ser1_8
ser1_7:
	anl	a,#3fh
	mov	DPL,a
	jnb	RI1,$                   ; Wait for byte (no timeout).
	clr	RI1
	mov	a,SBUF1
	movx	@DPTR,a                 ; Save the received byte in the correct location.

	jmp	ser1_5                  ; Keep reading data until we see a checksum marker.
ser1_8:                                 ; FDh (checksum marker).
	jnb	RI1,$
	clr	RI1
	mov	a,SBUF1
	mov	b,a
	jnb	RI1,$
	clr	RI1                     ; Receive end_marker from remote console.
	mov	a,SBUF1
	cjne	a,#0FEh,ser1_9
	jmp     ser1_10
ser1_9:
	call	send_error1
	jmp	ser1_1
ser1_10:
	xcall	check_sum
	cjne	a,b,ser1_9
	call	send_okay1
	setb    transmit2_flag          ; This informs main loop to ignore local data and transmit keyboard2 array.
;	mov	p1,#receive_on          ; Turn off RS485 driver.
	clr     tr_enab                 ; Turn off RS485 driver.
	mov	r5,#00h                 ; Required to exit wait!
	pop     DPH
	pop     DPL
	pop     b
	pop     acc
        pop     psw
	pop     ar2
	setb	ES1                     ; Re-enable interrupts.

	reti
