2017年8月23日 星期三

Python 入門學習快速筆記 (六)


例外處理 try: ... excpet:



a=100
b=10

try:
    c=a/b
    print("c=",c)
except:
    c=-1
    print("Divdie by zero")
    print("c=",c)

print("c=",c)

------------------
try-except 也可以和 else 連用, else 後的程式區塊放的是沒有發生例外,程式所執行的工作,例如
try:
    c=a/b
    print("c=",c)
except:
    c=-1
    print("Divdie by zero")
    print("c=",c)
else:
    print("No exception")
-----------------------------------------------
例外的名稱也可以寫在 except 之後,如
try:
    c=a/b
    print("c=",c)
except ZeroDivisionError:
    c=-1
    print("Divdie by zero")
    print("c=",c)
except NameError:
except TypeError:
except IndexError:

-----------------------------
若加入另一個關鍵字 finally ,無論例外有沒有發生都會執行 finally 後的程式區塊
try:
    c=a/b
    print("c=",c)
except ZeroDivisionError:
    c=-1
    print("Divdie by zero")
    print("c=",c)
except NameError:
except TypeError:
except IndexError:
findally:

--------------------------

Raise 觸發Exception Error


try:
raise NameError
     
except NameError:
        print("發生例外,NameError")
except ZeroDivisionError:
print("發生例外,ZeroDivisionError")


print("after exception....")

Python 所有Exception
https://docs.python.org/3/library/exceptions.html#exception-hierarchy



Python檔案處理


file = open('dream.txt', 'r', encoding='UTF-8')

li=file.readlines()
print(li[3],end='')



Python 第三方套件



https://pypi.python.org/pypi?:action=browse&c=533&show=all

安裝 Python 第三方套件-使用pip 工具
C:\Python34\Scripts>pip install pyqrcode

Collecting pyqrcode
  Downloading PyQRCode-1.1.tar.gz
Building wheels for collected packages: pyqrcode
  Running setup.py bdist_wheel for pyqrcode
  Stored in directory: C:\Users\student\AppData\Local\pip\Cache\wheels\03\93\7f\
d3a38165d01798524d99329a6aabba2b378d81da1d46f15b36
Successfully built pyqrcode
Installing collected packages: pyqrcode
Successfully installed pyqrcode-1.1

C:\Python34\Scripts>pip install pypng


#-*-coding:UTF-8 -*-
#  EX08_01.py
#  
#  pyqrcode 使用範例
#  
from pyqrcode import QRCode
url = QRCode('http://www.ntu.edu.tw')
#輸出SVG格式檔案:url.svg ,縮放比為10
url.svg('url.svg', scale=10)
#輸出PNG格式檔案:url.png ,縮放比為10

url.png('url.png', scale=10)






PT100 溫度感測器



電阻系數

對於一般物體,電阻R,電阻率p、長度 L 與截面積A之間的關係如下:
在上式中,
  • 電阻 R單位為歐姆
  • 長度 L單位為米
  • 截面面積 A 單位為平方米
  • 電阻率 p 單位為歐姆·米
這個方程被稱為電阻定律。

由於 p 很小, 所以A 導線截面積要很小, 才會有R (電阻)可以量, 不然沒有R的訊號

電阻與溫度的關係

電阻率一般會隨溫度變化而變化。在溫度變化不大時,電阻率p與溫度之間存在線性關係:
pt=p0(1+aT)
其中 P0是該材料在0攝氏度時的電阻率,Pt是T攝氏度時的電阻率,a是電阻率的溫度係數。 a在溫度變化不大的範圍內可以認為是常量。多數金屬常溫下的電阻率溫度係數a 約為0.004。 

PT100


大部分電阻式溫度感測 器是以金屬作成的,其中以鉑(Pt)作成的電阻式溫度檢測器,最為穩定-耐酸堿、不會變 質、相當線性...,最受工業界採用。  

PT100 溫度感測器是一種以鉑(Pt) (俗稱白金)作成的電阻式溫度感測器,屬於正電阻係數,其電阻和 溫度變化的關係式如下:R=Ro(1+αT)     其中α=0.00392,Ro 為 100Ω (在 0℃的電阻值),T 為攝氏溫度,因此白金作成的電阻式溫度感測器,又稱為 PT100

PT100 溫度感測器 0℃時電阻值為 100Ω,電阻變化率為 0.3851Ω/℃。

pt100 的積體電路,要注意 的是這個積體電路採集的不是電流信號,而是電阻值。

PT100 原理及分度表 電阻式溫度感測器(RTD,Resistance Temperature Detector)-一種物質材料作成的電阻,它 會隨溫度的上升而改變電阻值,如果它隨溫度的上升而電阻值也跟著上升就稱為正電阻 係數,如果它隨溫度的上升而電阻值反而下降就稱為負電阻係數。

參考:

http://www.dmatek.com.tw/tn/download/SensorData_E390.pdf


2017年8月17日 星期四

嵌入式系統常用的通訊協定-RS485/Modbus



RS485:
  • 為半雙工(同一時間只能選擇傳送或接收的動作)
  • 為差動傳輸,使用纜線兩端(D+、D-)的電壓差來表示傳遞訊號
  • 常用於單機傳送(Master),多機接收(Slave)的通訊鏈結。可將數個感應裝置的數值回傳給Master
Modbus over RS485:
  • Modbus是一種串列通訊協定
  • Modbus為master/slave架構。一個裝置為Master節點,其他節點為slave。
  • 每一個slave裝置都有一個唯一的位址/ID (1~247)
  • Since a single byte is normally used to define the slave address and each slave on a network requires a unique address, the number of slaves on a network is limited to 256. The limit defined in the modbus specification is even lower at 247.
  • master和slave之間走的Modbus 協定其封包格式有分ASCII 或RTU兩種
  • ASCII 為文字模式, 用character 傳送, RTU 為binary 模式傳送
  • 封包格式:   Slave ID  | Function Code | Data | CRC
RTU 訊息格式
  • Information is stored in the Slave device in four different tables.
  • Two tables store on/off discrete values (coils) and two store numerical values (registers). The coils and registers each have a read-only table and read-write table.
  • Each table has 9999 values.
    Each coil or contact is 1 bit and assigned a data address between 0000 and 270E.
    Each register is 1 word = 16 bits = 2 bytes and also has data address between 0000 and 270E.

Coil/Register Numbers
Data Addresses
TypeTable Name
1-9999
0000 to 270E(9999)
Read-WriteDiscrete Output Coils
10001-19999
0000 to 270E
Read-OnlyDiscrete Input Contacts
30001-39999
0000 to 270ERead-OnlyAnalog Input Registers
40001-49999
0000 to 270ERead-WriteAnalog Output Holding Registers

Coil/Register Numbers can be thought of as location names since they do not appear in the actual messages. The Data Addresses are used in the messages.For example, the first Holding Register, number 40001, has the Data Address 0000. The difference between these two values is the offset.
Each table has a different offset. 1, 10001, 30001 and 40001.

只有Data Address(Offset)才出現在送出的Message 中, 如何知道查的是那一張表呢?
答案是利用Function Code 來決定


What is a function code?

The second byte sent by the Master is the Function code. This number tells the slave which table to access and whether to read from or write to the table.

Function Code (Command) 3 is to read 4xxx registers, and  4 for 3xxx registers. 

Function CodeActionTable Name
01 (01 hex)ReadDiscrete Output Coils
05 (05 hex)Write singleDiscrete Output Coil
15 (0F hex)Write multipleDiscrete Output Coils
02 (02 hex)ReadDiscrete Input Contacts
04 (04 hex)ReadAnalog Input Registers
03 (03 hex)ReadAnalog Output Holding Registers
06 (06 hex)Write singleAnalog Output Holding Register
16 (10 hex)Write multipleAnalog Output Holding Registers

Example: 


Read Holding Registers (FC=03)
Request
This command is requesting the content of analog output holding registers # 40108 to
 40110 from the slave device with address 17.
11 03 006B 0003 7687
11: The Slave Address (11 hex = address17 )
03: The Function Code 3 (read Analog Output Holding Registers)
006B: The Data Address of the first register requested.
             ( 006B hex = 107 , + 40001 offset = input #40108 )
0003: The total number of registers requested. (read 3 registers 40108 to 40110)
7687: The CRC (cyclic redundancy check) for error checking.
Response
11 03 06 AE41 5652 4340 49AD
11: The Slave Address (11 hex = address17 )
03: The Function Code 3 (read Analog Output Holding Registers)
06: The number of data bytes to follow (3 registers x 2 bytes each = 6 bytes)
AE41: The contents of register 40108
5652: The contents of register 40109
4340: The contents of register 40110
49AD: The CRC (cyclic redundancy check).


Read Coil Status (FC=01)

Request
This command is requesting the ON/OFF status of discrete coils # 20 to 56
from the slave device with address 17.
11 01 0013 0025 0E84

11: The Slave Address (11 hex = address17 )
01: The Function Code 1 (read Coil Status)
0013: The Data Address of the first coil to read.
             ( 0013 hex = 19 , + 1 offset = coil #20 )
0025: The total number of coils requested.  (25 hex = 37,  inputs 20 to 56 )
0E84: The CRC (cyclic redundancy check) for error checking.
Response
11 01 05 CD6BB20E1B 45E6
11: The Slave Address (11 hex = address17 )
01: The Function Code 1 (read Coil Status)
05: The number of data bytes to follow (37 Coils / 8 bits per byte = 5 bytes)
CD: Coils 27 - 20 (1100 1101)
6B: Coils 35 - 28 (0110 1011)
B2: Coils 43 - 36 (1011 0010)
0E: Coils 51 - 44 (0000 1110)
1B: 3 space holders & Coils 56 - 52 (0001 1011)
45E6: The CRC (cyclic redundancy check).
The more significant bits contain the higher coil variables. This shows that coil 36 is off (0) and 43 is on (1). Due to the number of coils requested, the last data field1Bcontains the status of only 5 coils.  The three most significant bits in this data field are filled in with zeroes.





RS485 模組 (MAX485)的Pin 腳定義:


  • pin 1 :RO (receive out)
  • pin 2 RE (receive enable) 
  • pin 3 :DE (data enable) 
  • pin 4 :DI (data in)
  • pin 5, pin 8 :  Gnd  and Vcc onnected
  • pin 6,7 : A and B  the RS485 pair

RO------->UART TX
DI<------- UART RX

把/RE and DE 短路, 然後MCU/Pi 用一根GPIO 決定 /RE 或 DE.(即是決定是在送還是在收的狀態, 因為RS485是單工)















讀出來若是以Float 表示, 則需進行轉換成十進制

須考慮 Byte ordering的問題!

http://www.61131.com/download.htm






 AQUAS-SR04

2017年8月9日 星期三

凸函數-Convex Function




凸集是指有這麼一個點的集合,其中任取兩個點連一條直線,這條線上的點仍然在這個集合內部,因此說「凸」是很形象的。例如下圖,對於凸函數(在數學表示上,滿足約束條件是仿射函數,也就是線性的Ax+b的形式)來說,局部最優就是全局最優,但對非凸函數來說就不是了。二次表示目標函數是自變量的二次函數。4




原文網址:https://read01.com/BQ45kj.html



2017年8月8日 星期二

[新手教學] Linux Driver 數位課程學習須知



 教學影片內容為Linux Driver 觀念解說與各Linux Driver 技術主題的說明並有講師Demo Pi 3影片


先備知識: 

  1. 具Linux 系統操作能力
  2. 熟悉的C程式設計能力
  3. 會使用gcc, make 的等GNU開發工具
  4. 有Linux 系統程式設計的經驗
Raspberry Pi 3 環境建置:


請先登入學習平台,在[課程公告]處, 點選下載 Pi3_driver_shield3_image_20161223_16G.rar, 此為艾鍗 Linux Driver 上課用 image

請先解開 rar 壓縮檔, 然後再用WinDisk工具寫入至 16G MicroSD 卡中

開機後, root登入, 密碼: 123456

2017年8月7日 星期一

常常弄不清楚的 CSS Selector



CSS class definition with multiple identifiers

常常弄不清楚的 CSS Selector
  • .class1.class2 will match only the elements that have both of them classes defined.
    .class1.class2 { background: red; }
    <div class="class1 class2"></div>
  • .class1, .class2 will match the elements with .class1 or .class2
    .class1, .class2 { background: yellow; }
    <div class="class1"></div>
    <div class="class2"></div>
  • .class1 .class2 will match only the elements with class2 within elements with class1.
    .class1 .class2 { background: blue; }
    <div class="class1">
        <div class="class2"></div>
    </div>

善用工具: 

開Chrome瀏覽器 F12 開發者工具偵察現有的網頁 CSS 樣式規則,判斷產生該效果的 CSS 樣式設定來源



2017年8月6日 星期日

Pi 新手教學(三) ---使用SSH登入Raspberry Pi




使用SSH登入Raspberry Pi


  1. 先碓認 Pi 和 PC之間網路必須能連通, 可以用 ping <IP位址>
  2. 同時再確認: Pi 本身的SSH 服務必須是開啓的 (一般來說應該都是開啓的), 可以使用 netstat -atn 查看是否SSH 處於listen 狀態 (tcp port: 22)



  Note: 官方最新版的Image預設是沒有SSH 啓動的,所以得自己去開啓它


HTML Color Codes


HTML Color Codes

1在CSS中常常須要設定顏色, 例如要設定 background-color: #4CAF50; 常常須要RGB色碼, 可以到這裡去找快速查詢





2) 若是想要知道某一個圖所用的"顏色", 不知道色碼時怎麼辦 ? 可以先 [Print Screen] , ,再用小畫家, 用"點滴"查看color  







2017年8月1日 星期二

About BBC Micro: bit


The Micro Bit (also referred to as BBC Micro Bit, stylized as micro:bit) is an ARM-based embedded system designed by the BBC for use in computer education in the UK.

One of the main aims of the micro:bit is to “help a new generation get creative with technology and take advantage of the opportunities a career in science, technology, engineering and maths offers”.

The micro:bit, measuring just 4cm by 5cm, is a stripped down computer which children can use to code and create anything they set their minds to! It’s intended as a starter device to give children a basic introduction to physical computing and tinkering, so that they can move on to using more advanced devices such as an Arduino or Raspberry Pi.




Key features:

  • A display consisting of 25 red LEDs;
  • Two programmable buttons;
  • An on-board motion sensor or “accelerometer”;
  • A built-in compass or “magnetometer”;
  • Bluetooth Smart Technology;
  • Three input and output (I/O) rings.





Spec:


  • Nordic nRF51822 Multi-protocol Bluetooth® 4.0 low energy/2.4GHz RF SoC
    • 32-bit ARM Cortex M0 processor (16MHz)
    • 16kB RAM
    • 256kB Flash
    • Bluetooth Low Energy Master/Slave capable
  • Input/Output
    • 25 LED Matrix
    • Freescale MMA8652 3-axis Accelerometer
    • Freescale MAG3110 3-axis Magnetometer (e-compass)
    • Push Button x2
    • USB and Edge connector Serial I/O
    • 2/3 reconfigurable PWM outputs
    • 5 x Banana/Croc-clip connectors
    • Edge connector
    • 6 x Analog In
    • 6-17 GPIO (configuration dependent)
    • SPI
    • i2c

There are four official code editors in the BBC micro:bit web site