2020年12月23日 星期三

[PLC] For Next 測試

傳統的程式語言中,For ... Next 是基本的迴圈敘述,在 PLC 中則屬於應用指令。底下做一個簡單測試,計算從 1 加到 10 是多少。

PLC 跟傳統語言不一樣的地方,由於每一個  statement 都必須有執行條件,因此 FOR 迴圈裡面的每一個 statement 前面放了 LD X1 指令,以便用按鍵來觀察計算結果。

 


 

 

2020年12月17日 星期四

[PLC] FX3U-485-BD 簡單 modbus 測試

使用價格較便宜的 FX3U-485-BD 發送 modbus 封包至遠端繼電器 (9600, E81, modbus addr#1), 使用 Function 5 控制燈泡, 這是查閱 MITSUBISHI_FX3U-Users-Manual-Data-Communications-Networking (JY997D16901C), Non-Protocol Communication (RS/RS2 Instruction) 的範例改的, 就不多解釋了

 操作影片:

 階梯圖: 

2020年12月15日 星期二

[PLC] 計時器/循環 design pattern

計時器 T 在循環控制的基本迴圈 design pattern

觀念上, 傳統的 Timer, 通電前 B 接點導通, 通電後開始計時, 計時終了換成 A 接點導通

本例中, X0 為啟動按鈕, X1 為停止按鈕, 使用 M0 進行自鎖, 要控制 8 個 Y 輸出, 用到 T0 ~ T7 共 8 個計時器, 依 T0 ~ T7 順序啟動, 等於傳統配電盤上串接在一起, 且 T7 的 B 接點用來串接在整個電路之前, 當 T7 計時終了,  B 接點斷開, 瞬間切斷整個計時迴路, T7 本身也斷電, 因此 T7 B 接點又馬上重新接上, 使得 T0 從頭開始計時.

操作影片:


階梯圖:

2020年11月24日 星期二

[JavaScript] 練習使用 fetch

 index.html

<!DOCTYPE html>
<html>
  <head>
    <title>Code or Nocode</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script>
      function getAnswer() {
        const ul = document.getElementById('answer'); 
        const url = 'http://localhost/test/answer.html'
        fetch(url)
          .then((resp=> resp.json())
          .then(function(data) {
            ul.innerText = "Answer="+data.answer
        })
        setTimeout(getAnswer1000);
      }
    </script>
  </head>
  <body>
    <h1 id="answer">Answer=</h1>
    <script>
      setTimeout(getAnswer1000);
    </script>
  </body>
</html>

 

answer.html

{"answer":"3"}

 

2020年10月11日 星期日

MikroTik RouterOS 設定 NAT

WebFig進入點: IP>FireWall>NAT頁面

Chain: dstnt
Protocol: 6(tcp)
Dst. Port: AAAA-BBBB
Dst.Address type: local
Action: dst-nat
To Address: 192.168.88.NNN
To Port:AAAA-BBBB

2020年10月1日 星期四

[ESP32] Using the MFRC522 RFID reader and SD card reader

Today some guy in the Facebook proposed an interesting problem about how to use the RFID and the SD in ESP32s .  Both peripheral devices used the SPI connections. The point is they need it's own CS (chip select) pin to enable the SPI (MOSI/MISO/CLK) bus. Only one of them can be enabled in the same time because the enabled device will occupy the SPI bus.

Uusing the original SD ReadWrite code of the arduino example, we could find it's failed even if the CSs are defined well. To see what happened, I checked the compiler messages, as the following:

Use library SPI、verison 1.0,in folder:C:\Users\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SPI
Use library SD、version 1.0.5,in follder:C:\Users\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\SD
Use library FS、version 1.0,in folder:C:\Users\user\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.4\libraries\FS

From the view point of the software architecture, the SD layer is stacked above the FS which is stacked on the SPI hearware driver. The above messages showed that ESP32 SD library was applied, which caused the original SD ReadWrite code to be failed. 

To solve the problem, some library definition are to be modified as the follows.

2020年7月11日 星期六

C# WebRequest 連接 https 發生 SSL 錯誤

這個問題在 .Net 4.5 以前無法自動切換至 SSL, 問題在於憑證的驗證, 網路上有許多做法, 不過現在最簡單的方法, 就是改成 .Net 4.6以上的版本就可以了,  預設使用 TLS1.2, 如果網站 TLS 版本不符, 用 ServicePointManager 指定版本就好

https://docs.microsoft.com/zh-tw/dotnet/framework/network-programming/tls

2020年7月8日 星期三

設定Windows排程器執行powershell script




在Windows排程器中編輯動作:

程式或指令碼: powershell
新增引數: -ExecutionPolicy Bypass -File "C:\RmtRuntime\rmt_zip_daily.ps1"




這裡必須指定 -ExecutionPolicy Bypass 才可執行

以下為 rmt_zip_daily.ps1 的內容
--------------------------------------------
#REQUIRES -Version 2

$ZipDate = [DateTime]::Today.AddDays(-1).ToString("yyyy-MM-dd")

$SourceFile = "D:\TempRMT\Source2020\$ZipDate.zip"
if (Test-Path $SourceFile) {
  Remove-Item $SourceFile
}
7z a $SourceFile  "E:\RMT Processing\Source\$ZipDate"
copy $SourceFile  "\\NAS01\Temp\Backup\Google\RMT Processing\Source2020\$ZipDate.zip"



$Source20sFile = "D:\TempRMT\Source2020-20s\$ZipDate.zip"
if (Test-Path $Source20sFile) {
  Remove-Item $Source20sFile
}
7z a $Source20sFile "E:\RMT Processing\Source20s\$ZipDate"
copy $Source20sFile "\\NAS01\Temp\Backup\Google\RMT Processing\Source2020-20s\$ZipDate.zip"

2020年6月28日 星期日

[tensorflow] 在v2下使用v1

tenserflow 2版之後有許多功能已經跟 v1不相容, 幸好 v2 包含了 v1 的方法, 只是改了類別名稱, 如果原有 v1 的程式碼想要在 v2 環境使用, 只要照以下 import 方式就可以繼續使用原 v1 程式碼:

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior() 

2020年6月16日 星期二

2020年4月13日 星期一

[python] 咖啡豆去背

咖啡豆辨識, 先設計打光場景, 背景用單色, 先用HSV色彩過濾得到一個初步遮罩, 再用 morphology處理遮罩, 把遮罩內的洞補起來, 並且縮小邊緣, 以獲得完全不含背景色的圖片

import cv2
import numpy as np

pic = 'pic/b2.jpg'
img=cv2.imread(pic,cv2.IMREAD_COLOR)

hsv=cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
lower_coffee = np.array([0,0,0])
upper_coffee = np.array([60,255,180])

mask = cv2.inRange(hsv, lower_coffee, upper_coffee)
kernel1 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(13,13))
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(15,15))
   
#mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.dilate(mask ,kernel1)
mask = cv2.erode(mask, kernel2)

out = cv2.bitwise_and(img, img, mask= mask)

cv2.imshow("img",img)
cv2.imshow('mask',mask)
cv2.imshow("out",out)

cv2.waitKey(0)
cv2.destroyAllWindows()


原始圖:















遮罩:















結果:





2020年3月10日 星期二

[python] google map API

最近熱門的藥局領口罩, 有公開的CSV可以下載, 用python簡單就可抓取了, 觀察了一下資料,更新速度還蠻快的, 但是有住址而無GPS座標, 如果要用地圖, 需要一個轉換, 一次轉換那麼多點要花很多錢, 最好是用cache的方式, 有抓到就先存起來, 要查詢時先去cache找, 沒有再去查詢.

2020年3月5日 星期四

使用 Synology 下載證交所交易資料

在前一篇安裝 pip3 後, 即可 ssh 進入來執行 python 程式. 本篇使用 python 來下載證交所資料, 先安裝 package 供未來使用:

# pip3 install lxml pymysql httplib2 wget pandas requests html5lib

以下是程式碼

[Synology] 安裝 pip3

為了將 NAS 直接當成 IOT 主機, 除了安裝 Serial Driver, 若要用 python 進行資料分析, 需要安裝其他 package, 但 Synology NAS 目前無法在終端機下直接呼叫 pip 來進行安裝. 參考這篇的內容, 提到啟用虛擬環境後, 即可使用 pip, 經測試發現此方法雖然可用, 卻無法使用排程來啟動執行python程式. 此篇後段提到使用 curl 來安裝 pip3, 此方法方可排程使用.

2020年2月28日 星期五

[Synology] 第三方套件開發之環境設定

最近發現 NAS 也有個類似 App Market 的東西,找到原廠開發文件,打算用手上這台 DS918+ 來試試,這篇先做個紀錄。

2020年2月22日 星期六

[Synology NAS] 安裝 Serial Driver

最近想在 NAS 透過 USB Serial 到外部裝置, 找到這個網站: http://www.jadahl.com, 我的 NAS是 DS918+, 點選右上角 DSM6.2 drivers 進入驅動程式頁面 (http://www.jadahl.com/drivers_6.2/), 找到 DS918+ 的驅動程式, 下載後檔名為 UsbSerialDrivers_apollolake-6.2_6-7.spk, 進入 NAS 套件中心, 使用手動安裝後, 顯示安裝成功:

2020年1月28日 星期二

使用 ffmpeg 之 cuda 編碼器將 DVD 轉換成 mp4 格式

ffmpeg 使用 cuda 編碼器的參數範例:


ffmpeg -i "concat:VTS_01_1.VOB|VTS_01_2.VOB|VTS_01_3.VOB|VTS_01_4.VOB" -c:v h264_nvenc -preset slow -crf 22 -c:a copy "output.mp4"

與一般cpu版常用的libx264不同的是, 目前並沒有支援 veryslow 預設參數

參考資料:
[1] "HWAccelIntro", https://trac.ffmpeg.org/wiki/HWAccelIntro

2020年1月24日 星期五