Rookie Road--python Learning serial communication (and STC89C51) source and packaging release test water

Source: Internet
Author: User

Want to do host computer control system, take 51来 try water.

Python Environment: Win10+python 3.6.4 (64-bit) +serial,pyserial

First, the preparation of the upper computer program
import serialser = serial.Serial("COM3", 4800)ser.write(‘1‘.encode())ser.close()

Yes, that's right, just a few lines, send the ASCII of "1" to the COM3 port at 4800 baud rate.
. Encode () is a string encoded and can be decoded with. Decode ()
The above just implements the sending of a character, in order to make him look good, I joined the Pythongui. The Tkinter module is used.

import serialfrom tkinter import *ser = serial.Serial("COM3", 4800)def open():    ser.write(‘1‘.encode())    var.set("已打开")def close():    ser.write(‘2‘.encode())    data = ser.read()    print(data)    var.set("未打开")root = Tk()root.title("设备打开与关闭")frame1 = Frame(root)frame2 = Frame(root)var = StringVar()var.set("未打开")textLabel = Label(frame1,                  textvariable=var,                  justify=LEFT)textLabel.pack(side=LEFT)theButton1 = Button(frame2, text="打开", command=open)theButton1.pack()theButton2 = Button(frame2, text="关闭", command=close)theButton2.pack()frame1.pack(padx=10, pady=10)frame2.pack(padx=10, pady=10)mainloop()

This is what happens after you run it.

is not particularly shabby, hahaha, I just started to learn today. Let's see.
Basically is the point to open, to the serial port to send data ' 1 ', point Open, to the serial port to send data ' 2 '. The next machine microcontroller inside will be the ' 1 ' and ' 2 ' to identify, remember Oh, this is the characters ' 1 ' and ' 2 ' are not numbers 1 and 2. The serial communication is in the ASCII code to communicate.
attached:Serial common methods
Import Serial Module

Ser = serial. Serial (0) #打开第一个串口

Ser.portstr #串口的标识, under Windows is COM1,COM2,COM3

Ser.write ("Hello") #往串口写数据

Ser.close () Close the serial port

Ser.open () Open the serial port

Ser = serial. Serial (' COM1 ', 115200) set the port number and baud rate, and open the port

data = Ser.read () read one character

data = Ser.read (20) Read 20 characters

data = Ser.readline () reads a line, ends with/n, and reads without/n and is easily blocked.

data = Ser.readlines () and Ser.xreadlines () both need to set the time-out period

Ser.baudrate = 9600 Set baud rate

Ser View the status of the current serial port

Ser.isopen () see if this serial port has been opened

Note: do not name the new py file with serial, I am the name of the serial, causing the problem. Because there is an import serial in the code. Originally want to introduce people's official library, you name a serial, he cited the nearest you this document, definitely a problem.
That's the mistake.
Attributeerror:module ' Serial ' has no attribute ' serial '

Second, the single-chip computer program to write
#include "reg52.h"           //此文件中定义了单片机的一些特殊功能寄存器#include<intrins.h>typedef unsigned int u16;     //对数据类型进行声明定义typedef unsigned char u8;void UsartInit(){    SCON=0X50;          //设置为工作方式1    TMOD=0X20;          //设置计数器工作方式2    PCON=0X80;          //波特率加倍    TH1=0XF3;               //计数器初始值设置,注意波特率是4800的    TL1=0XF3;    ES=1;                       //打开接收中断    EA=1;                       //打开总中断    TR1=1;                  //打开计数器}void main(){       P2=0x00;    UsartInit();  //    串口初始化    while(1);       }void Usart() interrupt 4{    u8 receiveData;    u8 sendData;    sendData=‘1‘;    receiveData=SBUF;//出去接收到的数据    RI = 0;//清除接收中断标志位    if (receiveData==‘1‘)    {    P2=0xf0;    }    else if (receiveData==‘2‘)    {    P2=0x0f;    }}

And that's it. Burn to the microcontroller, and then you can do whatever you want.
In addition, the microcontroller can only receive one byte at a time, receive a byte to set the RI 0, and then to receive the next byte.

The one that came out today is to try the water. Play a bit. A good control system will be updated in the future.

Third, Packaging release EXE

Or to test the water, the written Python files are packaged as EXE can be run directly under Windows.
There are currently two modules of Pyinstaller and Py2exe
Py2exe does not support versions above Python3.4. I am Python3.6 (64-bit). You can use the latest version of Pyinstaller
That's right in the cmd window.
pip install pyinstaller
And then just
Pyinstaller.exe-f file path
On the line, pro-Test available

Rookie Road--python Learning serial communication (and STC89C51) source and packaging release test water

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.