用过SerialPort类的高手进来看下

yeedward 2004-08-20 09:16:53
我在使用SerialPort的时候发现该类只在读ASC码的时候正常

例如发送‘1’ 它读的是49

但是在读16进制数的时候就有问题了

16进制的123 读进去的是18 读12 也是18请问 这是怎么回事啊




手上分不多 就这些了
...全文
140 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeedward 2004-08-20
  • 打赏
  • 举报
回复
sorry 我对发送源的理解有问题 浪费大家的时间了 不好意思 谢谢大家的帮忙
top_hipster 2004-08-20
  • 打赏
  • 举报
回复
数据的本质是一样的,读0x123和0x12都只读到了0x12所以你只读到了18,还是检查一下具体的读数据的数据类型和内存的使用吧
yeedward 2004-08-20
  • 打赏
  • 举报
回复
void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat)
{
BOOL bRead = TRUE;
BOOL bResult = TRUE;
DWORD dwError = 0;
DWORD BytesRead = 0;
unsigned char RXBuff;//这里就是读的数据 我改成INT也不行

for (;;)
{
// Gain ownership of the comm port critical section.
// This process guarantees no other part of this program
// is using the port object.

EnterCriticalSection(&port->m_csCommunicationSync);

// ClearCommError() will update the COMSTAT structure and
// clear any other errors.

bResult = ClearCommError(port->m_hComm, &dwError, &comstat);

LeaveCriticalSection(&port->m_csCommunicationSync);

// start forever loop. I use this type of loop because I
// do not know at runtime how many loops this will have to
// run. My solution is to start a forever loop and to
// break out of it when I have processed all of the
// data available. Be careful with this approach and
// be sure your loop will exit.
// My reasons for this are not as clear in this sample
// as it is in my production code, but I have found this
// solutiion to be the most efficient way to do this.

if (comstat.cbInQue == 0)
{
// break out when all bytes have been read
break;
}

EnterCriticalSection(&port->m_csCommunicationSync);

if (bRead)
{
bResult = ReadFile(port->m_hComm,// Handle to COMMport
&RXBuff, // RX Buffer Pointer
1, // Read one byte
&BytesRead,// Stores number of bytes read
&port->m_ov);// pointer to the m_ov structure
// deal with the error code
if (!bResult)
{
switch (dwError = GetLastError())
{
case ERROR_IO_PENDING:
{
// asynchronous i/o is still in progress
// Proceed on to GetOverlappedResults();
bRead = FALSE;
break;
}
default:
{
// Another error has occured. Process this error.
port->ProcessErrorMessage("ReadFile()");
break;
}
}
}
else
{
// ReadFile() returned complete. It is not necessary to call GetOverlappedResults()
bRead = TRUE;
}
} // close if (bRead)

if (!bRead)
{
bRead = TRUE;
bResult = GetOverlappedResult(port->m_hComm, // Handle to COMM port
&port->m_ov, // Overlapped structure
&BytesRead, // Stores number of bytes read
TRUE); // Wait flag

// deal with the error code
if (!bResult)
{
port->ProcessErrorMessage("GetOverlappedResults() in ReadFile()");
}
} // close if (!bRead)

LeaveCriticalSection(&port->m_csCommunicationSync);

// notify parent that a byte was received
::SendMessage((port->m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM) RXBuff,
(LPARAM) port->m_nPortNr);
} // end forever loop

}
读数据的代码
yeedward 2004-08-20
  • 打赏
  • 举报
回复
兔子 你用过CSerialPort没有 就是这里的问题啊
我的程序从这里开始就有问题 程序接受ASC码发送的数据很好 但是接收16进制的数据就有问题
例如123 我可以接受‘1’‘2’‘3’但是123就不行 感觉就是不能接收数 只可以接受字符
不知道是怎么回事
Pipi0714 2004-08-20
  • 打赏
  • 举报
回复
把你的代码贴出来,看看我感觉你的思路错了.其实十六进制与十进制来说 10就是10没有什么值得差别只不过表现形式上有所差别在十六进制中他是0xa,其他没有什么!
xindao_wang 2004-08-20
  • 打赏
  • 举报
回复
CSerialPort还是比较好用的,对十六进制的数据同样适用,其实都是存储在内存中的数据,并没有什么区别,但是有可能存储方式不同,比如long int本来为四个字节,但是在压缩方式下,并不是四个字节,可以参考msdn中#pragma 的解释。
yeedward 2004-08-20
  • 打赏
  • 举报
回复
我觉得可能是读入的时候 数据类型的问题
比如说 16进制的数123 ,12读入的都是18 是什么问题呢

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧