|
Edited by afric at 2018-3-4 06:51
buy MAX3232 RS232 Serial Port To TTL and i Connected to uart 2- ttyS2 (pin4-5V,pin6-GND,pin11-UART2 RX,pin13-UART2 TX)
I use Armbian_5.35_Orangepizero_Debian_jessie_default_3.4.113
install python
- sudo apt-get install python-serial python3-serial
Copy code write the script
- #!/usr/bin/env python
- import time
- import serial
-
- ser = serial.Serial(
- port='/dev/ttyS2',
- baudrate = 38400,
- parity=serial.PARITY_NONE,
- stopbits=serial.STOPBITS_ONE,
- bytesize=serial.EIGHTBITS,
- timeout=1
- )
- counter=0
-
-
- while 1:
- x=ser.readline()
- print x
Copy code
and i run
- sudo python read_serial.py
Copy code to see if ti works
after that i write onether script (i find it ready)
- sudo nano serial_to_udp.py
Copy code- # Python script for serial to UDP communication
- # Alex Olwal, 2012 03 24
- # www.olwal.com
- #
- import serial
- import sys
- from socket import *
- def send(msg, ip, port):
- socket(AF_INET,SOCK_DGRAM).sendto(msg, (ip, port))
- baud_rate = 38400
- if ( len(sys.argv) == 1 ):
- print "Serial-to-UDP utility | Alex Olwal, 2012, www.olwal.com"
- print "Syntax: " + sys.argv[0] + " serial_port udp_ip(= 127.0.0.1) udp_port(= 5000)"
- print "Example: " + sys.argv[0] + " COM20 127.0.0.1 5000"
- quit()
- serial_port = sys.argv[1]
- if ( len(sys.argv) >= 3 ):
- udp_ip = sys.argv[2]
- else:
- udp_ip = "127.0.0.1"
- if ( len(sys.argv) >= 4 ):
- udp_port = sys.argv[3]
- else:
- udp_port = "5000"
-
- if ( len(sys.argv) >= 5):
- printing = 1
- else:
- printing = 0
- print "Reading from serial port: " + serial_port
- print "Sending to " + udp_ip + ":" + udp_port
- udp_port = int(udp_port)
- s = serial.Serial( serial_port, baud_rate, timeout=1 )
- while (1):
- line = s.readline()
- if (line != ''):
- if (printing):
- print line[:-1]
-
- send( line, udp_ip, udp_port )
- else:
- if (printing):
- print "."
- s.close()
Copy code now if you run
- sudo python serial_to_udp.py
Copy code you will take
- Serial-to-UDP utility | Alex Olwal, 2012, www.olwal.com
- Syntax: serial_to_udp.py serial_port udp_ip(= 127.0.0.1) udp_port(= 5000)
- Example: serial_to_udp.py COM20 127.0.0.1 5000
Copy code so run
- python serial_to_udp.py /dev/ttyS2 192.168.1.100 13000 &
Copy code and take serial data from ttyS2 and sent it to pc at port 13000
there are 2 ways to auto start on boot
the fisrt is direct config rc.local but put inside 1minute wait to find the ethernet
- sleep 1m
- python /root/serial_to_udp.py /dev/ttyS2 192.168.1.100 13000 &
Copy code before exit0
and the other to create a script
- sudo nano /etc/init.d/serial_to_udp.sh
Copy code- #!/bin/sh
- python /root/serial_to_udp.py /dev/ttyS2 192.168.1.100 13000 &
Copy code i make it executable with
- sudo chmod +x /etc/init.d/serial_to_udp.sh
Copy code and after that run
- sudo update-rc.d serial_to_udp.sh defaults
Copy code and after that modified the rc.local
write
- /etc/init.d/serial_to_udp.sh
- sh '/etc/init.d/serial_to_udp.sh'
Copy code before exit0
and it worksssssssssss
|
|