Linux-c語言串口程序

c語言串口程序

// C library headers
#include <stdio.h>
#include <string.h>// Linux headers
#include <fcntl.h> // Contains file controls like O_RDWR
#include <errno.h> // Error integer and strerror() function
#include <termios.h> // Contains POSIX terminal control definitions
#include <unistd.h> // write(), read(), close()int main() {// Open the serial port. Change device path as needed (currently set to an standard FTDI USB-UART cable type device)int serial_port = open("/dev/ttyUSB0", O_RDWR);// Create new termios struct, we call it 'tty' for conventionstruct termios tty;// Read in existing settings, and handle any errorif(tcgetattr(serial_port, &tty) != 0) {printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));return 1;}tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in communication (most common)tty.c_cflag &= ~CSIZE; // Clear all bits that set the data sizetty.c_cflag |= CS8; // 8 bits per byte (most common)tty.c_cflag &= ~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)tty.c_cflag |= CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)tty.c_lflag &= ~ICANON;tty.c_lflag &= ~ECHO; // Disable echotty.c_lflag &= ~ECHOE; // Disable erasuretty.c_lflag &= ~ECHONL; // Disable new-line echotty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSPtty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrltty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL); // Disable any special handling of received bytestty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g. newline chars)tty.c_oflag &= ~ONLCR; // Prevent conversion of newline to carriage return/line feed// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT PRESENT ON LINUX)// tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars (0x004) in output (NOT PRESENT ON LINUX)tty.c_cc[VTIME] = 10;    // Wait for up to 1s (10 deciseconds), returning as soon as any data is received.tty.c_cc[VMIN] = 0;// Set in/out baud rate to be 9600cfsetispeed(&tty, B9600);cfsetospeed(&tty, B9600);// Save tty settings, also checking for errorif (tcsetattr(serial_port, TCSANOW, &tty) != 0) {printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));return 1;}// Write to serial portunsigned char msg[] = { 'H', 'e', 'l', 'l', 'o', '\r' };write(serial_port, msg, sizeof(msg));// Allocate memory for read buffer, set size according to your needschar read_buf [256];// Normally you wouldn't do this memset() call, but since we will just receive// ASCII data for this example, we'll set everything to 0 so we can// call printf() easily.memset(&read_buf, '\0', sizeof(read_buf));// Read bytes. The behaviour of read() (e.g. does it block?,// how long does it block for?) depends on the configuration// settings above, specifically VMIN and VTIMEint num_bytes = read(serial_port, &read_buf, sizeof(read_buf));// n is the number of bytes read. n may be 0 if no bytes were received, and can also be -1 to signal an error.if (num_bytes < 0) {printf("Error reading: %s", strerror(errno));return 1;}// Here we assume we received ASCII data, but you might be sending raw bytes (in that case, don't try and// print it to the screen like this!)printf("Read %i bytes. Received message: %s", num_bytes, read_buf);close(serial_port);return 0; // success
};

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/80656.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/80656.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/80656.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

TCP IP

TCP/IP 通信協議&#xff0c;不是單一協議&#xff0c;是一組協議的集合 TCP IP UDP 1.建立鏈接 三次握手 第一步&#xff1a;客戶端發送一個FIN報文&#xff0c;SEQX,等待服務器回應 第二步&#xff1a;服務器端受到&#xff0c;發送ackx1,seqy, 等待客戶端回應 第三步&am…

用uniapp在微信小程序實現畫板(電子簽名)功能,使用canvas實現功能

效果&#xff1a; 功能&#xff1a;實現重簽 退出 保存 等功能 解決的問題: 電子簽名畫布抖動問題解 注意&#xff1a; 保存的時候上傳到自己的服務器地址&#xff0c;后端返回圖片地址 代碼&#xff1a; <template><view><view class"signature&qu…

機器學習經典算法:用決策樹原理優化新能源汽車續航能力

?? “用決策樹重構新能源車能量大腦!算法推導+代碼實戰全解,續航暴增15%” 決策樹算法就像我們生活中做決策的 “流程指南”,通過層層判斷得出最終結論。比如你去超市買水果,站在琳瑯滿目的貨架前,就不自覺地用上了決策樹思維。首先,你可能會想 “今天想吃酸的還是甜的…

【Unity中的數學】—— 四元數

一、四元數的定義&#x1f60e; 四元數是一種高階復數&#xff0c;是一個四維空間的概念&#xff0c;相對于復數的二維空間。它可以表示為 q s i x j y k z q s ix jy kz qsixjykz&#xff0c;其中 s s s、 x x x、 y y y、 z z z 都是實數&#xff0c;并且滿足 i …

macOS 15.4.1 Chrome不能訪問本地網絡

前言 最近使用macmini m4&#xff0c;自帶macOS15系統&#xff0c;對于開發者簡直是一言難盡&#xff0c;Chrome瀏覽器的本地網絡有bug&#xff0c;可以訪問本機&#xff0c;但是不能訪問路由器上的其他機器&#xff0c;路由器提供的頁面也不能訪問&#xff0c;如下是折騰解決…

瀏覽器刷新結束頁面事件,調結束事件的接口(vue)

瀏覽器刷新的時候&#xff0c;正在進行中的事件結束掉&#xff0c;在刷新瀏覽器的時候做一些操作。 如果是調接口&#xff0c;就不能使用axios封裝的接口&#xff0c;需要使用原生的fetch。 找到公共的文件App.vue 使用window.addEventListener(‘beforeunload’, function (e…

TCP/IP 模型每層的封裝格式

TCP/IP 模型是一個四層網絡架構&#xff0c;每一層在數據傳輸時都會對數據進行封裝&#xff0c;添加相應的頭部&#xff08;和尾部&#xff09;信息。以下是各層的封裝格式及關鍵字段說明&#xff1a; 1. 應用層&#xff08;Application Layer&#xff09; 封裝格式&#xff1a…

【行業深度解析】什么是馬甲包?

在 Android 應用分發和增長運營的實踐中&#xff0c;“馬甲包” 是一個常被提及的策略術語。特別是在 Google Play 平臺上&#xff0c;許多開發者或運營團隊出于營銷、風險分攤或生態布局等原因&#xff0c;會選擇通過發布“馬甲包”來實現多元化的業務拓展。 然而&#xff0c…

谷歌與微軟的AI戰爭:搜索、云服務與生態布局

谷歌與微軟的AI戰爭&#xff1a;搜索、云服務與生態布局 系統化學習人工智能網站&#xff08;收藏&#xff09;&#xff1a;https://www.captainbed.cn/flu 文章目錄 谷歌與微軟的AI戰爭&#xff1a;搜索、云服務與生態布局摘要引言技術路線對比1. AI基礎設施&#xff1a;算力…

uniapp自定義導航欄搭配插槽

<uni-nav-bar dark :fixed"true" shadow background-color"#007AFF" left-icon"left" left-text"返回" clickLeft"back"><view class"nav-bar-title">{{ navBarTitle }}</view><block v-slo…

無人機飛控算法開發實戰:從零到一構建企業級飛控系統

簡介 無人機飛控算法是實現穩定飛行和精確控制的核心技術,涉及飛行動力學建模、傳感器數據處理、狀態估計和控制策略等多個環節。本實戰指南將系統講解四旋翼無人機飛控算法的開發流程,包括飛行動力學模型建立、傳感器校準與數據融合、主流控制算法實現(PID、ADRC、EKF)以…

p2p虛擬服務器

ZeroTier Central ? 推薦工具&#xff1a;ZeroTier&#xff08;免費、穩定、跨平臺&#xff09; ZeroTier 可以幫你把多臺設備&#xff08;無論是否跨網&#xff09;加入一個虛擬局域網&#xff0c;彼此間可以像在同一個 LAN 中通信&#xff0c;UDP 視頻、文件傳輸、SSH 等都…

MySQL數據庫遷移SQL語句指南

MySQL數據庫遷移SQL語句指南 一、基礎遷移方法 1. 使用mysqldump進行全量遷移 -- 導出源數據庫&#xff08;在命令行執行&#xff09; mysqldump -u [源用戶名] -p[源密碼] --single-transaction --routines --triggers --events --master-data2 [數據庫名] > migration…

畫立方體軟件開發筆記 js three 投影 參數建模 旋轉相機 @tarikjabiri/dxf導出dxf

gitee&#xff1a; njsgcs/njsgcs_3d mainwindow.js:4 Uncaught SyntaxError: The requested module /3dviewport.js does not provide an export named default一定要default嗎 2025-05-10 14-27-58 專門寫了個代碼畫立方體 import{ scene,camera,renderer} from ./3dviewp…

【工具】HandBrake使用指南:功能詳解與視頻轉碼

HandBrake使用指南&#xff1a;功能詳解與視頻轉碼 一、前言 高清視頻在當下日益普及&#xff0c;從影視制作到個人拍攝&#xff0c;從社交媒體發布到遠程教育&#xff0c;如何高效地壓縮、轉換和管理視頻文件的體積與清晰度&#xff0c;成為內容創作者與技術開發者的核心任務…

Docker容器網絡架構深度解析與技術實踐指南——基于Linux內核特性的企業級容器網絡實現

第1章 容器網絡基礎架構 1 Linux網絡命名空間實現原理 1.1內核級隔離機制深度解析 1.1.1進程隔離的底層實現 通過clone()系統調用創建新進程時&#xff0c;設置CLONE_NEWNET標志位將觸發內核執行以下操作&#xff1a; 內核源碼示例&#xff08;linux-6.8.0/kernel/fork.c&a…

SAP 交貨單行項目含稅金額計算報cx_sy_zerodivide處理

業務背景&#xff1a;SAP交貨單只有數量&#xff0c;沒有金額&#xff0c;所以開發報表從訂單的價格按數量計算交貨單的金額。 用戶反饋近期報表出現異常&#xff1a; ****2012/12/12 清風雅雨 規格變更 Chg 修改開始 ** 修改原因:由于余數為0時&#xff0c;可能會報錯溢出。…

【高數上冊筆記01】:從集合映射到區間函數

【參考資料】 同濟大學《高等數學》教材樊順厚老師B站《高等數學精講》系列課程 &#xff08;注&#xff1a;本筆記為個人數學復習資料&#xff0c;旨在通過系統化整理替代厚重教材&#xff0c;便于隨時查閱與鞏固知識要點&#xff09; 僅用于個人數學復習&#xff0c;因為課…

每日算法刷題 Day3 5.11:leetcode數組2道題,用時1h(有點慢)

5.LC 零矩陣(中等) 面試題 01.08. 零矩陣 - 力扣&#xff08;LeetCode&#xff09; 思想: 法一: 利用兩個集合分別儲存要清0的行和列索引 另外兩種原地優化空間的做法暫時不是目前刷題目標&#xff0c;故不考慮 代碼 c: class Solution { public:void setZeroes(vector&l…

【小記】excel vlookup一對多匹配

一個學生報四門課&#xff0c;輸出每個學生課程 應用概述操作預處理數據計數指令 COUNTIFS進行一對多匹配 vlookup 應用概述 應用場景&#xff1a;學生報名考試&#xff0c;需要整理成指定格式&#xff0c;發給考試院。 一個學生最多報考四門 格式實例&#xff1a;準考證號 …