This article introduces two communication experiments which can be utilized by your Raspberry Pi.
1 USART Serial Communication
Before USB parallel communication becomes ubiquitous, Serial communications like USART protocal are very popular. Even today, these communications protocals are widespread used by the industry.
We plan to use USART 1 for this experiment. Referring to STM32F407 datasheet, PA9 and PA10 are individually used as TX and RX.
2 Socket communication
Socket communication is a kind of fundamental network communication mothod.Nowadays, Python is very popular so we will do a simple experiment for socket communication between Pi and the computer using python2 socket package.
Socket protocal is like TCT/IP, a two-directional protocal. But it has the design of server and client. We simply take Pi as the server and the computer as the client, neglecting security issues.
# 4.wait for client: connection, address = socket.accept() socket_con, (client_ip, client_port) = socket_tcp.accept() print("Connection accepted from %s." %client_ip) socket_con.send("Welcome to RPi TCP server!")
# 5.handle GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) whileTrue: try: print("Receiving package...") data = socket_con.recv(512) if len(data) > 0: if data == '1': num = bus.read_byte(address) print("Received:1, get rand number %d" %num) time.sleep(1) socket_con.send(str(num)) # socket data must be string elif data == '0': print("Received:0, exit") GPIO.output(11, GPIO.LOW) socket_con.close() # time.sleep(1) continue except Exception as e: print type(e) print e.args print e socket_tcp.close() sys.exit(1)
Comments
shortname
for Disqus. Please set it in_config.yml
.