Modbus 協議傳輸的資料一般只有基本的 U16 型態,但在實際應用上可能因裝置不同而有不同的應用,例如長整數、浮點數,但又因為硬體 CPU/MCU/OS 實際狀況而有位元順序的問題,底下是 python 轉換函式…
def Convert( theType, values ):
if( theType == 'FLOAT(ABCD)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[0],tobytes[1],tobytes[2],tobytes[3])
set_value = struct.unpack('>f', packit)[0]
elif( theType == 'FLOAT(CDAB)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[2],tobytes[3],tobytes[0],tobytes[1])
set_value = struct.unpack('>f', packit)[0]
elif( theType == 'FLOAT(BADC)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[1],tobytes[0],tobytes[3],tobytes[2])
set_value = struct.unpack('>f', packit)[0]
elif( theType == 'FLOAT(DCBA)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[3],tobytes[2],tobytes[1],tobytes[0])
set_value = struct.unpack('>f', packit)[0]
elif( theType == 'UINT32(ABCD)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[0],tobytes[1],tobytes[2],tobytes[3])
set_value = struct.unpack('>L', packit)[0]
elif( theType == 'UINT32(CDAB)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[2],tobytes[3],tobytes[0],tobytes[1])
set_value = struct.unpack('>L', packit)[0]
elif( theType == 'UINT32(BADC)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[1],tobytes[0],tobytes[3],tobytes[2])
set_value = struct.unpack('>L', packit)[0]
elif( theType == 'UINT32(DCBA)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[3],tobytes[2],tobytes[1],tobytes[0])
set_value = struct.unpack('>L', packit)[0]
elif( theType == 'INT32(ABCD)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[0],tobytes[1],tobytes[2],tobytes[3])
set_value = struct.unpack('>l', packit)[0]
elif( theType == 'INT32(CDAB)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[2],tobytes[3],tobytes[0],tobytes[1])
set_value = struct.unpack('>l', packit)[0]
elif( theType == 'INT32(BADC)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[1],tobytes[0],tobytes[3],tobytes[2])
set_value = struct.unpack('>l', packit)[0]
elif( theType == 'INT32(DCBA)' ):
packit = struct.pack('>hh',values[0],values[1])
tobytes = struct.unpack('>4c', packit)
packit = struct.pack('>4c',tobytes[3],tobytes[2],tobytes[1],tobytes[0])
set_value = struct.unpack('>l', packit)[0]
elif( theType == 'UINT16(AB)' ):
packit = struct.pack('>h',values[0])
tobytes = struct.unpack('>2c', packit)
packit = struct.pack('>cc',tobytes[0],tobytes[1])
set_value = struct.unpack('>H', packit)[0]
elif( theType == 'UINT16(BA)' ):
packit = struct.pack('>h',values[0])
tobytes = struct.unpack('>2c', packit)
packit = struct.pack('>cc',tobytes[1],tobytes[0])
set_value = struct.unpack('>H', packit)[0]
elif( theType == 'INT16(AB)' ):
packit = struct.pack('>h',values[0])
tobytes = struct.unpack('>2c', packit)
packit = struct.pack('>cc',tobytes[0],tobytes[1])
set_value = struct.unpack('>h', packit)[0]
elif( theType == 'INT16(BA)' ):
packit = struct.pack('>h',values[0])
tobytes = struct.unpack('>2c', packit)
packit = struct.pack('>cc',tobytes[1],tobytes[0])
set_value = struct.unpack('>h', packit)[0]
else:
set_value = values[0]
return set_value
#Modbus 讀值可能是 2byte 也可能是 4byte
values = [-4370,-8756]
Convert( 'FLOAT(ABCD)' ,values )
Convert( 'FLOAT(DCBA)' ,values )
Convert( 'FLOAT(BADC)' ,values )
Convert( 'FLOAT(CDAB)' ,values )
Convert( 'UINT32(ABCD)' ,values )
Convert( 'UINT32(DCBA)' ,values )
Convert( 'UINT32(BADC)' ,values )
Convert( 'UINT32(CDAB)' ,values )
Convert( 'INT32(ABCD)' ,values )
Convert( 'INT32(DCBA)' ,values )
Convert( 'INT32(BADC)' ,values )
Convert( 'INT32(CDAB)' ,values )
Convert( 'UINT16(AB)' ,values )
Convert( 'INT16(AB)' ,values )
Convert( 'INT16(BA)' ,values )
Convert( 'UINT16(BA)' ,values )
2019年3月15日 星期五
2019年1月23日 星期三
Holtek 使用 HSI 的方式
Holtek 使用 HSI 的方式…
以 HT32f2220 為例
從 project_template 建立專案的話,修改底下 define …
在 system_ht32f5xxxx_02.c 中…
#define HSI_ENABLE (1) /*!< 0: DISABLE, 1: ENABLE */
#define HSE_ENABLE (0) /*!< 0: DISABLE, 1: ENABLE */
#define LSI_ENABLE (1) /*!< 0: DISABLE, 1: ENABLE */
#define LSE_ENABLE (0) /*!< 0: DISABLE, 1: ENABLE */
#define PLL_ENABLE (1) /*!< 0: DISABLE, 1: ENABLE */
#define PLL_CLK_SRC (1) /*!< 0: HSE, 1: HSI */
以 HT32f2220 為例
從 project_template 建立專案的話,修改底下 define …
在 system_ht32f5xxxx_02.c 中…
#define HSI_ENABLE (1) /*!< 0: DISABLE, 1: ENABLE */
#define HSE_ENABLE (0) /*!< 0: DISABLE, 1: ENABLE */
#define LSI_ENABLE (1) /*!< 0: DISABLE, 1: ENABLE */
#define LSE_ENABLE (0) /*!< 0: DISABLE, 1: ENABLE */
#define PLL_ENABLE (1) /*!< 0: DISABLE, 1: ENABLE */
#define PLL_CLK_SRC (1) /*!< 0: HSE, 1: HSI */
再直接使用 example 中的 SysClockConfig_HSI() 函式進入 HIS 模式即可。
這樣 XTALIN、XTALOUT 就能拿來當 gpio 腳了。
2018年11月21日 星期三
Debian Security Bug Tracker
檢查弱點分析的方式
到…
Debian Security Bug Tracker
https://security-tracker.debian.org/tracker/
查詢弱點…
譬如 CVE-2017-3169
Debian / Raspberry Pi 檢查版本
dpkg -l apache2
比較版本號是否有修正此 bug
到…
Debian Security Bug Tracker
https://security-tracker.debian.org/tracker/
查詢弱點…
譬如 CVE-2017-3169
Debian / Raspberry Pi 檢查版本
dpkg -l apache2
比較版本號是否有修正此 bug
2018年7月20日 星期五
備份 Raspberry Pi 時SD卡容量不相符的解決方式
備份回復 rpi image 的時候有時會遇到 sd card 雖號稱一樣 g 數,但實際大小卻不一樣,導致回復時出問題,此時可以用底下 scripts 解決,先把檔案變小就好解決… (linux 環境下)
Shrink your Raspberry Pi SD Card Image size
https://github.com/Drewsif/PiShrink
Very Nice !!
硬體從 rpi2 改到 rpi3
舊 SDCard 無法順利在 rpi3 開機的問題
apt-get install rpi-update
rpi-update
Shrink your Raspberry Pi SD Card Image size
https://github.com/Drewsif/PiShrink
Very Nice !!
硬體從 rpi2 改到 rpi3
舊 SDCard 無法順利在 rpi3 開機的問題
apt-get install rpi-update
rpi-update
2018年7月1日 星期日
mqtt@RaspberryPi
broker / server / client library install...
sudo apt-get install mosquitto mosquitto-clients
sudo apt-get install python3-pip
sudo pip3 install paho-mqtt
import paho.mqtt.publish as publish
# publish a message then disconnect.
host = "localhost"
topic = "where/is/my/topic"
payload = "hello"
# If broker asks user/password.
auth = {'username': "", 'password': ""}
# If broker asks client ID.
client_id = ""
publish.single(topic, payload, qos=1, hostname=host)
#publish.single(topic, payload, qos=1, host=host, auth=auth, client_id=client_id)
# 安裝 broker 及 所需函式庫
sudo apt-get install mosquitto mosquitto-clients
sudo apt-get install python3-pip
sudo pip3 install paho-mqtt
fix python search path...
# poah 安裝路徑若 python 找不到要作修正
cd /usr/lib/python2.7/dist-packages
sudo ln -s /usr/local/lib/python3.5/dist-packages/paho
import paho.mqtt.client as mqtt
sudo ln -s /usr/local/lib/python3.5/dist-packages/paho
subscribe...
# Subscribe 訂閱訊息/接收訊息
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("where/is/my/topic")
print("Connected with result code "+str(rc))
client.subscribe("where/is/my/topic")
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
print(msg.topic+" "+str(msg.payload))
client = mqtt.Client()
client.on_connect = on_connect #call back function
client.on_message = on_message #call back function
client.connect("localhost", 1883, 60)
client.loop_forever()
client.on_connect = on_connect #call back function
client.on_message = on_message #call back function
client.connect("localhost", 1883, 60)
client.loop_forever()
pubilsh...
# Publisher 發佈訊息
import paho.mqtt.publish as publish
# publish a message then disconnect.
host = "localhost"
topic = "where/is/my/topic"
payload = "hello"
# If broker asks user/password.
auth = {'username': "", 'password': ""}
# If broker asks client ID.
client_id = ""
publish.single(topic, payload, qos=1, hostname=host)
#publish.single(topic, payload, qos=1, host=host, auth=auth, client_id=client_id)
2018年5月9日 星期三
Raspberry pi VLAN
Raspberry pi 3 僅一個 RJ45 port,若要當二個實體 ip 用,需建立 VLAN 虛擬網卡
二張網卡在不同子網域 A&B,A是主要網卡,B是延伸的虛擬網卡
A:IP=192.168.254.31
B:IP=192.168.1.31
其中一個 ip 必須能與其它裝置建立 UDP 通訊,且此裝置無法進行進一步的網路設置
注意!! UDP 是透過 python socket 的 create_udp_socket() 來建立連線,界面 A 必須建立在 UDP 所要通訊的網址上,否則無法正常通訊 why? route issues?
一、建立二張網卡界面…
sudo nano /etc/network/interfaces
iface eth0 inet manual
# VLAN Interface
auto eth0.1
iface eth0.1 inet manual
vlan-raw-device eth0
二、設定二張網卡 ip / router …
sudo nano /etc/dhcpcd.conf
#A-->B-->
#A
interface eth0
static ip_address=192.168.254.31/16
static routers=192.168.1.31
static domain_name_servers=xxx.xxx.xxx.xxx
#B
# Static IP configuration for VLan
interface eth0.1
static ip_address=192.168.1.31/16
static routers=192.168.1.72
static domain_name_servers=xxx.xxx.xxx.xxx
2018年3月20日 星期二
Make Raspberry pi as a Bluetooth Peripheral Device
install nodejs npm
sudo apt-get install nodejs npm
install nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
source ~/.bashrc
nvm install stable
install bleno
npm install bleno
example code…
http://www.skyrise.tech/blog/tech/bluetooth-raspberry-pi-bleno-part-1-ibeacon/
https://github.com/noble/bleno/blob/master/test.js
run…
node app.js
run BLE App @ ur phone
what's next ?
訂閱:
文章 (Atom)
VirtualBox 空間減肥
sdelete64 -z c: VBoxManage modifymedium disk "/Users/fellow/VirtualBox VMs/Win10/Win10.vdi" --compact *.vdi 路徑可以在 VirtualBox 儲...
-
https://github.com/ljean/modbus-tk/ install pip… sudo apt-get install python-pip install... download modbus_tk-x.x.x.tar.gz tar zxvf...
-
set0 bit 位元設0 set1 bit 位元設1 tog bit 位元0/1切換 swapc bit 位元與c切換 =================== call addr 呼叫副程式 goto addr 跳躍到標籤 icall [index] 呼叫指標指到的副程式 ig...
-
rails blog -d mysql 建立一個名為 blog 的網站app,使用 mysql cd blog 進去 gen 好的網站 config/database.yml 編輯資料庫設定 rake db:create 自動建立資料庫 ruby script/generate ...