Php Serial Port Communication Linux Tutorial Pdf

TCP/IP COMMUNICATION

Data link over TCP/IP

Synchronous Communications; Accessing Serial Ports. Standard MODEM Commands; Common MODEM Communication Problems. Under IRIX®, HP-UX, SunOS®, Solaris®, Digital UNIX®, Linux®, and most other UNIX operating systems.

Information exchange between a program running on the Raspberry Pi and a partner program running on a remote computer systems becomes important when the Raspberry Pi is a front end of a measurement system and transfers sensor data in real-time to a control station or when a remote PC sends commands a RPi based robot (remote control mode).

In most situations the data link is established via TCP/IP and the well-established client/server technology based on socket programming is used. In this chapter we show typical programs for simple TCP communication without going into much details (consult other tutorials about socket programming to acquire a full understanding).

Most important and somewhat unexpected is the fact that the communication partners, the server and the client, are not symmetrical. Rather, first the server program must be started before the client program can engage a connection to it. In order to identify the two computers on the Internet, their IP address is used. In addition, server and client specify one of 65536 communication channels (IP ports).

When the server starts, it creates a server socket (like a electrical plug) that uses a particular port and goes in a wait state. We say that the server is 'listening' for an incoming client. The client then creates a client socket (the plug counterpart) and tries to establish a communication link to the server using its IP address and port number. In the most single arrangement client and server are connected to the same router and use the same IP segment.

Experiment 1: Programming with a low-level socket library

In a client/server application between a Raspberry Pi and a remote PC, the RPi can be server while the PC is a client or the roles may be interchanged. It depends on the specific situation which mode is preferred. In the following example the RPi acts as a measurement server that reports data from an attached sensor to a remote PC client. The programs are written by using the basic Python socket module and thus require a lot of code.

Aim:
Run a socket server on the RPi that reports the state of a GPIO input (button pressed/release) to a PC client each time the client sends a 'go' command.

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
The server enters the listening state by calling the blocking function serverSocket.accept() that returns with connect information when a link with a client is established. Then a SocketHandler thread is started that handles the connection with the client. Normally the main program could enter the listening state and wait for another client, but in our program the main thread just 'hangs' in a loop without doing something special.

It is recommended to define a simple flag to enter a 'debug mode', where verbose information is written to stdout. Once the program works successfully, the verbose mode can be turned off.

Connectify pro activator. The clients just sends a 'go' about every 2 seconds and displays the server reply.

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
The client also starts a thread to handle the incoming data. This is not really necessary in this situation, but usual for more complicated client/server applications.

Experiment 2: Easy coding with an event-driven socket library

Linux serial driver tutorial

No special knowledge about socket programming is needed, if you use our event-driven tcpcom package (see www.aplu.ch/tcpcom for full information). Download tcpcom.py from here and copy it in the same folder with your program. The code is dramatically simplified while maintaining the same functionality.

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
You may run the same client as above, but the client code is also very much simplified by the tcpcom module.

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)
Php serial port communication linux tutorial pdf online

Experiment 3: Accessing a local server from the Internet

In this experiment we exchange the roles of client and server and run the client on the Raspberry Pi and the server on a PC connected through a WLAN or Ethernet router located anywhere on the Internet.

To make the your PC server accessible from the Internet, you must setup the router for 'IP forwarding' by specifying a single or a range of IP ports. (The setup is sometimes called 'Virtual Servers'.) Any request from outside to the router's IP address with a port in the specified range is then forwarded to your PC.

There are two problems: Because your PC's IP address is given by your router's DHCP, it is subject to change. Thus the IP forwarding to the specified IP may fail. To avoid this problem, you can either setup your PC to use a fixed IP address or bind the IP address to the fixed Mac address of your PC's WLAN or Ethernet adapter by an 'address reservation' (if available on your router).

The second problem arises because your router's IP address is delivered by your provider using DHCP, and thus is also subject to change. Therefore your PC server cannot be accessed from outside with a fixed, well-known IP address. This problem can be solved by using an Dynamic Update Client (DUC), provided for free from noip.com. The DUC connects every 5 minutes (or another interval you chose) to the no-ip server and announces your router's IP address. From the TCP client the link is established using the IP alias provided by no-ip and the connection request is then forwarded by the no-ip server to your router that finally forwards it to the PC.

Aim:
You run a simple socket server on a PC listening on port 22000 that just displays state messages in a console. Try to setup your router so that the server is visible from anywhere on the Internet. A client on the Raspberry Pi connects to the server and sends sensor information (here just the state of a button) every second.

If you use the tcpcom library, the programs remain almost the same. The PC server just displays in the console the sensor information received from the client.

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

The client code on the Raspberry Pi is simple too.
(If server and client are sitting on the same internal IP segment for a test arrangement, the client connects using the PC's IP address that you may find out by running ipconfig on Windows or ifconfig on Mac/Linux or by using another network tool.)

Program:[►]

Highlight program code(Ctrl+C copy, Ctrl+V paste)

Remarks:
After you succeed in establishing the data link with server and client on the same network segment (attached to the same router), you may exercise the situation when the PC is accessed from the Internet. Setup your router with IP forwarding as described above and establish a connection from the Raspberry Pi using the IP address (or the no-ip alias) of your router.

If you do not know the IP address of the router, it is displayed by visiting www.portchecktool.com with a PC browser. As you see, an external server can trace back the route to your router's IP address that is given by your provider and where possibly your name/address is registered.

If everything works find, you can move your Raspberry Pi to another place (even far away) and establish an Internet connection from there. You may also acquire an account with no-ip.org, download and run a DUC (Dynamic Update Client) on your PC and access the PC server from the Raspberry Pi with your no-ip IP alias.

If you prefer to exchange the roles and use a Raspberry Pi as server and DUC, just do it know.