Hello World Dev Conference
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Help
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # 如何用JS建構POS系統 - 發票機跟刷卡機 - 高逸宸(Logan) {%hackmd @HWDC/BJOE4qInR %} >#### 》[議程介紹](https://hwdc.ithome.com.tw/2024/session-page/3125) >#### 》[填寫議程滿意度問卷|回饋建言給辛苦的講者](https://forms.gle/Zji1xkyDjvQWzbSZ6) --- ## Javascript POS 系統商支援線上電商 需求:原本有實體Pos 有多個通路 有線上跟線下 ## 如何解決線下通路問題 POS(Point of Sale) - 出單機 - 終端機 - 展示螢幕 - 收銀機 出單機? 大家都沒碰過XD 你機械系畢業你應該碰過吧 !? 原本是想用Browser 內建的 ctrl + p 遇到二聯三聯發票的瓶頸 # EPSON 出單機 USB 電源 RS232(早期的通訊埠):Pos 機設備經常使用的規格 --可能廠商不支援下列通訊方式-- LAN(新型):網路通訊 使用TCP IP ,適合長距離控制 藍芽(新型):移動 POS 設備可能會使用藍牙進行無線通信,適合需要靈活佈置的應用 # 通訊文本格式 - ECS/POS EPSON制定給POS的碼(ASCII) 連結:https://download4.epson.biz/sec_pubs/pos/reference_en/escpos/commands.html ## Web Serial / USB API 瀏覽器會跳視窗提醒,導致每次列印都會跳提醒。 > 解決策略 Node.js USB > https://developer.mozilla.org/en-US/docs/Web/API/Web_Serial_API ![](https://i.imgur.com/l1j2ynY.png) ## Node.js USB - escpos - escpos-web https://www.npmjs.com/package/escpos https://github.com/node-escpos/driver 範例程式碼 ``` js const escpos = require("escpos"); escpos.USB = require("escpos-usb"); const device = new escpos.USB(0x04b8, 0x0e17); const options = { encoding: "BIG5" }; const printer = new escpos.Printer(device, options); device.open(function (error) { printer .font("a") .align("ct") .style("bu") .size(1, 1) .text("The quick brown fox jumps over the lazy dog") .text("敏捷的棕色狐狸跳過懶狗") .barcode("1234567", "EAN8") .table(["One", "Two", "Three"]) .tableCustom([ { text: "Left", align: "LEFT", width: 0.33, style: "B" }, { text: "Center", align: "CENTER", width: 0.33 }, { text: "Right", align: "RIGHT", width: 0.33 }, ], { encoding: "cp857", size: [1, 1] } // Optional ) .qrimage("https://github.com/song940/node-escpos", function (err) { this.cut(); this.close(); }); }); ``` - 客戶端防火牆的 port 打開 ## 部署流程 由於需求要以線下為主,因此必須將開發的內容包裝成能夠 Local 執行 方案 1. - JS POS Client click button - Local Serrver - pkg --output pos.exe - Printer 2. Electron.js ## 刷卡機 - 確認通訊方式 - 確認通訊文本格式(文件跟信用卡機要) - 機台還要跟信用卡中心要求開啟 POS 連線功能 乙太網路(需要跟銀行要求開啟) 電話孔(跟銀行通訊確認信用卡資訊的) 電源 Serial Port USB to RS232 當時只是看到伽利略的名字很厲害就買了 > https://www.npmjs.com/package/serialport 連接 -> 寫入 -> 監聽 ``` js // 寫入 const sendMessage = (message, callback) => { cardReader.write(message, (err) => { if (err) { console.log("Error on write: ", err.message); res.status(500).send("Failed to write message"); cardReader.close(); return; } console.log("Message sent: ", message); }); }; let completeData = ""; // 監聽 cardReader.on("data", (data) => { completeData += Buffer.from(data).toString("utf8"); console.log("Data received: ", completeData); if (completeData.length >= 400) { cardReader.close((err) => { if (err) { console.log("Error on close: ", err.message); } console.log("Card reader closed"); callback(completeData); }); } }); cardReader.on("error", (err) => { console.log("Error: ", err.message); res.status(500).send("Card reader error"); // cardReader.close(); }); cardReader.on("close", () => { console.log("Serial port closed"); }); ``` 要跟刷卡機的公司要資料格式 需要自己處理fileds製作欄位 信用卡中心要開啟POS連線功能,因為預設POS連線備鎖起來了 ## 如果你要JS串連硬體設備 - 問客服 他們一定有技術客服 - 確認通訊方式和文本格式 - 選擇適合的一條路 新型的有wi-fi 和藍牙 ## 優勢 - 跨平台支援 - 靈活性與擴充性 - 很多人做過惹,網路上很多文件教學函式庫等等可以用 - 低成本

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully