當前位置:首頁 » 編程軟體 » linux串口編程

linux串口編程

發布時間: 2022-01-08 01:36:19

linux下的串口編程

這有個友善的串口常式,參考下吧,用gcc編譯可以在linux下用
# include <stdio.h>
# include <stdlib.h>
# include <termio.h>
# include <unistd.h>
# include <fcntl.h>
# include <getopt.h>
# include <time.h>
# include <errno.h>
# include <string.h>

int CommFd, TtyFd;

static void Error(const char *Msg)
{
fprintf (stderr, "%s\n", Msg);
fprintf (stderr, "strerror() is %s\n", strerror(errno));
exit(1);
}
static void Warning(const char *Msg)
{
fprintf (stderr, "Warning: %s\n", Msg);
}

static int SerialSpeed(const char *SpeedString)
{
int SpeedNumber = atoi(SpeedString);
# define TestSpeed(Speed) if (SpeedNumber == Speed) return B##Speed
TestSpeed(1200);
TestSpeed(2400);
TestSpeed(4800);
TestSpeed(9600);
TestSpeed(19200);
TestSpeed(38400);
TestSpeed(57600);
TestSpeed(115200);
TestSpeed(230400);
Error("Bad speed");
return -1;
}

static void PrintUsage(void)
{

fprintf(stderr, "comtest - interactive program of comm port\n");
fprintf(stderr, "press [ESC] 3 times to quit\n\n");

fprintf(stderr, "Usage: comtest [-d device] [-t tty] [-s speed] [-7] [-c] [-x] [-o] [-h]\n");
fprintf(stderr, " -7 7 bit\n");
fprintf(stderr, " -x hex mode\n");
fprintf(stderr, " -o output to stdout too\n");
fprintf(stderr, " -c stdout output use color\n");
fprintf(stderr, " -h print this help\n");
exit(-1);
}

static inline void WaitFdWriteable(int Fd)
{
fd_set WriteSetFD;
FD_ZERO(&WriteSetFD);
FD_SET(Fd, &WriteSetFD);
if (select(Fd + 1, NULL, &WriteSetFD, NULL, NULL) < 0) {
Error(strerror(errno));
}

}

int sendUart(char c)
{
WaitFdWriteable(CommFd);
return write(CommFd, &c, 1);
}

char recUart()
{
char c='\0';
if (FD_ISSET(CommFd, &ReadSetFD))
{
if(read(CommFd, &c, 1) == 1) return c;
else printf("No data to receive.\n");
}
}

int main(int argc, char **argv)
{
struct termios TtyAttr;
struct termios BackupTtyAttr;

int DeviceSpeed = B115200;
int TtySpeed = B115200;
int ByteBits = CS8;
const char *DeviceName = "/dev/ttyS0";
const char *TtyName = "/dev/tty";
int OutputHex = 0;
int OutputToStdout = 0;
int UseColor = 0;

printf("Now we start.\n");
opterr = 0;
for (;;) {
int c = getopt(argc, argv, "d:s:t:7xoch");
if (c == -1)
break;
switch(c) {
case 'd':
DeviceName = optarg;
break;
case 't':
TtyName = optarg;
break;
case 's':
if (optarg[0] == 'd') {
DeviceSpeed = SerialSpeed(optarg + 1);
} else if (optarg[0] == 't') {
TtySpeed = SerialSpeed(optarg + 1);
} else
TtySpeed = DeviceSpeed = SerialSpeed(optarg);
break;
case 'o':
OutputToStdout = 1;
break;
case '7':
ByteBits = CS7;
break;
case 'x':
OutputHex = 1;
break;
case 'c':
UseColor = 1;
break;
case '?':
case 'h':
default:
PrintUsage();
}
}
if (optind != argc)
PrintUsage();

CommFd = open(DeviceName, O_RDWR, 0);
if (CommFd < 0)
Error("Unable to open device");
if (fcntl(CommFd, F_SETFL, O_NONBLOCK) < 0)
Error("Unable set to NONBLOCK mode");

memset(&TtyAttr, 0, sizeof(struct termios));
TtyAttr.c_iflag = IGNPAR;
TtyAttr.c_cflag = DeviceSpeed | HUPCL | ByteBits | CREAD | CLOCAL;
TtyAttr.c_cc[VMIN] = 1;

if (tcsetattr(CommFd, TCSANOW, &TtyAttr) < 0)
Warning("Unable to set comm port");

TtyFd = open(TtyName, O_RDWR | O_NDELAY, 0);
if (TtyFd < 0)
Error("Unable to open tty");

TtyAttr.c_cflag = TtySpeed | HUPCL | ByteBits | CREAD | CLOCAL;
if (tcgetattr(TtyFd, &BackupTtyAttr) < 0)
Error("Unable to get tty");

if (tcsetattr(TtyFd, TCSANOW, &TtyAttr) < 0)
Error("Unable to set tty");

for (;;) {
unsigned char Char = 0;
fd_set ReadSetFD;

void OutputStdChar(FILE *File) {
char Buffer[10];
int Len = sprintf(Buffer, OutputHex ? "%.2X " : "%c", Char);
fwrite(Buffer, 1, Len, File);
}

FD_ZERO(&ReadSetFD);

FD_SET(CommFd, &ReadSetFD);
FD_SET( TtyFd, &ReadSetFD);
# define max(x,y) ( ((x) >= (y)) ? (x) : (y) )
if (select(max(CommFd, TtyFd) + 1, &ReadSetFD, NULL, NULL, NULL) < 0) {
Error(strerror(errno));
}
# undef max

if (FD_ISSET(CommFd, &ReadSetFD)) {
while (read(CommFd, &Char, 1) == 1) {

WaitFdWriteable(TtyFd);
if (write(TtyFd, &Char, 1) < 0) {
Error(strerror(errno));
}
if (OutputToStdout) {
if (UseColor)
fwrite("\x1b[01;34m", 1, 8, stdout);
OutputStdChar(stdout);
if (UseColor)
fwrite("\x1b[00m", 1, 8, stdout);
fflush(stdout);
}
}
}

if (FD_ISSET(TtyFd, &ReadSetFD)) {
while (read(TtyFd, &Char, 1) == 1) {
static int EscKeyCount = 0;
WaitFdWriteable(CommFd);
if (write(CommFd, &Char, 1) < 0) {
Error(strerror(errno));
}
if (OutputToStdout) {
if (UseColor)
fwrite("\x1b[01;31m", 1, 8, stderr);
OutputStdChar(stderr);
if (UseColor)
fwrite("\x1b[00m", 1, 8, stderr);
fflush(stderr);
}

if (Char == '\x1b') {
EscKeyCount ++;
if (EscKeyCount >= 3)
goto ExitLabel;
} else
EscKeyCount = 0;
}
}

}

ExitLabel:
if (tcsetattr(TtyFd, TCSANOW, &BackupTtyAttr) < 0)
Error("Unable to set tty");

return 0;
}

⑵ Linux RS485串口編程

對於編程來說,沒什麼區別,通過控制485的使能端該程序完全可以使用。唯一的區別就是你在發送的時候通過程序把485的控制腳拉高,接收的時候把他拉低就可以了。至於電氣方面的區別:RS232是全雙工,可以同時收發,RS485是半雙工,不能同時收發,還有電平信號不一樣,這個編程你就不要理了。

⑶ linux文件編程和串口編程的基本概念是什麼

簡單說幾句吧,linux下的設備都是文件,流程也無非是open, read/write, close等當然,串口你得設置各種屬性才行對不對,比如在win下的超級終端就設置了波特率啊,停止位啊,奇偶校驗啊什麼的,這些屬性都通過 int tcgetattr(int fd, struct termios *termios_p); int tcsetattr(int fd, int optional_actions, const struct termios *termios_p);函數來設置。完整代碼嗎自己去google,一把一把的,其實最重要的是設置好屬性,剩下的就是read,write的問題咯。希望對你有用對了,了解終端函數的詳情請在linux命令行終端獲取: man termios

⑷ 如何實現linux下的串口中斷編程

#include
#include
#include
#include
#include
#include

#define BAUDRATE B38400
#define MODEMDEVICE "/dev/ttyS1"
#define _POSIX_SOURCE 1 /* POSIX 系統相容 */
#define FALSE 0
#define TRUE 1

volatile int STOP=FALSE;

void signal_handler_IO (int status); /* 定義訊號處理程序 */
int wait_flag=TRUE; /* 沒收到訊號的話就會是 TRUE */

main()
{
int fd,c, res;
struct termios oldtio,newtio;
struct sigaction saio; /* definition of signal action */
char buf[255];

/* 開啟裝置為 non-blocking (讀取功能會馬上結束返回) */
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd <0) {perror(MODEMDEVICE); exit(-1); }

/* 在使裝置非同步化前, 安裝訊號處理程序 */
saio.sa_handler = signal_handler_IO;
saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);

/* 允許行程去接收 SIGIO 訊號*/
fcntl(fd, F_SETOWN, getpid());
/* 使檔案ake the file descriptor 非同步 (使用手冊上說只有 O_APPEND 及
O_NONBLOCK, 而 F_SETFL 也可以用...) */
fcntl(fd, F_SETFL, FASYNC);

tcgetattr(fd,&oldtio); /* 儲存目前的序列埠設定值 */
/* 設定新的序列埠為標准輸入程序 */
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag = 0;
newtio.c_lflag = ICANON;
newtio.c_cc[VMIN]=1;
newtio.c_cc[VTIME]=0;
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);

/* 等待輸入訊號的迴圈. 很多有用的事我們將在這做 */
while (STOP==FALSE) {
printf(".\n");usleep(100000);
/* 在收到 SIGIO 後, wait_flag = FALSE, 輸入訊號存在則可以被讀取 */
if (wait_flag==FALSE) {
res = read(fd,buf,255);
buf[res]=0;
printf(":%s:%d\n", buf, res);
if (res==1) STOP=TRUE; /* 如果只輸入 CR 則停止迴圈 */
wait_flag = TRUE; /* 等待新的輸入訊號 */
}
}
/* 回存舊的序列埠設定值 */
tcsetattr(fd,TCSANOW,&oldtio);
}

⑸ linux 串口編程亂碼

關於串口設備,最好聯系廠家詢問是否提供API介面。

關於API介面一般是一個可供調用的DLL文件。

如果有可以直接在C#中引用,作為類庫來操作設備。

具體調用方式需要詢問廠家或參閱api的文檔說明。

不提供軟體介面的設備是無法進行開發的。

我大概搜了下這個設備,設備是提供配套軟體的,那麼這款設備是有相關通訊介面類庫的,也就是可以用C#進行開發。
如果找不到API文檔,
請嘗試用串口調試工具,跟蹤配套軟體的每一步操作,獲取串口通訊報文,用C#模擬操作報文自己封裝通訊類後進行開發。

⑹ 如何在LINUX下編寫一個C語言的串口程序

1、參考這個:POSIX操作系統串口編程指南和 UNIX環境高級編程。
2、簡單介紹一下:
《POSIX操作系統的串口編程指南》是在UNIX環境或PC上對串口進行編程的教程,每一章提供的常式都使用POSIX(Portable Standard for UNIX)終端控制函數,只需極少的修改就可運行在IRIX 、HP-UX、 SunOS、 Solaris、 Digital UNIX、 Linux等大多數類UNIX操作系統。

⑺ 很簡單的linux串口編程問題:fd = open("/dev/ttysn",|XXXXXXX)。其中哪個ttysn具體是多少

沒做過linux下的,提供個建議,看成不成。
PC端的串口必須配置正確。 要確定 與 板子的 波特率 要一致。
此外, PC端作為串口總控端, COM埠配置只針對於PC端自己。 比如你使用了COM1口,那麼定義的時候,(ttysn 應該是 ttysn1 --- 沒用過linux下的不知道是不是在這配置,你要查)

板子端的COM口配置也是只針對於自己,如果你使用板子的COM1和COM2, 那麼程序中初始化的時候需要同時把COM1/2都初始化,那麼PC端就可以連接任意的板子埠。

PC(COM1) ---- 板子(COM1) 或 PC(COM1) ---- 板子(COM2)

在確保板子硬體沒有問題的情況下,且PC端程序無誤, 如果PC端無法接收到數據, 嘗試
在PC端編寫程序時,在 接收數據之前 加上時間延遲。 也就是說,PC發出數據後需要等待
一段時間才能接收到板子 返回的數據。 具體時間測試來看。

⑻ 關於linux下多串口編程

或許 是 硬體資源 的 問題
串口 相關的 中斷、IO 地址等等

⑼ linux下怎樣對串口編程

使用串口協議登錄Linux終端控制台,通過Zmodem文件傳輸協議接收一個外部文件。 命令:rz -y 會彈出文件瀏覽窗口,選擇要上傳的文件即可。 -y 表示若文件已存在,則覆蓋。

熱點內容
電腦下載配置錯誤是什麼意思 發布:2024-10-18 18:17:11 瀏覽:503
這可不是愛ftp 發布:2024-10-18 18:16:15 瀏覽:808
可區分存儲單元中存放的是指令還是數據 發布:2024-10-18 17:57:26 瀏覽:886
java中return返回值 發布:2024-10-18 17:57:11 瀏覽:560
安卓換蘋果主題對手機有什麼影響 發布:2024-10-18 17:49:11 瀏覽:117
華易資料庫 發布:2024-10-18 17:48:05 瀏覽:358
概率題演算法 發布:2024-10-18 17:44:30 瀏覽:421
可以寫源碼的文本編譯器 發布:2024-10-18 17:44:29 瀏覽:282
100到200素數c語言 發布:2024-10-18 17:38:20 瀏覽:181
有錢還眾籌源碼 發布:2024-10-18 17:33:13 瀏覽:213