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

zhuxincong1989 2012-01-18 08:36:26
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;

}

求各位大神看下
...全文
306 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2012-01-19
  • 打赏
  • 举报
回复
·换串口或换根线
·换驱动
·换电脑
·换用通用串口终端程序
·换人(^_^)
zhuxincong1989 2012-01-19
  • 打赏
  • 举报
回复
确定![Quote=引用 5 楼 zhao4zhong1 的回复:]
引用 3 楼 zhuxincong1989 的回复:
线肯定是插对的

引用 1 楼 zhao4zhong1 的回复:
线没插对位置?

确定线不是自环线?
[/Quote]
zhuxincong1989 2012-01-19
  • 打赏
  • 举报
回复
确定!
赵4老师 2012-01-18
  • 打赏
  • 举报
回复
线没插对位置?
赵4老师 2012-01-18
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zhuxincong1989 的回复:]
线肯定是插对的

引用 1 楼 zhao4zhong1 的回复:
线没插对位置?
[/Quote]
确定线不是自环线?
永远的明日 2012-01-18
  • 打赏
  • 举报
回复
先试试主对主,行不行先··
zhuxincong1989 2012-01-18
  • 打赏
  • 举报
回复
线肯定是插对的[Quote=引用 1 楼 zhao4zhong1 的回复:]
线没插对位置?
[/Quote]
zhuxincong1989 2012-01-18
  • 打赏
  • 举报
回复
一个是虚拟机,一个是真机,虽然是两台电脑~[Quote=引用 1 楼 zhao4zhong1 的回复:]
线没插对位置?
[/Quote]

64,670

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧