蘋果小豬研究室
  • 首頁
  • 中譯資料
  • 網路觀念
    • OSGi 平台
    • SCTP 通訊協定簡介
      • 1 多重串流 (Multi-streaming)
      • 2 路徑多宿 (Multi-homing)
      • 3 SACK 機制
      • 4 Path MTU Discovery
      • 5 Ordered/Unordered 傳送機制
      • 6. SCTP API
      • 7. TCP 與 SCTP 通訊協定比較
      • 8. 參考文獻
    • TCP over SCTP tunnel
    • TCP 與 SCTP 協定轉換機制
    • UPnP 與 UPnP AV
  • Linux 程式設計
    • [C] 產生亂數
    • [C] Pointer and array
    • [Script] strncmp
    • [C] Linux 取得 HD 序號
    • [C] 程式範例-切割 subnet
    • C struct 的使用
    • C 目錄相關
    • const 修飾詞
    • Dangling pointer(懸置指標)
    • dnsmole
    • dos2unix 程式碼
    • Function pointer in C
    • GCC header limits.h
    • GDB 參數
    • glibc detected (double free)
    • IEEE 754 and float 範例程式
    • kernel 中的網路卡名稱與命名順序關係
    • Linux 序列埠程式設計
    • loop device 的數目
    • Perl & LWP
    • Remote Control over CGI
    • 偵測 NAT 內部主機連線狀況
    • 利用 libpcap 取得所有的網路裝置名稱
    • 取得時間
    • strncpy
    • 從應用層程式取得傳輸層 TCP 資訊
    • 於 Linux 上撰寫 daemon server 要注意的
    • 為什麼需要 & 0377
    • 研究 setsid()
    • 與 HTTP 協定有關的 RFC
    • 製作 patch 檔
    • 設計在背景執行的 Linux 程式
    • 讀取 STDIN (Standard INPUT) 範例程式
    • 關於 Linux CPU smp_affinity
    • 陣列名稱與指標
  • 網路服務推薦
    • 專案管理工具 - clickup
    • 文件大師 - gitbook
  • glibc 中文手冊
    • GNU C 函式庫常見問題(FAQ)
  • Linux 系統操作
    • /proc/sys/net/ipv4/conf/eth* 目錄中的參數
    • Linux 伺服器效能調校
    • Linux 設定網卡速度
    • Proxy ARP
    • sysrq
    • Turn off beep in Linux system
    • umask()
    • 以 ethtool 查看網路卡狀態及設定
    • 使用 nameif 修改 Linux 網路卡名稱
    • 使用 xhost
    • 利用 sed 取出檔案中某行
    • 在 bash 中設定 Proxy server
    • 在 Linux 系統呈現 dd 的進度
    • 字串取代,使用 awk & sed
    • 尋找多種副檔名
    • 常用 VIM 選項
    • 自動 CPU 調頻節能控制
    • 自動執行程式
    • 設定 Debian 預設啟動服務
    • 設定 DHCPd server
    • 透過 RS-232 操作 Linux console
    • Linux connect to serial port
  • 資訊安全
    • ARP spoofing 技術管控區網流量實例
    • dsniff
    • Linux Socket Filtering
    • Linux 系統安全管理
    • Security 資訊
    • sniffer 相關工具 - IPgrad, tcpick
    • 以 Linux 實務 ARP Spoofing
    • 將文字隱藏於 JPEG 圖檔
    • 資安工具
    • 資安相關網站
    • 限制 localhost 的 P2P 上傳流量
  • 實用工具
    • 實驗基本數據製圖指令 gnuplot
  • 交換連結
  • 網頁設定
  • 網路應用
    • 網路時光機
  • Foldng@Home
  • 文獻推薦
    • 電子書
    • 提問的智慧
    • 好書
  • 隱私政策
Powered by GitBook
On this page
Edit on GitHub
  1. Linux 程式設計

Linux 序列埠程式設計

Previouskernel 中的網路卡名稱與命名順序關係Nextloop device 的數目

Last updated 2 years ago

這裡我們探討 Linux 序列埠程式設計,你需要熟悉 和 Linux。 首先,用下列的程式來開啟序列埠。

int open_port(const char *pathname) {

    /* open the pathname, ex. pathname="/dev/ttyACM0" */ 

    int serial_fd = open(pathname, O_RDWR|O_NOCTTY|O_NDELAY);

    /* fail to open the serial port */
    if(serial_fd<0) {

        if( debug ) {
            printf("%s: fail to open [%s].\n", __func__, pathname);
        }
        return -1;
    }

    printf("%s: %s, serial_fd: %3d\n", __func__, pathname, serial_fd);

    // enable the nonblocking mode for reading.
    fcntl(serial_fd, F_SETFL, FNDELAY);

    return serial_fd;
}

接著透過 file descriptor(檔案描述子)設定序列埠。

    #include <termios.h> /* more baud rates could be found here */
    #include <fcntl.h>

    struct termios options;
    speed_t baud = B38400;

    tcgetattr(serial_fd, &options); /* Get the original setting */

    /* Setting the baudrate */
    cfsetispeed(&options, baud);
    cfsetospeed(&options, baud);

    /* enable the receiver and set local mode */
    options.c_cflag |= (CLOCAL|CREAD);

    /* setting the character size (8-bits) */
    options.c_cflag &= ~CSIZE;
    options.c_cflag |= CS8;

    /* Setting Parity Checking: NONE (NO Parity) */
    options.c_cflag &= ~PARENB;
    options.c_cflag &= ~CSTOPB;

    /* Disable Hardware flow control */
    options.c_cflag &= ~CRTSCTS;

    /* Disable Input Parity Checking */
    options.c_iflag &= ~INPCK;

    /* Disable software flow control */
    options.c_iflag &= ~(IXON|IXOFF|IXANY);

    options.c_iflag &= ~(IGNPAR|ICRNL);

    /* output raw data */
    options.c_oflag &= ~OPOST;

    /* disablestd input */
    options.c_lflag &= ~(ICANON|ECHO|ECHOE|ISIG);

    /* clean the current setting */
    tcflush(serial_fd, TCIFLUSH);

    /* Enable the new setting right now */
    tcsetattr(serial_fd, TCSANOW, &options);

參考文獻

之後的部分,如讀寫則與一般網路程式設計大同小異,可利用 descriptor 進行讀寫,以及透過 select() 函式之類的方式檢查 serial port 是否有資料可讀寫。 select() 的使用請參考 的第七章。

[1] Michael Kerrisk, , 2010. [2] Michael R. Sweet, Serial Programming Guide for POSIX Operating Systems, 5th Edition.

Aaron Liao
C 語言
Beej's Guide to Network Programming 正體中文版
The Linux Programming Interface