Linux和Windows之间的串口通信,为什么无法互相通信

zhuxincong1989 zhuxincong1989 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
Linux:
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<errno.h>
#include<fcntl.h>
#include<unistd.h>
#include<termios.h>
#include<stdlib.h>
#include<sys/time.h>
#include<sys/ioctl.h>


//com bit,pid,bytes
int set_opt(int fd,int nSpeed,int nBits,char nEvent,int nStop)
{
struct termios newtio,oldtio;
if(tcgetattr(fd,&oldtio)!=0)
{
perror("SetupSerial 1");
return -1;
}

bzero(&newtio,sizeof(newtio));
newtio.c_cflag =newtio.c_cflag | CLOCAL | CREAD;
newtio.c_cflag=newtio.c_cflag & ~CSIZE;
//bit
switch(nBits)
{
case 5:
newtio.c_cflag |=CS5;
break;
case 6:
newtio.c_cflag |=CS6;
break;
case 7:
newtio.c_cflag |=CS7;
break;
case 8:
newtio.c_cflag |=CS8;
break;
}

//Event
switch(nEvent)
{
case 'O'://ji jiaoyan
newtio.c_cflag |=PARENB;
newtio.c_cflag &=~PARODD;
newtio.c_iflag |=(INPCK|ISTRIP);
break;

case 'E'://ou jiaoyan
newtio.c_iflag |=(INPCK|ISTRIP);
newtio.c_cflag |=PARENB;
newtio.c_cflag |=PARODD;

case 'N'://no jiaoyan
newtio.c_iflag &=~PARENB;
break;

}

//nSpeed
switch(nSpeed)
{
case 2400:
cfsetispeed(&newtio,B2400);
cfsetospeed(&newtio,B2400);
break;

case 4800:
cfsetispeed(&newtio,B4800);
cfsetospeed(&newtio,B4800);
break;

case 9600:
cfsetispeed(&newtio,B9600);
cfsetispeed(&newtio,B9600);

default:
cfsetispeed(&newtio,B9600);
cfsetispeed(&newtio,B9600);
break;
}

if(nStop==1)
{

newtio.c_cflag &=~CSTOPB;
}
else if(nStop==2)
{
newtio.c_cflag &=CSTOPB;
}


newtio.c_cc[VTIME]=0;//read one word wait time;
newtio.c_cc[VMIN]=0;//how bits

tcflush(fd,TCIFLUSH);//if more and more ,it not read,but can jieshou

if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
printf("set done\n");
return 0;
}

//open com
int open_port(int fd,int comport)
{
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;

if(comport==1)
{
fd=open("dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd==-1)
{
perror("can not open serial port");
return -1;
}
else
{
printf("open ttyS0....\n");
}
}


else if(comport==2)
{
fd=open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd==-1)
{
perror("can not open serial port");
return -1;
}
else
{
printf("open ttyS1...\n");
}
}

else if(comport==3)
{
fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
if(fd==-1)
{
perror("can not open serial port");
}
else
{
printf("open ttyS2...\n");
}
}


//FILE zhuangtai

if(fcntl(fd,F_SETFL,0)<0)
{
printf("fcntl failed\n");
}
else
{
printf("fcntl=%d\n",fcntl(fd,F_SETFL,0));
}

// check the equipment
if (isatty(STDIN_FILENO)==0)
{
printf("standard input is not a terminal device\n");
}
else
{
printf("isatty success\n");
}

printf("fd open=%d\n",fd);

return fd;


}


//send data

int data_send(int fd,char *data,int datalen)
{
int len ;
len=write(fd,data,datalen);

if(len==datalen)
{
return len;
}
else
{
tcflush(fd,TCOFLUSH);
return -1;
}
}

//receive data

int data_Recv(int fd,char *data,int datalen,int baudrata)
{
int recvlen,fd_sel;
fd_set fs_read;

struct timeval tv_timeout;
FD_ZERO(&fs_read);
FD_SET(fd,&fs_read);
tv_timeout.tv_sec=5;
tv_timeout.tv_usec=0;


fd_sel=select(fd+1,&fs_read,NULL,NULL,&tv_timeout);

if(fd_sel)
{
recvlen=read(fd,data,datalen);
return(recvlen);
}
else
{
return -1;
}
return recvlen;
}



int main()
{
int fd;
int sendLen,i,j;
char buf[]="hello\n";
if((fd=open_port(fd,2))<0)
{
perror("open_port erroe");
return;
}

if((i=set_opt(fd,9600,8,'N',1))<0)
{
perror("set_opt error");
return;
}
printf("fd=%d\n",fd);

j=0;
while(1)
{
j++;
sendLen=data_send(fd,"helloword!",sizeof("helloword!"));
if(sendLen>0)
{
printf("NO.%d times send %d data hello!word!\n",j,sendLen);
}
else
{
printf("Error!send failen\n");
}
sleep(1);
}

close(fd);
return;
}



Windows:

#include "stdafx.h"
#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <stdio.h>
using namespace std;

HANDLE hComm;

void PrintInfo()
{
//显示信息
printf("[F1]Help\n");
printf("[ESC]Exit \n\n");
}


int _tmain(int argc, _TCHAR* argv[])
{

PrintInfo();


DCB dcb;

//打开串口
hComm = CreateFile(_T("COM1"),GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING ,0,0);
if(hComm == INVALID_HANDLE_VALUE)
{
printf("Error:Can not open COM1 port!\n");
getchar();
return 0;
}
GetCommState(hComm,&dcb);
int sRate,sByteSize,sParity,sStopBite;
dcb.BaudRate=CBR_38400;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;

//设置串口参数
if(!SetCommState(hComm,&dcb))
{
printf("Error:Can not Setup COM port!\n");
CloseHandle(hComm);
getchar();
return 0;
}

printf("Connecting ....\n");
unsigned long lrc;
//控制台循环
while(1)
{
char Buff[1024];
char SendData;
DWORD nBytesRead,dwEvent,dwError;
COMSTAT cs;

Sleep(1);
//读取串口数据并显示
ClearCommError(hComm,&dwError,&cs);
if(cs.cbInQue>sizeof(Buff))
{
PurgeComm(hComm,PURGE_RXCLEAR);
continue;
}
if(cs.cbInQue>0)
{
ReadFile(hComm,Buff,cs.cbInQue,&nBytesRead,NULL);
Buff[cs.cbInQue]='\0';
printf(Buff);
}


//获取键盘键入值并通过串口发送
if(kbhit())
{
SendData=getch();
if(SendData==13) //如果敲入回车键则转换为/r
SendData='\n';
if(SendData==27) //如果为ESC键则退出
break;
if(SendData==59) //如果为F1键则显示帮助
{
PrintInfo();
continue;
}
WriteFile(hComm,&SendData,1,&lrc,NULL);
}

}
CloseHandle(hComm);

return 0;

}

求各位大神看下
...全文

相关推荐

vm虚拟机使用宿主机串口

文档还提供了通过在Fedora12和Ubuntu10.04虚拟机上安装配置串口并进行互相通信的测试案例。

jlgcumt

跨平台串口通信实例:Linux和Windows之间的实践指南

串口通信,全称串行通信,是计算机中常见的数据通信方法之一。通过串行端口(通常为RS-232标准),数据能够在单个信号线中逐位传输,与并行通信相对。串口通信具有成本低、布线简单、适用于长距离传输等优势,广泛应用于计算机与外围设备的连接,如调制解调器、鼠标、打印机等。随着技术的发展,串口通信也扩展到更高级的USB、FireWire等接口。在串口通信中,常用的调试工具有PuTTY、minicom、Termite等。这些工具各有特点,例如PuTTY支持多种协议,而minicom则在Linux系统上更为常见。

美丽回忆一瞬间

VM中linux和windows主机进行串口通信

最近在做关于AIS的内容。为了对AIS电文进行解码,串口收发。 数据有PC机模拟发送,为了调试方便,不用次次将程序放到开发板上运行,所以利用pc主机和虚拟机进行串口通信模拟该过程。 首先需要用到一个软件----虚拟串口软件。具体用法说明可百度,增加两个互相通信的串口,pc机占一个,虚拟机占一个。 http://download.csdn.net/detail/

KimHerojae

跨平台串口通信实例(linux和windows)

本实例着重探讨如何在Linux和Windows操作系统之间实现串口通信,这对于开发者来说是非常实用的技能。串口通信,也称为UART(通用异步收发传输器),是一种允许设备通过串行接口进行数据交换的技术。

qwaszx0822

Qt编写串口通信程序详解

本文档详细介绍了如何使用Qt框架在Windows XP环境下通过第三方库qextserialport进行串口通信程序的编写。作者首先强调了编程环境的选择,并提到了在Windows下的具体操作步骤,包括

yafeilinux
评论 8
收藏 已收藏
分享
返回顶部
zhuxincong1989
C++ 语言 65,214 成员 250,514 内容