当前位置:首页>科技>正文

Linux下QT串口通信,收不到数据??? MFC下串口通信程序, 接收不到数据

2023-04-29 12:27:45 互联网 未知 科技

 Linux下QT串口通信,收不到数据??? MFC下串口通信程序, 接收不到数据

Linux下QT串口通信,收不到数据???

#include "posix_qextserialport.h"
#include <QMessageBox>
#include <QIODevice>

Posix_QextSerialPort *myCom

void Form7::openCom()
{
myCom = new Posix_QextSerialPort("/dev/ttyS0")
myCom->open()
}

void Form7::senddata()
{

myCom->setBaudRate(BAUD115200)
myCom->setFlowControl(FLOW_OFF)
myCom->setParity(PAR_NONE)
myCom->setDataBits(DATA_8)
myCom->setStopBits(STOP_1)
myCom->setTimeout(5,0)

int a=myCom->writeBlock("1",1)

QString str
str.setNum(a)
QMessageBox::warning(this,"a",str)
}

void Form7::readCom()
{
char buf[256]={0}
int a= myCom->readBlock(buf,256)
QString str
str.setNum(a)
QMessageBox::warning(this,"a",str)
this->lineEdit6->setText(buf)
}
void Form7::Nclose()
{
myCom->close()
}

MFC下串口通信程序, 接收不到数据

最好用线程来接受程序

static UINT RevThreadProc2(LPVOID pParam)
{
HANDLEh_gEvent
DWORDdwBytesRead
OVERLAPPEDOverlapped
BOOL bReadStatus = FALSE
dwBytesRead = 0
//MessageBox("abc")
memset(&Overlapped,0,sizeof(OVERLAPPED))
h_gEvent = NULL
h_gEvent = CreateEvent(
NULL,
TRUE,//手工设置事件有无信号
FALSE,//初始化事件为无信号状态
NULL//无名事件对像
)
if(h_gEvent == NULL) return 0
Overlapped.hEvent = h_gEvent//指定一个I/O操作完成后触发的事件
BYTE data[COM_BUFSIZE]
ZeroMemory(data,COM_BUFSIZE)
DWORD dwEvtMask=0
GetCommMask(g_pMainDlg->m_Io0.m_hHandle,&dwEvtMask)
dwEvtMask |=EV_RXCHAR
SetCommMask(g_pMainDlg->m_Io0.m_hHandle,dwEvtMask)
while(g_pMainDlg->m_Io0.m_hHandle)
{
WaitCommEvent(g_pMainDlg->m_Io0.m_hHandle,&dwEvtMask,NULL)
if (g_pMainDlg->m_Io0.m_hHandle == INVALID_HANDLE_VALUE)
{
return 0
}
if ((dwEvtMask&EV_RXCHAR) == EV_RXCHAR)
{
COMSTAT ComStat
DWORD dwLength,dwErrorFlags
ClearCommError(g_pMainDlg->m_Io0.m_hHandle, &dwErrorFlags, &ComStat )
dwLength = ComStat.cbInQue //输入缓冲区接收了多少数据?
if (dwLength > 0)
{
//AfxMessageBox("RevThreadProc2")
bReadStatus = ReadFile( g_pMainDlg->m_Io0.m_hHandle, data,dwLength, &dwBytesRead, &Overlapped)
if(!bReadStatus)
{
if(GetLastError()==ERROR_IO_PENDING)
{
while(!GetOverlappedResult(g_pMainDlg->m_Io0.m_hHandle,
&Overlapped, &dwBytesRead, TRUE ))
{
if(GetLastError() == ERROR_IO_INCOMPLETE)
continue
}
for(DWORD i = 0i{
g_pMainDlg->m_RevData[rec_pos_lineless] = data[i]
rec_pos_lineless
}
if (rec_pos_lineless >= 5)
{
SetStudentUpDownFromCom(g_pMainDlg->m_RevData,rec_pos_lineless)
rec_pos_lineless = 0
}
}
}
else
{
for(DWORD i = 0i{
g_pMainDlg->m_RevData[rec_pos_lineless] = data[i]
if (data[i] == 144 || data[i] == 80 ||data[i] == 255)
{
bvn
}
rec_pos_lineless
}
if (bvn >= 3)
{
、、、、、、、、处理接受到的数据
bvn =0
rec_pos_lineless = 0
}
}
}
}
}
ExitThread(0)
CloseHandle(h_gEvent)//读取时的OS
return 0
}
这个是我写的一个线程,可以接受的,希望对你有用

用vb设计串口通信时,不能接收到数据。请高手帮忙!!!

把frmmain.MSComm1.InputMode = 1改成
frmmain.MSComm1.InputMode = 0
我测试过你的代码,用虚拟串口 串口调试软件测试的,把InputMode改成1之后,没有问题啊

串口通信收不到程序急!

串口调试助手挺好用的啊

你的单片机要保证的确有数据发出或是可以接收数据

串口连接线接线方法为2to 3,3 to 2,5to 5
不知道你单片机那边发送和接收是哪两个针脚,在电脑中的2号针脚为发送,3为接收,注意要交叉连接这条线

还有就是
波特率、停止位、奇偶校验、数据位、还有数据流控制(这个一般用不到)
用调试助手时注意选中16进制,因为通迅数据在10进制中可能显示不出来的