導航:首頁 > 編程語言 > linuxc串口編程

linuxc串口編程

發布時間:2022-08-02 10:54:06

A. 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;
}

B. 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

C. 關於怎樣在linux上用C寫串口收發數據程序

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

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

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

E. C語言串口設置問題:linux下怎麼用C語言設置串口通訊的MARK, SPACE校驗

struct termios opt;

memset(&opt, 0, sizeof(opt));
cfmakeraw (&opt);
opt.c_cflag |= CLOCAL | CREAD | CS8; //8bit
opt.c_cflag &= ~(PARENB | CSTOPB); //no parity check,no stop bit
opt.c_cc[VMIN] = 1;
opt.c_cc[VTIME] = 0; /*read waite for x*0.1s*/
更多知識看看unix環境高級編程-終端編程哪一章,或網路linux串口編程。

F. Linux c串口編程中什麼是奇偶效驗

奇偶校驗是一種校驗代碼傳輸正確性的方法。根據被傳輸的一組二進制代碼的數位中「1」的個數是奇數或偶數來進行校驗。採用奇數的稱為奇校驗,反之,稱為偶校驗。採用何種校驗是事先規定好的。通常專門設置一個奇偶校驗位,用它使這組代碼中「1」的個數為奇數或偶數。若用奇校驗,則當接收端收到這組代碼時,校驗「1」的個數是否為奇數,從而確定傳輸代碼的正確性。

G. 求一段在Linux下用C編寫的讀寫232串口的程序

網上大把資料,就是簡單的串口編程。

H. Linux 用C寫串口(modem)(急!)

是的,linux是linus
tovalds當時為了研究一個多用戶多任務操作系統,用c代碼編寫了一個很小的操作系統內核,他把這個源碼公布,大家都來修改它和發展它,最終發展成現在的linux操作系統.

I. 串口編程,linux C編程,RS-232的程序可以直接在RS-485上使用嗎

基本可以的

用 RS232轉RS485轉換器

請看 武漢鴻偉光電
E485A RS232/RS485無源轉換器
E485B RS232/RS485有源隔離轉換器

J. Linux下串口編程

這是基本的C語言哈。
|= 就是「或等於」,跟 += 是一個道理,按位或你肯定知道哈。
1. newtio.c_cflag |=PARENB 相當於 newtio.c_cflag = netwtio.c_cflag | PARENB
意思就是netwtio.c_cflag 或 上 PARENB 的結果賦給netwtio.c_cflag
2. |=的含義跟上面相同, ~的含義是按位取反
newtio.c_cflag |=~PARENB 相當於 newtio.c_cflag = netwtio.c_flag | (~PARENB)

閱讀全文

與linuxc串口編程相關的資料

熱點內容
gz壓縮文件夾 瀏覽:177
字母h從右往左跑的c語言編程 瀏覽:127
安卓手機如何擁有蘋果手機橫條 瀏覽:765
業余編程語言哪個好學 瀏覽:137
按照文件夾分個壓縮 瀏覽:104
航空工業出版社單片機原理及應用 瀏覽:758
如何在電信app上綁定親情號 瀏覽:376
安卓的怎麼用原相機拍月亮 瀏覽:805
配音秀為什麼顯示伺服器去配音了 瀏覽:755
c盤清理壓縮舊文件 瀏覽:325
app怎麼交付 瀏覽:343
圖蟲app怎麼才能轉到金幣 瀏覽:175
如何做徵文app 瀏覽:446
用什麼app管理斐訊 瀏覽:169
安卓如何下載寶可夢劍盾 瀏覽:166
編譯器開發屬於哪個方向 瀏覽:940
megawin單片機 瀏覽:687
以色列加密貨幣監督 瀏覽:909
程序員前端現在怎麼樣 瀏覽:499
伺服器和介面地址ping不通 瀏覽:557