|
Edited by afric at 2018-3-3 21:53
Final i install python
- sudo apt-get install python-serial python3-serial
- sudo nano read_serial.py
Copy code i write the script
- #start read_serial.py 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
- #}
- #end read_serial.py script
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- #start script
- # 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()
- #end script
Copy code now if you run sudo python serial_to_udp.py 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 i run
- python serial_to_udp.py /dev/ttyS2 192.168.1.100 13000 &
Copy code and i take serial data from ttyS2 (i have a MAX3232 RS232 Serial Port To TTL ) and i sent it to my pc at port 13000
|
|