Raspberry Pi uses COM serial port to send and receive messages, and use pyCharm for remote debugging

First post the Python code
import serial
ser = serial.Serial('/dev/ttyUSB0',115200,parity=serial.PARITY_NONE)
if ser.isOpen == False:
        ser.open()
ser.write("serial turn on")
try:
        while 1:
                size = ser.inWaiting()
                if size != 0:
                        response = ser.read(size)
                        print response
                        ser.flushInput()
except keyboardInterrupt:
        ser.close()


After the above code runs, it will monitor the input of the keyboard, and then it will send out the output of the serial port, and monitor the input port at the same time.
I use a line to connect the output and input of the serial port, so that the output content will enter the input port,

so the keyboard input After the content, press Enter, send, and the program will receive the content and print it at the same time.

Let's talk about remote debugging.
Remote debugging will basically be divided into three steps
. First, build a local server
. Second, deploy files to the remote, and configure remote programs and local mapping.
Third , run the program. The


first step is to build a local server
pyCharm for remote debugging. First, Find a file called pycharm-debug.egg in the pyCharm installation directory, transfer this file to the raspberry, and then use the command
easy_install pycharm-debug.egg

After installation, it is time to configure the contents of pyCharm. Under
Menu- >Run->Edit Configuration, click the plus sign to create a Python Remote Debug, as shown in the figure below.






The Local host name is the local IP configuration, and the port is selected by yourself. , the purpose of this block is to create a Server locally for remote programs to access, and remote programs to access the local Server to implement remote debugging calls of the code.

There is also a Path mappings in the figure, which is mainly used for path mapping between local code and remote code.

After this configuration, you can start the server. After the startup is complete, two lines of code will be output, similar to the following
import pydevd
pydevd.settrace('192.168.1.101', port=50000, stdoutToServer=True, stderrToServer=True)


Paste these two lines of code at the top of the python code above.
The above part only starts the local Server service.


The following describes the configuration of the running program.
2. Deploy to remote
Note that the community edition does not have this function.
Menu->Tools->Deployment->Configurations, as shown in the figure below



Of course , the name can be arbitrarily named here, SFTP host is the remote address, Port is the remote port, Root path is the path to upload to, and the remaining user names, password or something.
After filling in, jump to the second label Mappings, as shown in the figure below,



Local path is the local code mapping address, and Deployment path on Server is filled in with the deployment directory address based on the remote address in the industry based on the first label, here I filled in /, that is,
click .

Then you can upload, download, synchronize and compare functions through Upload, Download, Sync, etc. under
Menu- >Tools->Deployment.

The third part, debugging
In the Preferences of pyCharm, find the corresponding project, then Project Interpreter, as shown below,



select Deployment configuration or SSH Credentials, configure the remote connection parameters, and save it.

The next step is to configure the debugger.
Menu->Tools->Deployment->Configurations, create a Python application, the configuration is as follows



This is mainly the configuration of the python interpreter, which is created in the above step.

After the above configuration is completed, run the program directly. On the top, we add the configuration code of pydevd in the first step. This code will access our local server, and the local server will be associated with the local code. Try the breakpoint, the program can be debugged remotely.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326302288&siteId=291194637