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
uint8 c;
uint8 flag;
void initUART(void)
{
TMOD = 0x20;// 定时器工作方式2