Program Analysis and Explanation

Share for us

Abstract

From the perspective of software, the smart car is of C/S structure. The TCP server program is run on Raspberry Pi to listen to the command from the client and control the car accordingly. The client program is run on the PC and connected with the server through the TCP, which provides the user with a graphical user interface (GUI) to conveniently control the Raspberry Pi remotely. Both the client and server programs are written in Python.

Make sure that the circuit is connected properly. Power the smart car, log in the Raspberry Pi remotely, go to the directory Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi and check the files under it.

cd ~/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi

ls

pi@raspberrypi ~/Sunfounder_Smart_Video_Car_Kit_for_RaspberryPi $ ls

client  datasheet  html_server  i2cHelper.py  mjpg-streamer  README.md  server

You can see seven files under the directory: client, datasheet, html_server, i2cHelper.py, mjpg-streamer, a file README.md, and server.

Wherein, 

client, the client run on your PC,

datasheet contains some PDF files about the chip (you need to view them on PC),

html_server, the web server run on the Raspberry Pi for Android app client,

i2cHelper, a simple script to help you set up i2c on the Raspberry Pi,

mjpg-streamer, the camera driver to acquire and upload images,

README.md, an introduction file with update information,

server, the server run on the Raspberry Pi for the client on your PC.

Introduction of Socket

The C/S-structure program of the SunFounder Raspberry Pi-based Smart Car is written based on the socket module of the Python language. Socket wraps and applies the TCP/IP and is used to describe IP address and port. Also it is a network data structure for computer. The socket module should be created before the communication of network applications. If the socket can be said to be the plug of a telephone, which is the lowest layer of communication, then the combination of IP address and ports can be said to be that of area code and phone numbers. Only having the hardware for a phone call making is not enough. You still need to know whom and where to call. An Internet address is composed of the essential IP address and port for network communication.

1.    Server

Here we provide a pseudocode which creates a universal TCP server for explanation. Note that this is just one of the methods for server design. After you have a good knowledge about it, you can alter the pseudocode as you want:

= socket( )                # Create a socket for the server.

s.bind( )                    # Bind the address to the socket.

s.listen( )                   # Listen to the connection.

inf_loop:                   # Indefinite loop of the server.

    c = s.accept( )          # Accept the connection from the client.

comm_loop:               # Communication loop.

    c.recv( )/c.send( )       # Dialog (receiving or sending data)

c.close( )                   # Close the socket of the client.

s.close( )                   # Close the socket of the server (optional).

All kinds of socket can be created via the function socket.socket( ) and then bound with IP address and port by the function bind( ). Since TCP is a connection-oriented communication system, some settings need to be completed before the TCP server starts operation. The TCP server must “listen” to connections from the client.

After the settings are done, the server will enter an indefinite loop. A simple, like single-thread, server will call the function accept( ) to wait for the coming connection. By default, the function accept( ) is a blocking one, which means it is suspended before the connection comes. Once a connection is received, the function accept( ) returns a separate client socket for the subsequent communication. After the temporary socket is created, communication begins. Both the server and client use the new socket for data sending and receiving. The communication does not end until either end closes the connection or sends a null character string.

Process Diagram of Server Program

2.    Client

It is easier to create a TCP client than to do a server. Take the following pseudocode:

= socket( )                # Create a client socket.

c.connect( )                # Try to connect a server.

comm_loop:               # Communication loop.

    c.send( )/c.recv( )       # Dialog (sending out and receiving data)

c.close( )                   # Close the client socket.

As mentioned above, all sockets are created via the function socket.socket( ). Then, the function connect( ) can be called to connect the server. After the connection is built, the dialog between the client and the server is enabled. When the dialog ends, the client can close the socket and the connection.

Introduction of Tkinter

Developed based on Tkinter, our client program carriesgraphical interfaces. Tkinter is a GUI widget set for Python. We can develop application programs with graphical interfaces fast by Python language based on it. It is quite easy to use Tkinter. All you have to do is toimport the module into Python.

To create and run a GUI program, take the following steps:

a) Import the Tkinter module (by import Tkinter or from Tkinter import *).

b) Create a top window object to contain the whole GUI program.

c)  Create the GUI module needed on the object and enable the functions.

d) Connect the GUI modules with the code at the system back-end.

e)  Enter the main event loop.

Take a simple GUI program:

Create a file Tk_test.py under the path /home:

touch Tk_test.py

Add executable privilege to the file:

chmod +x Tk_test.py

Open the file:

vim Tk_test.py

Type in the following code:aaaaaaaaaaaaaa

#!/usr/bin/env python
from Tkinter import *

top = Tk()       # Create a top window
top.title('Sunfounder.com')

label = Label(top, text='Hello Geeks !', fg='blue')  # Create a label and set its foreground color as blue
label.pack()     # layout

top.mainloop()  # main loop

Save the code and exit.

Run

./Tk_test.py

Then the following picture will appear on your screen:

Click  to close the program.

Process Diagram of Client Program