Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import socket
  4. import sys
  5. import os
  6. import wiringpi2 as wiringpi
  7. import serial
  8. import _thread as thread
  9. from time import sleep
  10.  
  11. ######################### SETUP #########################################################
  12. wiringpi.wiringPiSetup()  #enable pins
  13. wiringpi.pinMode(7, 1) # sets GPIO 7 to output  
  14. wiringpi.pinMode(8, 1) # sets GPIO 8 to output  
  15. wiringpi.pinMode(9, 1) # sets GPIO 9 to output
  16.  
  17. server_address = '/tmp/uds_dnvff'
  18.  
  19. port = serial.Serial("/dev/ttyAMA0", baudrate=38400, timeout=3.0, bytesize=serial.EIGHTBITS, parity=serial.PARITY_EVEN, stopbits=serial.STOPBITS_ONE)
  20. port.write('Hello world\n'.encode('utf-8'))
  21.  
  22. # Make sure the socket does not already exist
  23. try:
  24.     os.unlink(server_address)
  25. except OSError:
  26.     if os.path.exists(server_address):
  27.         raise
  28.        
  29. # Create a UDS (UDP) socket
  30. sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  31.  
  32. # Bind the socket to the port
  33. print ('starting up on %s' % server_address)
  34. sock.bind(server_address)
  35.  
  36. os.chmod(server_address, 0o777)
  37.  
  38. # Listen for incoming connections
  39. sock.listen(1)
  40. #########################################################################################
  41.  
  42.  
  43. def listen_on_TCP():
  44.  
  45.     while True:
  46.         # Wait for a connection
  47.         print ('waiting for a connection')
  48.         connection, client_address = sock.accept()
  49.         try:
  50.             print ('connection')
  51.  
  52.             # Receive the data in small chunks and retransmit it
  53.             while True:
  54.                 data = connection.recv(32)
  55.                 print('received "%s"' % data)
  56.                 if data:
  57.                     print('sending data back to the client')
  58.                     connection.send(data)
  59.                     port.write(data)
  60.                 else:
  61.                     print('no more data ')
  62.                     break
  63.                
  64.         finally:
  65.             # Clean up the connection
  66.             connection.close()
  67.  
  68.  
  69. def listen_on_UART():
  70.     print('listening on uart...')
  71.    
  72.    
  73. try:
  74.    thread.start_new_thread( listen_on_TCP, () )
  75.    thread.start_new_thread( listen_on_UART, () )
  76. except:
  77.    print('Error: unable to start thread')    
  78.  
  79. while 1:
  80.     sleep(10)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement