Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import socket
- import sys
- import os
- import wiringpi2 as wiringpi
- import serial
- import _thread as thread
- from time import sleep
- ######################### SETUP #########################################################
- wiringpi.wiringPiSetup() #enable pins
- wiringpi.pinMode(7, 1) # sets GPIO 7 to output
- wiringpi.pinMode(8, 1) # sets GPIO 8 to output
- wiringpi.pinMode(9, 1) # sets GPIO 9 to output
- server_address = '/tmp/uds_dnvff'
- port = serial.Serial("/dev/ttyAMA0", baudrate=38400, timeout=3.0, bytesize=serial.EIGHTBITS, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE)
- port.write('Hello world\n'.encode('utf-8'))
- # Make sure the socket does not already exist
- try:
- os.unlink(server_address)
- except OSError:
- if os.path.exists(server_address):
- raise
- # Create a UDS (UDP) socket
- sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- # Bind the socket to the port
- print ('starting up on %s' % server_address)
- sock.bind(server_address)
- os.chmod(server_address, 0o777)
- # Listen for incoming connections
- sock.listen(1)
- #########################################################################################
- def listen_on_TCP():
- while True:
- # Wait for a connection
- print ('waiting for a connection')
- connection, client_address = sock.accept()
- try:
- print ('connection')
- # Receive the data in small chunks and retransmit it
- while True:
- data = connection.recv(32)
- print('received "%s"' % data)
- if data:
- print('sending data back to the client')
- connection.send(data)
- port.write(data)
- else:
- print('no more data ')
- break
- finally:
- # Clean up the connection
- connection.close()
- def listen_on_UART():
- print('listening on uart...')
- try:
- thread.start_new_thread( listen_on_TCP, () )
- thread.start_new_thread( listen_on_UART, () )
- except:
- print('Error: unable to start thread')
- while 1:
- sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement