# RS232/485/422 port

<abstract>
CM-Panel POE is equipped with a serial port capable of operating in three different modes: RS232, RS485, and RS422.
</abstract>

<div class="alert alert-danger">
Please note that the J15 serial port is physically equal to the POE connector but the signals  they carry are vastly different and not interchangeable. 
<br/>
!! Connecting the wrong cables can lead to device malfunctions, data corruption, or even hardware damage !!
</div>

The image displayed below illustrates the locations of the <code>J15 serial port connector</code> and <code>SW1 dip switch</code> 

<img src="./serial_port.jpg" style="max-width:1024px; width:100%;">

The operational mode of the serial port can be selected through a 4-position DIP switch.

Serial port is visible in Linux as <code>/dev/ttyAMA0 device</code>.


Check the file <code>/boot/config.txt</code> to see if these lines are present:

	# RS232/RS485/RS422 port on /dev/ttyAMA0 
	force_turbo=1
	dtoverlay=uart0,txd0_pin=36,rxd0_pin=37,pin_func=6

If not insert them and reboot.

## Set the RS232 Mode

Dip switch positions:

	SW1 1 OFF
	SW1 2 OFF
	SW1 3 OFF
	SW1 4 OFF

Pinout:

	J15 2 RXD
	J15 3 GND
	J15 6 TXD

## Set the RS485 full duplex mode (RS422)

Dip switch positions:

	SW1 1 ON
	SW1 2 OFF
	SW1 3 OFF=No termination - ON=120 ohm termination
	SW1 4 OFF=Normal - ON=Limited slew rate limited

Pinout:

	J15 2 A
	J15 3 GND
	J15 4 B
	J15 5 Y
	J15 6 Z


## Set the RS485 half duplex mode

Dip switch positions:

	SW1 1 ON
	SW1 2 ON
	SW1 3 OFF=No termination - ON=120 ohm termination
	SW1 4 OFF=Normal - ON=Limited slew rate limited

Pinout:

	J15 3 GND
	J15 5 A
	J15 6 B
	
### RS485 Python example

	import RPi.GPIO as GPIO
	import serial
	import time
	
	DE_pin = 38 # Driver enable pin
	
	GPIO.setmode(GPIO.BCM)
	GPIO.setup(DE_pin, GPIO.OUT)
	
	ser = serial.Serial('/dev/ttyAMA0', 115200) 
	
	def send_data(data):
	    GPIO.output(DE_pin, GPIO.HIGH)  # Enable sending mode
	    ser.write(data.encode())  # Send data
	    ser.flush()  # Ensure all data is sent
	    GPIO.output(DE_pin, GPIO.LOW)  # Disable sending mode
	
	try:
	    send_data("Hello from RS485")  # Send a string
	finally:
	    GPIO.cleanup()  # Clean up GPIO on exit



## Links

* [SP335E - RS-232/RS-485/RS-422 Transceiver datasheet](https://assets.maxlinear.com/web/documents/sp335e.pdf)
 
<hr>

@include='adv_cmpanel'

