//6800FF01000016
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"windows.h"
#include"time.h"
int initMb(HANDLE& mbCom);
void SetCom(HANDLE& hCom);
void CtMbPort(HANDLE& hCom,unsigned char *sendBuffer,unsigned char *exceptedBuffer,unsigned char *recvBuffer);
void Send(HANDLE& hCom,unsigned char *sendBuffer);
int Receive(HANDLE& hCom,unsigned char *recvBuffer);
void SetCom(HANDLE& hCom)
{
SetupComm(hCom,100,100); //输入缓冲区和输出缓冲区的大小都是100
COMMTIMEOUTS TimeOuts;
//设定读超时
TimeOuts.ReadIntervalTimeout=MAXDWORD;
TimeOuts.ReadTotalTimeoutMultiplier=0;
TimeOuts.ReadTotalTimeoutConstant=0;
//在读一次输入缓冲区的内容后读操作就立即返回,
//而不管是否读入了要求的字符。
//设定写超时
TimeOuts.WriteTotalTimeoutMultiplier=100;
TimeOuts.WriteTotalTimeoutConstant=500;
SetCommTimeouts(hCom,&TimeOuts); //设置超时
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate=9600; //波特率为9600
dcb.ByteSize=8; //每个字节有8位
dcb.Parity=NOPARITY; //无奇偶校验位
dcb.StopBits=ONE5STOPBITS; //一个停止位
SetCommState(hCom,&dcb);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
}
void Send(HANDLE& hCom,unsigned char lpOutBuffer[7])
{
// TODO: Add your control notification handler code here
OVERLAPPED m_osWrite;
memset(&m_osWrite,0,sizeof(OVERLAPPED));
m_osWrite.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
DWORD dwBytesWrite=7;
COMSTAT ComStat;
DWORD dwErrorFlags;
BOOL bWriteStat;
ClearCommError(hCom,&dwErrorFlags,&ComStat);
bWriteStat=WriteFile(hCom,lpOutBuffer,
dwBytesWrite,& dwBytesWrite,&m_osWrite);
if(!bWriteStat)
{
if(GetLastError()==ERROR_IO_PENDING)
{
WaitForSingleObject(m_osWrite.hEvent,1000);
}
}
}
int Receive(HANDLE& hCom,unsigned char str[100])
{
// TODO: Add your control notification handler code here
OVERLAPPED m_osRead;
memset(&m_osRead,0,sizeof(OVERLAPPED));
m_osRead.hEvent=CreateEvent(NULL,TRUE,FALSE,NULL);
COMSTAT ComStat;
DWORD dwErrorFlags;
//memset(str,'\0',100);
DWORD dwBytesRead=100;//读取的字节数
BOOL bReadStat;
ClearCommError(hCom,&dwErrorFlags,&ComStat);
dwBytesRead=min(dwBytesRead, (DWORD)ComStat.cbInQue);
bReadStat=ReadFile(hCom,str,
dwBytesRead,&dwBytesRead,&m_osRead);
if(!bReadStat)
{
if(GetLastError()==ERROR_IO_PENDING)
//GetLastError()函数返回ERROR_IO_PENDING,表明串口正在进行读操作
{
WaitForSingleObject(m_osRead.hEvent,2000);
//使用WaitForSingleObject函数等待,直到读操作完成或延时已达到2秒钟
//当串口读操作进行完毕后,m_osRead的hEvent事件会变为有信号
}
}
PurgeComm(hCom, PURGE_TXABORT|
PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
return (strlen((const char *)str));
}
void CtMbPort(HANDLE& hCom,unsigned char sendBuffer[7],unsigned char exceptedBuffer[7],unsigned char recvBuffer[100])
{
for(int i=0;i<256;i++)
{
char comstr[8]="COM";
char tmp[4];
sprintf(tmp,"%d",i);
strcat(comstr,tmp);
hCom=CreateFileA((LPCSTR)comstr,//COM2口
GENERIC_READ|GENERIC_WRITE, //允许读和写
0, //独占方式
NULL,
OPEN_EXISTING, //打开而不是创建
FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, //重叠方式
NULL);
if(hCom!=(HANDLE)-1)
{
printf("%s\n",comstr);
SetCom(hCom);
Send(hCom,sendBuffer) ;
clock_t start, finish;
double duration;
start = clock();
while(true)
{
int len = Receive(hCom,recvBuffer);
if(len>0)
break;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
if(duration>1)
break;
}
//if(!strcmp((const char *)recvBuffer,(const char *)exceptedBuffer))
if((recvBuffer[0]==exceptedBuffer[0])&&(recvBuffer[6]==exceptedBuffer[6]))
{
printf("create main board com port success\n");
return ;
}
CloseHandle(hCom);
}
}
}
int initMb(HANDLE& mbCom)
{
unsigned char sendBuffer[7];
unsigned char exceptedBuffer[7];
unsigned char recvBuffer[100];
memset(sendBuffer,'\0',7);
memset(exceptedBuffer,'\0',7);
memset(recvBuffer,'\0',100);
sendBuffer[0]=0x68;
sendBuffer[1]=0x00;
sendBuffer[2]=0xff;
sendBuffer[3]=0x01;
sendBuffer[4]=0x00;
sendBuffer[5]=0x00;
sendBuffer[6]=0x16;
exceptedBuffer[0]=0x68;
exceptedBuffer[6]=0x16;
recvBuffer[4]=0x00;
recvBuffer[5]=0x01;
CtMbPort(mbCom,sendBuffer,exceptedBuffer,recvBuffer);
if(mbCom==((HANDLE)-1))
return 0;
unsigned char low=recvBuffer[4];
unsigned char high = recvBuffer[5];
if((low==0x00)&&(high==0x00))
return 1;
return 0;
}
int openBox(HANDLE& hCom,int no)
{
unsigned char sendBuffer[7];
unsigned char recvBuffer[100];
sendBuffer[0]=0x68;
sendBuffer[1]=0x00;
sendBuffer[2]=0xff&no;
//printf("%x",sendBuffer[2]);
sendBuffer[3]=0x02;
sendBuffer[4]=0x00;
sendBuffer[5]=0x00;
sendBuffer[6]=0x16;
Send(hCom,sendBuffer) ;
memset(recvBuffer,'\0',100);
clock_t start, finish;
double duration;
start = clock();
while(true)
{
int len = Receive(hCom,recvBuffer);
if(len>0)
break;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
if(duration>1)
break;
}
int len = strlen((const char *)recvBuffer);
if(len==0)
return 0;
unsigned char low=recvBuffer[4];
unsigned char high = recvBuffer[5];
if((low==0x00)&&(high==0x00))
return 1;
return 0;
}
unsigned short int readTemp(HANDLE& hCom )
{//6800ff0601ee16
unsigned char sendBuffer[7];
unsigned char recvBuffer[100];
sendBuffer[0]=0x68;
sendBuffer[1]=0x00;
sendBuffer[2]=0xff;
sendBuffer[3]=0x06;
sendBuffer[4]=0xff;
sendBuffer[5]=0xff;
sendBuffer[6]=0x16;
Send(hCom,sendBuffer);
memset(recvBuffer,'\0',100);
clock_t start, finish;
double duration;
start = clock();
while(true)
{
int len = Receive(hCom,recvBuffer);
if(len>0)
break;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
if(duration>1)
break;
}
int len = strlen((const char *)recvBuffer);
if(len==0)
return 0;
unsigned char ch[]={recvBuffer[5],recvBuffer[4]};
unsigned short int temp = 0;
strncpy((char*)&temp,(const char *)ch,2);
return temp;
}
int main()
{
HANDLE mbCom=NULL;
int r = initMb(mbCom);
if(r == 1)
printf("init mb success!\n");
else
printf("%d\n",r);
// printf("open box status:%d\n",openBox(mbCom,23));
printf("read temp:%d\n" ,readTemp(mbCom ));
if(mbCom!=((HANDLE)-1))
CloseHandle(mbCom);
}
串口通信示例
最新推荐文章于 2022-11-17 14:50:39 发布