Python与C51单片机交互,嘿嘿,其实是在OSX下用串口交互了~~


1.首先装上CH340/CH341 For Mac USB转串口驱动程序

2.安装Python串口模块  pip install pySerial

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2016-08-14 19:47:05
# @Author  : PiaoYun (piaoyunsoft@163.com)
# @Link    : http://www.dllhook.com

import serial
import time


def test():
	# 连接单片机
	ser = serial.Serial('/dev/tty.wchusbserial1420', 9600) 
	# 查看状态 
	if ser.isOpen():
	# 发送指令 -- 方便测试 写个死循环
	while True:
		for x in xrange(0, 3):
		    ser.write(str(x))
		    #图片上面忘记写读取单片机传回来数据的代码了,这里补上...
		    s = ser.read(10) 
		    print s
		    time.sleep(2)
	# 端口连接
	ser.close()

if __name__ == '__main__':
	test()
// 单片机程序
#include <reg52.h>
uint8 c;
uint8 flag;

void initUART(void)
{
    TMOD = 0x20;	// 定时器工作方式2
    //SCON = 0x50;	// 打开串口为方式1-如下分解:
    SM0 = 0;
    SM1 = 1;
    SM2 = 0;
    REN = 1;
    TH1 = 0xFD;	// 波特率6900
    TL1 = TH1;

    EA = 1;	// 打开中断总开关
    ES = 1;	// 打开串口中断开关
    TR1 = 1;	// 启动定时器1
}

// 发送一个字节数据
void sendOneByte(uint8 c)
{
    SBUF = c;
    while(!TI);	// 等待数据发送结束
    TI = 0;
}

// 串口中断
void serial() interrupt 4
{
    flag = 1;
    c = SBUF;
    if(RI)
    {
        RI = 0;
        //P1 = c;
		// 这里随便写了两个功能
		switch (c)
		{
			case '1':
				P1 = 0xAA;
				break;
			case '2':
				P1 = 0x55;
				break;
			default:
				P1 = 0xFE;
				break;
		}
    }
    else
        TI = 0;
}

void main(void)
{
    initUART();
    P1 = 0x7F;
    while(1){
	if (flag == 1)
	{
			
		ES = 0;		// 关闭串口中断
		flag = 0;	// 复位标记
		sendOneByte('p');
		sendOneByte('i');
		sendOneByte('a');
		sendOneByte('o');
		sendOneByte('y');
		sendOneByte('u');
		sendOneByte('n');
		ES = 1;
	}
	}
}


你可能感兴趣的文章

评论区

已有255位网友发表了看法:

1L 访客  2021-07-11 10:01:08 回复
想问一下具体如何操作的,就是如何实现串口发送
2L 游客  2024-07-11 09:18:18 回复
楼上的真不讲道理!http://58u36.69ey2.cn
3L 游客  2024-07-15 10:43:01 回复
观点鲜明,立场坚定,作者态度明确。http://9mqc.zhijian.me
4L 游客  2024-07-15 13:44:14 回复
有机会找楼主好好聊聊!http://2drew.0478g.com
5L 游客  2024-07-15 13:48:05 回复
态度决定一切,不错!https://sdceda.com/fei/324073856/
6L 游客  2024-07-15 13:48:32 回复
楼主好聪明啊!https://sdceda.com/fei/831777080/
7L 游客  2024-07-15 13:51:59 回复
很经典,收藏了!http://lbnp.ffnbx.net/test/480949054.html
8L 游客  2024-07-15 16:39:39 回复
回帖也有有水平的!https://www.henghost.com/
9L 游客  2024-07-15 17:35:09 回复
终于看完了,很不错!http://zhiw.ffnbx.net/test/395988110.html
10L 游客  2024-07-15 19:15:50 回复
以后就跟楼主混了!http://www.zhijian.me/t-56-1-1.html
11L 游客  2024-07-15 20:13:41 回复
感觉不错!http://m3c.google520.net
12L 游客  2024-07-15 21:39:14 回复
支持一下,下面的保持队形!https://sdceda.com/fei/218203596/
13L 游客  2024-07-15 21:53:42 回复
看了这么多帖子,第一次看到这么有深度了!https://www.msl.wang/search/
14L 游客  2024-07-15 21:55:39 回复
怎么我回帖都没人理我呢?https://www.wangzhanfenlei.com/adfabu.html
15L 游客  2024-07-16 01:13:52 回复
论坛的人气越来越旺了!https://sdceda.com/fei/977623993/
16L 游客  2024-07-16 02:35:46 回复
看帖不回帖的人就是耍流氓,我回复了!https://www.weimaitu.com/13.html
17L 游客  2024-07-16 03:35:59 回复
青春不在了,青春痘还在!https://sdceda.com/fei/371625933/
18L 游客  2024-07-16 04:20:21 回复
听楼主一席话,省我十本书!https://sdceda.com/fei/222546089/
19L 游客  2024-07-16 07:20:39 回复
白富美?高富帅?https://sdceda.com/fei/189939049/
20L 游客  2024-07-16 07:42:30 回复
看了这么多帖子,第一次看到这么经典的!https://www.uuu9923.cn/982.html
21L 游客  2024-07-16 07:43:40 回复
顶顶更健康!http://cmrx.ffnbx.net/test/526302463.html
22L 游客  2024-07-16 07:53:50 回复
经典!http://seme.ffnbx.net/test/475650611.html
23L 游客  2024-07-16 10:41:44 回复
每次看到楼主的帖子都有惊吓!http://ibgn.ffnbx.net/test/185250464.html
24L 游客  2024-07-16 11:00:00 回复
看了这么多帖子,第一次看到这么有深度了!http://izfr.ffnbx.net/test/094495546.html
25L 游客  2024-07-16 13:23:09 回复
谢谢楼主的分享!https://www.guanzhangtu.com/redianzixun/
26L 游客  2024-07-16 14:06:24 回复
帖子好乱!http://dvjf.ffnbx.net/test/881137169.html
27L 游客  2024-07-16 14:19:17 回复
在这个版块混了这么久了,第一次看见这么给你的帖子!http://liey.ffnbx.net/test/114129940.html
28L 游客  2024-07-16 16:34:12 回复
好无聊啊!https://www.weimaitu.com/13.html
29L 游客  2024-07-16 17:02:04 回复
文章写太挺好了,真的值得推荐https://sdceda.com/fei/243038332/
30L 游客  2024-07-16 17:05:41 回复
看帖、回帖、拿分、走人http://fkuk.www.weimaitu.com
31L 游客  2024-07-16 23:38:52 回复
我默默的回帖,从不声张!http://4al26.www.zsbfz.com
32L 游客  2024-07-16 23:39:18 回复
有钱、有房、有车,人人都想!http://ys0m.www.weimaitu.com
33L 游客  2024-07-16 23:51:57 回复
顶顶更健康!http://mu0tl.https://www.hkdiyi.com/a/43970.html
34L 游客  2024-07-17 04:54:37 回复
看帖回帖一条路!http://shanghai004.dafangyicai.fun/
35L 游客  2024-07-17 08:31:03 回复
每次看到楼主的帖子都有惊吓!http://www.dfpump.cn
36L 游客  2024-07-17 08:32:53 回复
鸟大了,什么林子都敢进啊!http://gengsha.cn/dna/177.html
37L 游客  2024-07-17 10:37:52 回复
有内涵!http://599k.wk.a691.com
38L 游客  2024-07-17 10:50:00 回复
每次看到楼主的帖子都有惊吓!https://weibo.com/tv/show/1034:5056362657284148
39L 游客  2024-07-17 11:58:46 回复
看了这么多帖子,第一次看到这么经典的!http://hnjlyzjd.com/xl/wz_21468.html
40L 游客  2024-07-17 12:05:53 回复
楼主的文笔不错!http://osl.google520.net
41L 游客  2024-07-17 12:12:42 回复
我裤子脱了,纸都准备好了,你就给我看这个?https://www.msl.wang/links/5c7658274ca982403532.html
42L 游客  2024-07-17 12:17:04 回复
看了这么多帖子,第一次看看到这么有内涵的!http://wqc.0478g.com
43L 游客  2024-07-17 12:40:51 回复
雷锋做好事不留名,都写在帖子里!https://www.henghost.com/
44L 游客  2024-07-17 13:41:32 回复
楼上的很有激情啊!https://weibo.com/tv/show/1034:5056362657284148
45L 游客  2024-07-17 16:15:25 回复
精华帖的节奏啊!https://www.alcgpos.com/hz/1052.html
46L 游客  2024-07-17 18:41:01 回复
楼上的心情不错啊!http://www.dfpump.cn
47L 游客  2024-07-17 18:57:22 回复
这个帖子会火的,鉴定完毕!http://3vp9r.0478g.com
48L 游客  2024-07-17 20:10:56 回复
信楼主,考试不挂科!http://175kf.com
49L 游客  2024-07-17 20:16:08 回复
楼上的能详细介绍一下么?http://xq0jj.https://www.hkdiyi.com/a/42061.html
50L 游客  2024-07-17 21:43:27 回复
看帖回帖一条路!http://5397zk.https://www.weimaitu.com/
51L 游客  2024-07-18 00:41:38 回复
好好学习楼主的帖子!http://175kf.com
52L 游客  2024-07-18 05:54:13 回复
看帖不回帖都是耍流氓!http://175kf.com
53L 游客  2024-07-18 11:29:37 回复
林子大了,什么鸟都有了啊!https://weibo.com/tv/show/1034:5057432364449818
54L 游客  2024-07-18 14:13:29 回复
我就搞不明白了,看帖回帖能死人么,居然只有我这么认真的在回帖!http://gengsha.cn/dna/113.html
55L 游客  2024-07-18 14:18:55 回复
林子大了,什么鸟都有了啊!https://weibo.com/tv/show/1034:5057432108597283
56L 游客  2024-07-18 17:25:05 回复
帖子很有深度!https://www.dwdmr.com
57L 游客  2024-07-18 19:29:45 回复
缺乏激情了!https://weibo.com/u/5901183547
58L 游客  2024-07-18 21:10:49 回复
在这个版块混了这么久了,第一次看见这么给你的帖子!https://www.uuu9923.cn/1858.html
59L 游客  2024-07-18 21:27:32 回复
小弟默默的路过贵宝地~~~http://v4x.https://www.weimaitu.com/
60L 游客  2024-07-19 00:41:45 回复
楼主加油,看好你哦!http://7jluqp.https://www.hkdiyi.com/a/43831.html
61L 游客  2024-07-19 05:22:03 回复
观点鲜明,立场坚定,作者态度明确。http://m68rsx.www.szhqty.com/
62L 游客  2024-07-19 05:55:44 回复
很有品味!http://175kf.com
63L 游客  2024-07-19 06:30:19 回复
楼上的很有激情啊!https://www.uuu9923.cn/1858.html
64L 游客  2024-07-19 07:27:58 回复
楼主主机很热情啊!https://www.kaozn.com/gk/51924.html
65L 游客  2024-07-19 07:41:19 回复
好无聊啊!http://www.njqqjg.cn
66L 游客  2024-07-19 08:56:43 回复
楼上的能详细介绍一下么?http://www.pc235.com/news/205.html
67L 游客  2024-07-19 09:38:20 回复
最近压力山大啊!http://www.chinafishingrod.com/rm/9494.html
68L 游客  2024-07-19 11:30:58 回复
网页的加载速度非常快,不会影响用户体验。https://www.uuu9923.cn/1858.html
69L 游客  2024-07-19 13:30:19 回复
林子大了,什么鸟都有了啊!https://www.msl.wang/links/3575081a430b8fdcd238.html
70L 游客  2024-07-19 13:34:11 回复
我对楼主的敬仰犹如滔滔江水绵延不绝!https://www.uuu9923.cn/1858.html
71L 游客  2024-07-19 14:31:03 回复
看了这么多帖子,第一次看到这么高质量内容!https://www.uuu9923.cn/1858.html
72L 游客  2024-07-19 14:48:47 回复
宇宙第一贴诞生了!https://www.zhaitoushe.com
73L 游客  2024-07-19 15:35:24 回复
楼主的头像是本人吗?https://www.uuu9923.cn/1872.html
74L 游客  2024-07-19 16:45:51 回复
看在楼主的面子上,认真回帖!https://www.uuu9923.cn/1858.html
75L 游客  2024-07-19 17:13:02 回复
很有看点!https://www.uuu9923.cn/1858.html
76L 游客  2024-07-19 17:53:22 回复
观点鲜明,立场坚定,作者态度明确。http://www.abgobgw.cn
77L 游客  2024-07-19 18:05:32 回复
楼上的真不讲道理!https://www.uuu9923.cn/1858.html
78L 游客  2024-07-19 18:10:24 回复
楼主主机很热情啊!http://175kf.com
79L 游客  2024-07-19 18:28:13 回复
楼主是一个典型的文艺青年啊!https://www.moyidian.com
80L 游客  2024-07-19 20:08:59 回复
楼主内心很强大!http://175kf.com
81L 游客  2024-07-19 20:27:13 回复
楼主是好人!https://www.uuu9923.cn/1872.html
82L 游客  2024-07-19 21:16:48 回复
十分赞同楼主!http://175kf.com
83L 游客  2024-07-19 23:00:44 回复
楼主说的我也略懂!https://www.uuu9923.cn/1858.html
84L 游客  2024-07-20 00:55:32 回复
今天是个特别的日子,值得纪念!https://www.yxmingju.com/sitemap.txt
85L 游客  2024-07-20 00:56:25 回复
论坛人气好旺!http://175kf.com
86L 游客  2024-07-20 03:56:33 回复
今天过得很不爽!http://897xy.https://www.0478g.com
87L 游客  2024-07-20 04:27:25 回复
楼主的帖子实在是写得太好了。文笔流畅,修辞得体!http://175kf.com
88L 游客  2024-07-20 04:54:33 回复
大神好强大!http://175kf.com
89L 游客  2024-07-20 05:19:25 回复
以后就跟楼主混了!http://www.chinafishingrod.com/rm/11279.html
90L 游客  2024-07-20 06:08:06 回复
不错哦,楼主这是要火的节奏啊!http://175kf.com
91L 游客  2024-07-20 06:47:22 回复
论坛的人气不行了!https://www.qupojie.cn/category-winxt.html
92L 游客  2024-07-20 08:32:43 回复
好好学习楼主的帖子!https://www.uuu9923.cn/1858.html
93L 游客  2024-07-20 08:33:32 回复
顶顶更健康!https://www.uuu9923.cn/1872.html
94L 游客  2024-07-20 08:34:13 回复
太邪乎了吧?https://www.uuu9923.cn/1872.html
95L 游客  2024-07-20 10:28:48 回复
我只是来赚积分的!http://175kf.com
96L 游客  2024-07-20 13:40:50 回复
以后就跟楼主混了!http://www.pc235.com/news/73.html
97L 游客  2024-07-20 16:44:06 回复
感觉不错!http://www.chinafishingrod.com/rm/12693.html
98L 游客  2024-07-20 16:48:25 回复
宇宙第一贴诞生了!https://www.zgqy91.com
99L 游客  2024-07-20 17:43:57 回复
经典!https://www.yxmingju.com/sitemap.txt
100L 游客  2024-07-20 18:52:11 回复
今天过得很不爽!https://www.ystcw.com/y/173059.html

发表评论

必填

选填

选填

必填

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。