在编写串口通讯时,想到是否可以模拟CAN通讯,将接收到的WPARAM类型的变量转变成一个结构体变量,并且在发送时将结构体变量转变成WPARAM变量发送。
1.首先定义结构体
typedef volatile struct
{
union
{
WPARAM R;
struct
{
WPARAM wId : 16;
WPARAM wCmd : 16;
}B;
}wReceiveVal;
}ReceiveStruct;
2.在接收函数中对结构体变量赋值
void CSerialPortMsg::SerialPortReceiveHand(WPARAM buf)
{
g_stReceiveStruct.wReceiveVal.B.wId = HIWORD(buf);
g_stReceiveStruct.wReceiveVal.B.wCmd = LOWORD(buf);
}
3.在发送函数中将结构体变量转变成WPARAM变量
void CSerialPortMsg::SerialPortSendHand(ReceiveStruct stBuf)
{
WPARAM wSendVal = 0;
wSendVal = (stBuf.wReceiveVal.B.wId << 16) + stBuf.wReceiveVal.B.wCmd;
m_cSerialPort.WriteToPort(wSendVal);
}