當前位置:首頁 » 操作系統 » 串口通信linux

串口通信linux

發布時間: 2022-02-12 22:07:50

Ⅰ 如何查看linux下串口是否可用串口名稱等

1、查看串口是否可用,可以對串口發送數據比如對com1口,echo lyjie126 > /dev/ttyS0

2、查看串口名稱使用 ls -l /dev/ttyS* 一般情況下串口的名稱全部在dev下面,如果你沒有外插串口卡的話默認是dev下的ttyS* ,一般ttyS0對應com1,ttyS1對應com2,當然也不一定是必然的;

3、查看串口驅動:cat /proc/tty/drivers/serial

4、查看串口設備:dmesg | grep ttyS*

(1)串口通信linux擴展閱讀

介面劃分標准

同步串列介面(英文:SynchronousSerialInterface,SSI)是一種常用的工業用通信介面。。

非同步串列是指UART(Universal Asynchronous Receiver/Transmitter),通用非同步接收/發送。UART是一個並行輸入成為串列輸出的晶元,通常集成在主板上。UART包含TTL電平的串口和RS232電平的串口。 TTL電平是3.3V的,而RS232是負邏輯電平,它定義+5~+12V為低電平,而-12~-5V為高電平,MDS2710、MDS SD4、EL805等是RS232介面,EL806有TTL介面。

串列介面按電氣標准及協議來分包括RS-232-C、RS-422、RS485等。RS-232-C、RS-422與RS-485標准只對介面的電氣特性做出規定,不涉及接插件、電纜或協議。

Ⅱ linux下串口通信,第一次可以讀數據,然後往串口寫數據,再讀數據卻讀不出,求幫助

A<==RS232==>B,A和B通信,通過RS232協議,讀數據的話,是讀對方發來的數據;寫數據的話,是給對方寫數據。所以,要用個while死循環,始終監聽串口是否讀到數據。

Ⅲ linux串口通信有哪些實驗

經驗證串口應該沒數據讀所導致讀數據數量0列印hellobuff本身初始化Hello所顯示Hello
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>

int main()
{
static char filename[]="t1.txt" ;
int fd;
int nread, i;
char buff[] = "Hello\n";
if((fd = open(filename,O_RDONLY))<0)
{
return -1;
}
printf("fd = %d\n",fd);
nread = read(fd,buff,8);
//while( (i = read(fd,buff,512) ) > 0 )
// =i ;
printf("nread=%d,%s\n",nread, buff);
close(fd);
return 0;
}

Ⅳ linux系統中串口如何通訊

這個難實現,試試iptables的轉發思路能用嗎?

轉發TCP 8081到xx.xx.xx.xx:
#iptables -t nat -I PREROUTING -p tcp –dport 8081 -j DNAT –to xx.xx.xx.xx

Ⅳ 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串口通信問題

我只想說,你這個要編寫的話,有點麻煩啊,,我不喜歡麻煩~~~~

Ⅶ 如何在linux下進行串口通信

你所描述的情況,有可能是你的驅動沒有安裝。但是這個只是猜測。

Ⅷ linux串口通信問題

首先先要確認你PC上的虛擬機的USB串口是否正常,根據我的經驗,虛擬機上要控制USB口經常會有各種問題,那麼你最好驗證一下,最好是連接兩台PC,即把你的PC上的USB轉串口 連接到 另一台PC的串口上,在另一台PC上確認接收到的字元串是否是連續的。

如果你沒有兩台PC,那麼就試試先在非虛擬機的環境上給你的開發板發送字元串,看你的開發板收到的字元串是否連續。

如果最後確認下來,PC端的虛擬機沒有問題的話,再去查是否是開發板上的接收程序有bug。

Ⅸ linux下怎樣對串口編程

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

熱點內容
聊天軟體編程 發布:2024-09-17 03:00:07 瀏覽:725
linuxoracle安裝路徑 發布:2024-09-17 01:57:29 瀏覽:688
兩個安卓手機照片怎麼同步 發布:2024-09-17 01:51:53 瀏覽:207
cf編譯後沒有黑框跳出來 發布:2024-09-17 01:46:54 瀏覽:249
安卓怎麼禁用應用讀取列表 發布:2024-09-17 01:46:45 瀏覽:524
win10設密碼在哪裡 發布:2024-09-17 01:33:32 瀏覽:662
情逢敵手迅雷下載ftp 發布:2024-09-17 01:32:35 瀏覽:337
安卓如何讓軟體按照步驟自動運行 發布:2024-09-17 01:28:27 瀏覽:197
Z包解壓命令 發布:2024-09-17 01:27:51 瀏覽:221
吉林ipfs存儲伺服器雲主機 發布:2024-09-17 01:27:38 瀏覽:685