python 所有版本的源代碼可以在這里下載到:
在南昌縣等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供成都做網(wǎng)站、成都網(wǎng)站制作 網(wǎng)站設(shè)計制作按需規(guī)劃網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,網(wǎng)絡(luò)營銷推廣,外貿(mào)網(wǎng)站建設(shè),南昌縣網(wǎng)站建設(shè)費用合理。
python沒有像matlab那樣的函數(shù)可以直接查看某個函數(shù)的源代碼,只有去下載整個源代碼查看了,不過找起來應該也不難,另外你也可以寫一個小程序來查看對應函數(shù)的源代碼。
Python的函數(shù)調(diào)用方式是通過import來調(diào)用的對應的py文件。
庫函數(shù)有內(nèi)建函數(shù)build_in(會寫python的時候已經(jīng)可以不用看了,不會寫的時候看也看不懂),和通過pip直接下載或者github上下載再安裝的函數(shù)。本質(zhì)上都是py文件。后者有時候由于環(huán)境的不同需要自行修改(這種情況較少),一般在安裝路徑下"\Lib\site-packages"文件夾中存在。
學習庫函數(shù)最好的方法是看網(wǎng)上官方的幫助文檔,此外還可以通過python自帶的dir()方法查看所有的屬性和方法,或者用help()方法查看幫助文檔(部分別人造的輪子不一定有)。
另外推薦使用ipython,Python創(chuàng)始人之一的成員編寫的交互式系統(tǒng)。
就是打開串口后,啟動一個線程來監(jiān)聽串口數(shù)據(jù)的進入,有數(shù)據(jù)時,就做數(shù)據(jù)的處理(也可以發(fā)送一個事件,并攜帶接收到的數(shù)據(jù))。
我沒有用到串口處理太深的東西。
客戶的原程序不能給你,不過我給你改一下吧。
里面的一些東西,已經(jīng)經(jīng)過了處理,要運行,可能你要自己改一下,把沒有用的東西去掉。
我這里已經(jīng)沒有串口設(shè)備了,不能調(diào)了,你自己處理一下吧,不過基本的東西已經(jīng)有了。
=================================================================
#coding=gb18030
import sys,threading,time;
import serial;
import binascii,encodings;
import re;
import socket;
class ReadThread:
def __init__(self, Output=None, Port=0, Log=None, i_FirstMethod=True):
self.l_serial = None;
self.alive = False;
self.waitEnd = None;
self.bFirstMethod = i_FirstMethod;
self.sendport = '';
self.log = Log;
self.output = Output;
self.port = Port;
self.re_num = None;
def waiting(self):
if not self.waitEnd is None:
self.waitEnd.wait();
def SetStopEvent(self):
if not self.waitEnd is None:
self.waitEnd.set();
self.alive = False;
self.stop();
def start(self):
self.l_serial = serial.Serial();
self.l_serial.port = self.port;
self.l_serial.baudrate = 9600;
self.l_serial.timeout = 2;
self.re_num = re.compile('\d');
try:
if not self.output is None:
self.output.WriteText(u'打開通訊端口\r\n');
if not self.log is None:
self.log.info(u'打開通訊端口');
self.l_serial.open();
except Exception, ex:
if self.l_serial.isOpen():
self.l_serial.close();
self.l_serial = None;
if not self.output is None:
self.output.WriteText(u'出錯:\r\n %s\r\n' % ex);
if not self.log is None:
self.log.error(u'%s' % ex);
return False;
if self.l_serial.isOpen():
if not self.output is None:
self.output.WriteText(u'創(chuàng)建接收任務\r\n');
if not self.log is None:
self.log.info(u'創(chuàng)建接收任務');
self.waitEnd = threading.Event();
self.alive = True;
self.thread_read = None;
self.thread_read = threading.Thread(target=self.FirstReader);
self.thread_read.setDaemon(1);
self.thread_read.start();
return True;
else:
if not self.output is None:
self.output.WriteText(u'通訊端口未打開\r\n');
if not self.log is None:
self.log.info(u'通訊端口未打開');
return False;
def InitHead(self):
#串口的其它的一些處理
try:
time.sleep(3);
if not self.output is None:
self.output.WriteText(u'數(shù)據(jù)接收任務開始連接網(wǎng)絡(luò)\r\n');
if not self.log is None:
self.log.info(u'數(shù)據(jù)接收任務開始連接網(wǎng)絡(luò)');
self.l_serial.flushInput();
self.l_serial.write('\x11');
data1 = self.l_serial.read(1024);
except ValueError,ex:
if not self.output is None:
self.output.WriteText(u'出錯:\r\n %s\r\n' % ex);
if not self.log is None:
self.log.error(u'%s' % ex);
self.SetStopEvent();
return;
if not self.output is None:
self.output.WriteText(u'開始接收數(shù)據(jù)\r\n');
if not self.log is None:
self.log.info(u'開始接收數(shù)據(jù)');
self.output.WriteText(u'===================================\r\n');
def SendData(self, i_msg):
lmsg = '';
isOK = False;
if isinstance(i_msg, unicode):
lmsg = i_msg.encode('gb18030');
else:
lmsg = i_msg;
try:
#發(fā)送數(shù)據(jù)到相應的處理組件
pass
except Exception, ex:
pass;
return isOK;
def FirstReader(self):
data1 = '';
isQuanJiao = True;
isFirstMethod = True;
isEnd = True;
readCount = 0;
saveCount = 0;
RepPos = 0;
#read Head Infor content
self.InitHead();
while self.alive:
try:
data = '';
n = self.l_serial.inWaiting();
if n:
data = data + self.l_serial.read(n);
#print binascii.b2a_hex(data),
for l in xrange(len(data)):
if ord(data[l])==0x8E:
isQuanJiao = True;
continue;
if ord(data[l])==0x8F:
isQuanJiao = False;
continue;
if ord(data[l]) == 0x80 or ord(data[l]) == 0x00:
if len(data1)10:
if not self.re_num.search(data1,1) is None:
saveCount = saveCount + 1;
if RepPos==0:
RepPos = self.output.GetInsertionPoint();
self.output.Remove(RepPos,self.output.GetLastPosition());
self.SendData(data1);
data1 = '';
continue;
except Exception, ex:
if not self.log is None:
self.log.error(u'%s' % ex);
self.waitEnd.set();
self.alive = False;
def stop(self):
self.alive = False;
self.thread_read.join();
if self.l_serial.isOpen():
self.l_serial.close();
if not self.output is None:
self.output.WriteText(u'關(guān)閉通迅端口:[%d] \r\n' % self.port);
if not self.log is None:
self.log.info(u'關(guān)閉通迅端口:[%d]' % self.port);
def printHex(self, s):
s1 = binascii.b2a_hex(s);
print s1;
#測試用部分
if __name__ == '__main__':
rt = ReadThread();
f = open("sendport.cfg", "r")
rt.sendport = f.read()
f.close()
try:
if rt.start():
rt.waiting();
rt.stop();
else:
pass;
except Exception,se:
print str(se);
if rt.alive:
rt.stop();
print 'End OK .';
del rt;
用ser.isOpen()查看返回False,說明ser.close()起作用了啊。用管理員身份打開cmd,再執(zhí)行腳本試試?