2018年12月23日 星期日

什麼是 RS232C?


什麼是 RS232C

RS-232 (ANSI/EIA-232 標準) 為過去 IBM 相容電腦的序列連結功能, 可用以連接滑鼠、印表機或數據機,或用於工業級儀器控制等許多功能。
  • 電腦中最常用的介面之一。 
  • RS-232 : Recommend Standard number 232. 
  • C 表示最新的版本. 
  • 大多電腦都是符合RS-232C一部份標準. 
  • 連線長度只能50~100 呎(17~33m)
  • 一個 Comm Port只能接一台
  • 全功能的 RS-232C 規定使用25-pin D 接頭,使用其中的 22 pins. 
  • 對電腦通訊,大多是不必要的,因此大多數PC只使用一個9pin的公接頭。 
  • 其實,只要使用其中3根pin便可達到全雙工 (full duplex)通訊的目的,一根Send,一根 receive,一根ground
    (Full duplex:送資料與接收資料同時進行,亦即是 雙向的溝通bi-directional)

RS232定義兩種接頭形式,接腳訊號定義不同
  • DCE (data communication Equipment) 
    • 母接頭
    • 資料通訊設備→遠端儀器
  • DTE (data terminal equipment) 
    • 公接頭
    • 資料終端設備→電腦
  • DTE 與DCE可直接一對一連接,
  • 同類間連接需要用Null modem Cable,交換 send 與 receive腳位 

資料如何傳輸 - start bit
  • RS232是屬於序列式的(serial)傳輸資料,資料是一個bit一個bit傳
  • 一般狀態,TD 與RD是在高電位,一端的TD 是接到另端的RD,當有資料要傳時,一 端的TD會先被拉低電位,而另一端的 RD接收到低電位開始接收資料,這個動作叫做送起始位元( Start bit)

接收資料-data bits
  • Start bit後,兩邊的設備開始準備接收資料 
  • 為了溝通,兩邊必須有相同的傳輸速度(baud rate),及有多少 data bits要送
    (Baud rate: 每秒多少bit的傳輸動作)
  • 確定好傳輸速度,接著規定要傳多少bit, 一般是傳8或7 bit,以8 bit最常用。LSB最小位元先送。

資料檢查 Parity bit 
  • 檢查序列資料是否傳得正確?
  • 奇偶檢查 
  • Data bit送完,會再送一個parity bit,讓接收端檢查資料是否正確
  • 有以下幾種: 
    • none:不送parity bit (最常用) 
    • Odd:當data bit有偶數個1時 → 送1補成奇數個 
    • Even :當data bit有奇數個1時 → 送1補成偶數個 
    • Mark:永遠送1做parity bit 
    • Space :永遠送0做parity bit

Stop Bit
  • 停止位元: 將電位拉高,宣告資料已經傳 輸完畢。 
  • 最常用的傳輸格式常記為 8-N-1 8: 
    • 8 data bit 
    • N: No parity bit 
    • 1: 1 start/stop bit
      (設備較慢時用 2bits)

防止資料遺失
  • 使用Handshaking(握手 ) 
    • 可不用 
    • 很多方式
      • 利用某一根線的高低電位 ( 如DTR),當一端準備收資料時先拉高某一線電位,另一端接收到訊息後,才開始送資料
      • 利用TD, RD線,傳送特殊的byte 當成準備接收資料的代號
  • 使用中斷(interrupt)或詢問(polling) 
    • Interrupt:有事件發生時執行 
    • Polling:以軟體不斷檢查資料
  • 使用Acknowledgement(確認 ) 
    • 收到資料時,送回一個特殊byte確認資料收到
  • 錯誤檢查(error checking) 送checksum byte給接收端檢查資料正確性,有問題,通知重送資料

2018年12月10日 星期一

ASP.NET Core 2.0 - JwtBearerEvents


想在 .Net Core 2.0 裡接 jwt 驗證的事件,
比如拿到 token 驗證失敗的 exception,
該怎麼做呢?

建立 CustomJwtBearerEvents 類別,實作 JwtBearerEvents
public class CustomJwtBearerEvents : JwtBearerEvents
{
    private static Logger _logger = LogManager.GetCurrentClassLogger();

    public CustomJwtBearerEvents() : base()
    {}

    public override Task AuthenticationFailed
            (AuthenticationFailedContext context)
    {
        _logger.Error(context.Exception.Message);
        return this.OnAuthenticationFailed.Invoke(context);
    }

    public override Task TokenValidated(TokenValidatedContext context)
    {
        _logger.Trace("OnTokenValidated: " + context.SecurityToken);

        if (something)
        {
            var failMsg = $"Invalid API Token : {jwtToken.RawData}";
            context.Fail(failMsg);
            _logger.Error(failMsg);
                    
        }
        return this.OnTokenValidated.Invoke(context);
    }

    public override Task Challenge(JwtBearerChallengeContext context)
    {
        return this.OnChallenge.Invoke(context);
    }

    public override Task MessageReceived(MessageReceivedContext context)
    {
        return this.OnMessageReceived.Invoke(context);
    }
}

reference: https://www.codeproject.com/Articles/1205160/ASP-NET-Core-Bearer-Authentication

2018年3月28日 星期三

Runtime + Compiler on Vue-Cli 3.0


有需求動態載入 component,
在 jsbin 做簡單的測試是沒問題,
但使用 Vue-Cli 3.0 的開發環境,
卻拋出以下錯誤

You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.

經一番奮鬥後,發現 Vue-Cli 3.0 的 webpack 設定,
預設載入的 Vue 是 vue.runtime.esm.js,
所以在 runtime 的時候會發生此問題,
需將  vue.runtime.esm.js 換成  vue.esm.js
在 run time 期間也有 compiler 可以用

那該怎麼做呢?

在根目錄下設定 vue.config.js 如下即可
 (若沒有此檔,請自行新增!)
module.exports = {
  configureWebpack: {
    resolve: {
      alias: {
        'vue$': 'vue/dist/vue.esm.js'
      }
    }
  }
}


參考來源:

  1. https://github.com/vuejs/vue/tree/dev/dist#explanation-of-build-files
  2. https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md

2018年3月22日 星期四

關閉 Visual Studio 2017 自動編譯 ts 檔


在專案檔加入 <typescriptenabled>true</typescriptenabled>.

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
  </PropertyGroup>


reference:

TypeScriptCompileBlocked

If you are using a different build tool to build your project (e.g. gulp, grunt , etc.) and VS for the development and debugging experience, set true in your project. This should give you all the editing support, but not the build when you hit F5.

2017年12月14日 星期四

Startup pm2 command for Windows Server 2012 R2


需在 windows server 重新啟動的時候,
自動執行 pm2 start someapp.js,
pm2 有 startup command 提供此機制,但不支援 windows,
以下是 worked solution


The following is my latest solution which works really well:
  • create a specific Windows user for running node scripts
  • login as that user and npm install pm2 -g
  • pm2 start all the apps I would like to have startup
  • pm2 save to save the current process list
  • create a .bat file and within it paste the following commands:
@echo off
set HOMEDRIVE=C:
set HOMEPATH=\Users\%USERNAME%
set path=C:\Users\%USERNAME%\AppData\Roaming\npm;%path%
pm2 delete all & pm2 resurrect

Use Windows Task Scheduler to create a task that:
  • runs "whether user is logged on or not"
  • is Triggered "At startup"
  • is configured to run the .bat file that we created


2017年11月7日 星期二

How to enable hardware virtualization on a MacBook?

開發環境在 Mac 上的 windows 10 Home
需要啟用 virtualization ,
可是硬體本身有支援,卻沒有啟用 virtualization,
但是該如何啟用呢?
方式如下:

How to enable virtualization in Boot Camp.
  1. holding the option key on startup, boot in to OS X. 
  2. Then go to System Preferences 
  3. Startup Disk and choose your Boot Camp partition
  4. The computer will restart and boot into Windows, with virtualization enabled.

OK! 附張成功的圖紀念一下!

2017年11月1日 星期三

Error: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

使用 LinqToExcel 突然發生 error,原來是安裝 windows update 後,發生的慘案
Error: The 'Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine.

解決方案如下:

1. uninstall KB4041676 on Windows 10 and KB4041681 on Windows 7.

2. Find prior version (4.0.9801.0) of msexcl40.dll

3. Place in another directory. They suggest the application directory, but since in the next step you will modify registry to point to this older version, it can probably go anywhere.

4. Update registry key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Jet\4.0\Engines\Excel\win32 to point to the location from step 2.

但此招是救急,可能有安全性的疑慮!