//#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
//#include <iostream>
#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[i]);
}
}
CloseHandle(hCom);
system("pause");
return str[0];
}
int main()
{
send();
receive();
}
在一個行程里實作串口先后寫和讀(對同一個串口),硬體上短接com的2,3腳,實作自收自發。但運行出來的是寫資料成功,接收有問題,接收快取區沒有資料,自然讀不出來。
經除錯發現,如果在寫完串口沒有關閉的時候,在同一個函式(send)里此刻去讀,就能讀出來。
該怎么樣實作用兩個函式去分別讀寫呢,小妹分不多,傾囊求助。
uj5u.com熱心網友回復:

串口監聽精靈里顯示“寫入成功→IRP_MJ_CLEANUP→關閉串口→打開串口→配置串口→IRP_MJ_CLEANUP→關閉串口,但是并沒有執行讀串口操作,
uj5u.com熱心網友回復:
//#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
//#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <conio.h>
#include "tchar.h"
HANDLE hCom;//全域變數,串口句柄
void open() {
/*****************************打開串口***********************************/
COMMTIMEOUTS TimeOuts;
DCB dcb;
//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);//讀寫串口前清慷訓沖區
}
void send() {
// DWORD dwWriteLen = 0;
int data_in;
unsigned char sendData[1];//寫入串口快取區的資料
DWORD dwWriteLen=0 ;
/*******同步讀寫串口********/
scanf("%x",&data_in);
printf("0x%x\n",data_in);
//sendData[1]=(data_in&0x80) && 1;//拆
sendData[0]=(unsigned char)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("0x%x\n", sendData[0]);
}
void close() {
DWORD dwError;
COMSTAT cs;
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() {
COMMTIMEOUTS TimeOuts;
DCB dcb1;
int i;
DWORD wCount =1;
DWORD wCount1;
DWORD dwError;
//DWORD lpErrors;
unsigned char str[1];
/////////////////////////////////////
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("0x%02X\n", str[i]);
}
}
return (int)(str[0]);
}
int main() {
open();
send();
receive();
close();
system("pause");
}
uj5u.com熱心網友回復:
send程式中用了 scanf , 因此得控制臺輸入,才會有發送行為uj5u.com熱心網友回復:
有輸入,發送成功了。就是重新打開串口進行讀取的時候,快取區就沒有資料了。uj5u.com熱心網友回復:
用同一個檔案句柄 HANDLE,{
CreateFile
while(x)
{
WriteFile
ReadFile
}
CloseHandle
}
uj5u.com熱心網友回復:
重新打開串口肯定清慷訓沖區了,為什么要重新打開串口去接收呢?send完,直接ReadFile啊,流程應該是這樣子,
int main()
{
opencom(...)
send();
receive();
closecom();
}
串口通訊可以使用這個類
CnComm
http://www.cppblog.com/llbird/archive/2009/04/18/80386.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/212750.html
標籤:進程/線程/DLL
上一篇:幫忙寫下VB程式
