|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
#include "tchar.h"
void send()
{
/*****************************打开串口***********************************/
HANDLE hCom;//全局变量,串口句柄
COMMTIMEOUTS TimeOuts;
DCB dcb;
DWORD dwWriteLen=0 ;
// DWORD dwWriteLen = 0;
int data_in;
unsigned char sendData[1];//写入串口缓存区的数据
DWORD dwError;
COMSTAT cs;
//int error = GetLastError();
hCom = CreateFile(_T("COM4"),
GENERIC_READ | GENERIC_WRITE,//读写
0,//独占方式
NULL,
OPEN_EXISTING,
0,//同步方式
//FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERAPPLE,//重叠方式
NULL);
if (hCom==INVALID_HANDLE_VALUE )
{
printf("send open failed");
// printf("CreateFile() error:%d", GetLastError());
}
else
{
printf("send open successed\n");
}
/**************配置串口************/
SetupComm(hCom, 20480, 20480);//缓冲区
// COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout = 1000;//读超时
TimeOuts.ReadTotalTimeoutMultiplier = 500;
TimeOuts.ReadTotalTimeoutConstant = 5000;
TimeOuts.WriteTotalTimeoutMultiplier = 500;//写超时
TimeOuts.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(hCom, &TimeOuts);//设置超时
//DCB dcb;
GetCommState(hCom, &dcb);
dcb.BaudRate = 115200;//波特率115200
dcb.ByteSize = 8;//8bit/byte
dcb.Parity = NOPARITY;//无奇偶校验位
dcb.StopBits = ONESTOPBIT;//一个停止位
dcb.fParity = FALSE;
dcb.fNull = FALSE;
SetCommState(hCom, &dcb);
// PurgeComm(hCom, PURGE_TXCLEAR|PURGE_RXCLEAR);//读写串口前清空缓冲区
/*******同步读写串口********/
scanf("%x",&data_in);
printf("%d",&data_in);
//sendData[1]=(data_in&0x80) && 1;//拆
sendData[0]=data_in;//拆
//data_in=data_in<<1;
////////////////查看缓存区字节长度//////////////////
if(!WriteFile(hCom,sendData,1,&dwWriteLen, NULL))
{
printf("send failed\n");
}
printf("send successed\n");
printf("send data is\n");
printf("%x\n", sendData[0]);
// return 1;
if (!ClearCommError(hCom, &dwError, &cs))
{
printf("ClearCommError() failed\n");
}
else
{
printf("ClearCommError() successed\n");
printf("cs.cbOutQue=%d\n",cs.cbOutQue);
printf("cs.cbInQue=%d\n",cs.cbInQue);
}
/////////////////////////////////////
;
/***********关闭串口***********/
CloseHandle(hCom);
}
/********************************************************/
int receive()
{
HANDLE hCom;
COMMTIMEOUTS TimeOuts;
DCB dcb1;
int i;
DWORD wCount =1;
DWORD wCount1;
DWORD dwError;
COMSTAT cs;
//DWORD lpErrors;
unsigned char str[1];
hCom = CreateFile(_T("COM4"),
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hCom==INVALID_HANDLE_VALUE )
{
printf("read open failed\n");
}
else
{
printf("read open successed\n");
}
SetupComm(hCom, 20480, 20480);//缓冲区
// COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout = 1000;//读超时
TimeOuts.ReadTotalTimeoutMultiplier = 500;
TimeOuts.ReadTotalTimeoutConstant = 5000;
TimeOuts.WriteTotalTimeoutMultiplier = 500;//写超时
TimeOuts.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(hCom, &TimeOuts);
// DCB dcb1;
GetCommState(hCom, &dcb1);
dcb1.BaudRate = 115200;
dcb1.ByteSize = 8;//一个字节8bit
dcb1.Parity = NOPARITY;//无校验
dcb1.StopBits = ONESTOPBIT;//1位停止位
dcb1.fParity = FALSE;
dcb1.fNull = FALSE;
SetCommState(hCom, &dcb1);
//PurgeComm(hCom, PURGE_TXCLEAR|PURGE_RXCLEAR);
PurgeComm(hCom, PURGE_TXCLEAR);
////////////////查看缓存区字节长度//////////////////*/
if (!ClearCommError(hCom, &dwError, &cs))
{
printf("ClearCommError() failed\n");
}
else
{
printf("ClearCommError() successed\n");
printf("cs.cbInQue=%d\n",cs.cbInQue);
}
/////////////////////////////////////
if(!ReadFile(hCom, str,wCount , &wCount1, NULL))
{
printf("read failed!\n");
}
else
{
printf("read successed!\n");
printf("read actual length:%d\n", wCount1);
printf("read data is\n");
for(i=0;i<wCount1;i++)
{
printf("%02X\n", str);
}
}
CloseHandle(hCom);
system("pause");
return str[0];
}
int main()
{
send();
receive();
}
在一个进程里实现串口先后写和读(对同一个串口),硬件上短接com的2,3脚,实现自收自发。但运行出来的是写数据成功,接收有问题,接收缓存区没有数据,自然读不出来。 串口监听精灵里显示“写入成功→IRP_MJ_CLEANUP→关闭串口→打开串口→配置串口→IRP_MJ_CLEANUP→关闭串口,但是并没有执行读串口操作,
经调试发现,如果在写完串口没有关闭的时候,在同一个函数(send)里此刻去读,就能读出来。
该怎么样实现用两个函数去分别读写呢,小妹分不多,倾囊求助。
|
|