Linux和Windows之间的串口通信,为什么无法互相通信
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;
}
求各位大神看下