一、修改serialport文件
上一节中可以实现发送数据但是不能接收数据,最后发现是serialport文件有问题,修改如下:
serialport.h:
#ifndef SERIALPORT_H_
#define SERIALPORT_H_
#define WM_COMM_RXCHAR WM_USER+7 //句柄,用以发送消息给主窗体
#include
/** 串口通信类
*
* 本类实现了对串口的基本操作
* 例如监听发到指定串口的数据、发送指定数据到串口
*/
class CSerialPort
{
public:
CSerialPort(void);
~CSerialPort(void);
public:
/** 初始化串口函数
*
* @param: UINT portNo 串口编号,默认值为1,即COM1,注意,尽量不要大于9
* @param: UINT baud 波特率,默认为9600
* @param: char parity 是否进行奇偶校验,'Y'表示需要奇偶校验,'N'表示不需要奇偶校验
* @param: UINT databits 数据位的个数,默认值为8个数据位
* @param: UINT stopsbits 停止位使用格式,默认值为1
* @param: DWORD dwCommEvents 默认为EV_RXCHAR,即只要收发任意一个字符,则产生一个事件
* @return: bool 初始化是否成功
* @note: 在使用其他本类提供的函数前,请先调用本函数进行串口的初始化
* \n本函数提供了一些常用的串口参数设置,若需要自行设置详细的DCB参数,可使用重载函数
* \n本串口类析构时会自动关闭串口,无需额外执行关闭串口
* @see:
*/
bool InitPort(CWnd* pPortOwner, UINT portNo = 1, UINT baud = CBR_9600, char parity = 'N', UINT databits = 8,
UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR);
/** 串口初始化函数
*
* 本函数提供直接根据DCB参数设置串口参数
* @param: UINT portNo
* @param: const LPDCB & plDCB
* @return: bool 初始化是否成功
* @note: 本函数提供用户自定义地串口初始化参数
* @see:
*/
bool InitPort(UINT portNo, const LPDCB& plDCB);
/** 开启监听线程
*
* 本监听线程完成对串口数据的监听,并将接收到的数据打印到屏幕输出
* @return: bool 操作是否成功
* @note: 当线程已经处于开启状态时,返回flase
* @see:
*/
bool OpenListenThread();
/** 关闭监听线程
*
*
* @return: bool 操作是否成功
* @note: 调用本函数后,监听串口的线程将会被关闭
* @see:
*/
bool CloseListenTread();
/** 向串口写数据
*
* 将缓冲区中的数据写入到串口
* @param: unsigned char * pData 指向需要写入串口的数据缓冲区
* @param: unsigned int length 需要写入的数据长度
* @return: bool 操作是否成功
* @note: length不要大于pData所指向缓冲区的大小
* @see:
*/
bool WriteData( char* pData, unsigned int length);
/** 获取串口缓冲区中的字节数
*
*
* @return: UINT 操作是否成功
* @note: 当串口缓冲区中无数据时,返回0
* @see:
*/
UINT GetBytesInCOM();
/** 读取串口接收缓冲区中一个字节的数据
*
*
* @param: char & cRecved 存放读取数据的字符变量
* @return: bool 读取是否成功
* @note:
* @see:
*/
bool ReadChar(char &cRecved);
private:
/** 打开串口
*
*
* @param: UINT portNo 串口设备号
* @return: bool 打开是否成功
* @note:
* @see:
*/
bool openPort(UINT portNo);
/** 关闭串口
*
*
* @return: void 操作是否成功
* @note:
* @see:
*/
void ClosePort();
/** 串口监听线程
*
* 监听来自串口的数据和信息
* @param: void * pParam 线程参数
* @return: UINT WINAPI 线程返回值
* @note:
* @see:
*/
static UINT WINAPI ListenThread(void* pParam);
CWnd* m_pOwner;//窗口句柄,用于跟主窗体进行通信
int mportNo;//记录串口
private:
/** 串口句柄 */
HANDLE m_hComm;
/** 线程退出标志变量 */
static bool s_bExit;
/** 线程句柄 */
volatile HANDLE m_hListenThread;
/** 同步互斥,临界区保护 */
CRITICAL_SECTION m_csCommunicationSync; //!< 互斥操作串口
};
#endif //SERIALPORT_H_
serialport.cpp:
#include "StdAfx.h"
#include "SerialPort.h"
#include
#include
/** 线程退出标志 */
bool CSerialPort::s_bExit = false;
/** 当串口无数据时,sleep至下次查询间隔的时间,单位:秒 */
const UINT SLEEP_TIME_INTERVAL = 5;
CSerialPort::CSerialPort(void)
: m_hListenThread(INVALID_HANDLE_VALUE)
{
m_hComm = INVALID_HANDLE_VALUE;
m_hListenThread = INVALID_HANDLE_VALUE;
InitializeCriticalSection(&m_csCommunicationSync);
}
CSerialPort::~CSerialPort(void)
{
CloseListenTread();
ClosePort();
DeleteCriticalSection(&m_csCommunicationSync);
}
bool CSerialPort::InitPort(CWnd* pPortOwner, UINT portNo /*= 1*/, UINT baud /*= CBR_9600*/, char parity /*= 'N'*/,
UINT databits /*= 8*/, UINT stopsbits /*= 1*/, DWORD dwCommEvents /*= EV_RXCHAR*/)
{
m_pOwner = pPortOwner;
/** 临时变量,将制定参数转化为字符串形式,以构造DCB结构 */
char szDCBparam[50];
sprintf_s(szDCBparam, "baud=%d parity=%c data=%d stop=%d", baud, parity, databits, stopsbits);
/** 打开指定串口,该函数内部已经有临界区保护,上面请不要加保护 */
if (!openPort(portNo))
{
return false;
}
/** 进入临界段 */
EnterCriticalSection(&m_csCommunicationSync);
/** 是否有错误发生 */
BOOL bIsSuccess = TRUE;
/** 在此可以设置输入输出的缓冲区大小,如果不设置,则系统会设置默认值.
* 自己设置缓冲区大小时,要注意设置稍大一些,避免缓冲区溢出
*/
/*if (bIsSuccess )
{
bIsSuccess = SetupComm(m_hComm,10,10);
}*/
/** 设置串口的超时时间,均设为0,表示不使用超时限制 */
COMMTIMEOUTS CommTimeouts;
CommTimeouts.ReadIntervalTimeout = 0;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;
CommTimeouts.WriteTotalTimeoutConstant = 0;
if (bIsSuccess)
{
bIsSuccess = SetCommTimeouts(m_hComm, &CommTimeouts);
}
DCB dcb;
if (bIsSuccess)
{
// 将ANSI字符串转换为UNICODE字符串
/* DWORD dwNum = MultiByteToWideChar(CP_ACP, 0, szDCBparam, -1, NULL, 0);
wchar_t *pwText = new wchar_t[dwNum];
if (!MultiByteToWideChar(CP_ACP, 0, szDCBparam, -1, pwText, dwNum))
{
bIsSuccess = TRUE;
}*/
/** 获取当前串口配置参数,并且构造串口DCB参数 */
bIsSuccess = GetCommState(m_hComm, &dcb) && BuildCommDCB(szDCBparam, &dcb);
/** 开启RTS flow控制 */
dcb.fRtsControl = RTS_CONTROL_ENABLE;
/** 释放内存空间 */
//delete[] pwText;
}
if (bIsSuccess)
{
/** 使用DCB参数配置串口状态 */
bIsSuccess = SetCommState(m_hComm, &dcb);
}
/** 清空串口缓冲区 */
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
/** 离开临界段 */
LeaveCriticalSection(&m_csCommunicationSync);
return bIsSuccess == TRUE;
}
bool CSerialPort::InitPort(UINT portNo, const LPDCB& plDCB)
{
/** 打开指定串口,该函数内部已经有临界区保护,上面请不要加保护 */
if (!openPort(portNo))
{
return false;
}
/** 进入临界段 */
EnterCriticalSection(&m_csCommunicationSync);
/** 配置串口参数 */
if (!SetCommState(m_hComm, plDCB))
{
return false;
}
/** 清空串口缓冲区 */
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
/** 离开临界段 */
LeaveCriticalSection(&m_csCommunicationSync);
return true;
}
void CSerialPort::ClosePort()
{
/** 如果有串口被打开,关闭它 */
if (m_hComm != INVALID_HANDLE_VALUE)
{
CloseHandle(m_hComm);
m_hComm = INVALID_HANDLE_VALUE;
}
}
bool CSerialPort::openPort(UINT portNo)
{
/** 进入临界段 */
EnterCriticalSection(&m_csCommunicationSync);
mportNo = portNo;
/** 把串口的编号转换为设备名 */
char szPort[50];
sprintf_s(szPort, "COM%d", portNo);
/** 打开指定的串口 */
m_hComm = CreateFileA(szPort, /** 设备名,COM1,COM2等 */
GENERIC_READ | GENERIC_WRITE, /** 访问模式,可同时读写 */
0, /** 共享模式,0表示不共享 */
NULL, /** 安全性设置,一般使用NULL */
OPEN_EXISTING, /** 该参数表示设备必须存在,否则创建失败 */
0,
0);
/** 如果打开失败,释放资源并返回 */
if (m_hComm == INVALID_HANDLE_VALUE)
{
LeaveCriticalSection(&m_csCommunicationSync);
return false;
}
/** 退出临界区 */
LeaveCriticalSection(&m_csCommunicationSync);
return true;
}
bool CSerialPort::OpenListenThread()
{
/** 检测线程是否已经开启了 */
if (m_hListenThread != INVALID_HANDLE_VALUE)
{
/** 线程已经开启 */
return false;
}
s_bExit = false;
/** 线程ID */
UINT threadId;
/** 开启串口数据监听线程 */
m_hListenThread = (HANDLE)_beginthreadex(NULL, 0, ListenThread, this, 0, &threadId);
if (!m_hListenThread)
{
return false;
}
/** 设置线程的优先级,高于普通线程 */
if (!SetThreadPriority(m_hListenThread, THREAD_PRIORITY_ABOVE_NORMAL))
{
return false;
}
return true;
}
bool CSerialPort::CloseListenTread()
{
if (m_hListenThread != INVALID_HANDLE_VALUE)
{
/** 通知线程退出 */
s_bExit = true;
/** 等待线程退出 */
Sleep(10);
/** 置线程句柄无效 */
CloseHandle(m_hListenThread);
m_hListenThread = INVALID_HANDLE_VALUE;
}
return true;
}
UINT CSerialPort::GetBytesInCOM()
{
DWORD dwError = 0; /** 错误码 */
COMSTAT comstat; /** COMSTAT结构体,记录通信设备的状态信息 */
memset(&comstat, 0, sizeof(COMSTAT));
UINT BytesInQue = 0;
/** 在调用ReadFile和WriteFile之前,通过本函数清除以前遗留的错误标志 */
if (ClearCommError(m_hComm, &dwError, &comstat))
{
BytesInQue = comstat.cbInQue; /** 获取在输入缓冲区中的字节数 */
}
return BytesInQue;
}
UINT WINAPI CSerialPort::ListenThread(void* pParam)
{
/** 得到本类的指针 */
CSerialPort *pSerialPort = reinterpret_cast(pParam);
// 线程循环,轮询方式读取串口数据
while (!pSerialPort->s_bExit)
{
UINT BytesInQue = pSerialPort->GetBytesInCOM();
/** 如果串口输入缓冲区中无数据,则休息一会再查询 */
if (BytesInQue == 0)
{
Sleep(SLEEP_TIME_INTERVAL);
continue;
}
/** 读取输入缓冲区中的数据并输出显示 */
char cRecved = 0x00;
do
{
cRecved = 0x00;
if (pSerialPort->ReadChar(cRecved) == true)
{
//std::cout << cRecved;
continue;
}
} while (--BytesInQue);
}
return 0;
}
bool CSerialPort::ReadChar(char &cRecved)
{
BOOL bResult = TRUE;
DWORD BytesRead = 0;
if (m_hComm == INVALID_HANDLE_VALUE)
{
return false;
}
/** 临界区保护 */
EnterCriticalSection(&m_csCommunicationSync);
/** 从缓冲区读取一个字节的数据 */
bResult = ReadFile(m_hComm, &cRecved, 1, &BytesRead, NULL);
if ((!bResult))
{
/** 获取错误码,可以根据该错误码查出错误原因 */
DWORD dwError = GetLastError();
/** 清空串口缓冲区 */
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_RXABORT);
LeaveCriticalSection(&m_csCommunicationSync);
return false;
}
/** 离开临界区 */
LeaveCriticalSection(&m_csCommunicationSync);
::SendMessage((m_pOwner)->m_hWnd, WM_COMM_RXCHAR, (WPARAM)cRecved, (LPARAM)mportNo);
return (BytesRead == 1);
}
bool CSerialPort::WriteData( char* pData, unsigned int length)
{
BOOL bResult = TRUE;
DWORD BytesToSend = 0;
if (m_hComm == INVALID_HANDLE_VALUE)
{
return false;
}
/** 临界区保护 */
EnterCriticalSection(&m_csCommunicationSync);
/** 向缓冲区写入指定量的数据 */
bResult = WriteFile(m_hComm, pData, length, &BytesToSend, NULL);
if (!bResult)
{
DWORD dwError = GetLastError();
/** 清空串口缓冲区 */
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_RXABORT);
LeaveCriticalSection(&m_csCommunicationSync);
return false;
}
/** 离开临界区 */
LeaveCriticalSection(&m_csCommunicationSync);
return true;
}
二、对话框相关函数
1.开关串口
void IO::OnBnClickedButton1() //打开串口
{
// TODO: 在此添加控件通知处理程序代码
CString portnums;
GetDlgItemText(IDC_IO, portnums);
unsigned int flag = _ttoi(portnums);//将其转化为数值类型
MyPublicData.Public_com = flag;
if (!Isportopen)
{
if (!mySerialPort.InitPort(this, flag, CBR_9600, 'N', 8U, 1U, EV_RXCHAR)) MessageBox("initPort fail !");
if (!mySerialPort.OpenListenThread()) MessageBox("OpenListenThread fail !");
Isportopen = 1;
}
else if (!mySerialPort.OpenListenThread()) MessageBox("OpenListenThread fail !");
m_StateLed11.SetIcon(m_hIconLedGreen);
}
void IO::OnBnClickedguan() //关闭串口
{
// TODO: 在此添加控件通知处理程序代码
mySerialPort.CloseListenTread();
m_StateLed11.SetIcon(m_hIconLedRed);
}
2.发送数据
void IO::OnBnClickedButton17() //发送数据
{
// TODO: 在此添加控件通知处理程序代码
CString buffer;
GetDlgItemText(IDC_SEND, buffer);
CByteArray receivedata;
char *a = NULL;
a = new char[];
int receivedatalen;
receivedatalen = Str2Hex(buffer, receivedata);
for (int i = 0; i < receivedatalen; i++)
{
a[i] = receivedata[i];
}
mySerialPort.WriteData(a, receivedatalen);
a = NULL;
}
清空编辑框;
void IO::OnBnClickedButton16()
{
// TODO: 在此添加控件通知处理程序代码
receivedt = "";
UpdateData(FALSE);//读取编辑框内容
//方法一:关联变量,把变量的值给空,再UpdateData(FALSE);
//方法二:直接得到控件的指针,再设置其为空。如:
//GetDlgItem(IDC_XXX)->SetWindowText(_T(""));//给它空的字符串,就没东西了。
}
3.接收数据
//声明一个消息响应函数
afx_msg LONG OnComm(WPARAM ch, LPARAM port);//串口字符接收消息
//串口消息映射
ON_MESSAGE(WM_COMM_RXCHAR, OnComm)
//消息响应函数
LRESULT IO::OnComm(WPARAM ch, LPARAM port)
{
receivedt += (byte)ch;
rx_data[rx_count] = (byte)ch;
rx_count++;
CByteArray receivedata;
int receivedatalen;
receivedatalen = Str2Hex(rx_data, receivedata);
UpdateData(FALSE);
if (rx_count > 109)
{
int data36 = receivedata[0] + receivedata[1] + receivedata[2] + receivedata[3] + receivedata[4] + receivedata[5] + receivedata[6] + receivedata[7] + receivedata[8] + receivedata[9]
+ receivedata[10] + receivedata[11] + receivedata[12] + receivedata[13] + receivedata[14] + receivedata[15] + receivedata[16] + receivedata[17] + receivedata[18] + receivedata[19]
+ receivedata[20] + receivedata[21] + receivedata[22] + receivedata[23] + receivedata[24] + receivedata[25] + receivedata[26] + receivedata[27] + receivedata[28] + receivedata[29]
+ receivedata[30] + receivedata[31] + receivedata[32] + receivedata[33] + receivedata[34],
data37 = (data36 & 0xff00) >> 8, data38 = (data36 & 0xff);
/*************************************接收数据处理****************************************/
if ((receivedata[35] == data37) && (receivedata[36] == data38))
{
if ((receivedata[2] == 0x01))
{
if (receivedata[3] == 0x00) { m_StateLed1.SetIcon(m_hIconLedRed), m_StateLed2.SetIcon(m_hIconLedRed), m_StateLed3.SetIcon(m_hIconLedRed), m_StateLed4.SetIcon(m_hIconLedRed); }
if (receivedata[3] == 0x01) { m_StateLed1.SetIcon(m_hIconLedRed), m_StateLed2.SetIcon(m_hIconLedRed), m_StateLed3.SetIcon(m_hIconLedRed), m_StateLed4.SetIcon(m_hIconLedGreen); }
if (receivedata[3] == 0x02) { m_StateLed1.SetIcon(m_hIconLedRed), m_StateLed2.SetIcon(m_hIconLedRed), m_StateLed3.SetIcon(m_hIconLedGreen), m_StateLed4.SetIcon(m_hIconLedRed); }
if (receivedata[3] == 0x03) { m_StateLed1.SetIcon(m_hIconLedRed), m_StateLed2.SetIcon(m_hIconLedRed), m_StateLed3.SetIcon(m_hIconLedGreen), m_StateLed4.SetIcon(m_hIconLedGreen); }
if (receivedata[3] == 0x04) { m_StateLed1.SetIcon(m_hIconLedRed), m_StateLed2.SetIcon(m_hIconLedGreen), m_StateLed3.SetIcon(m_hIconLedRed), m_StateLed4.SetIcon(m_hIconLedRed); }
if (receivedata[3] == 0x05) { m_StateLed1.SetIcon(m_hIconLedRed), m_StateLed2.SetIcon(m_hIconLedGreen), m_StateLed3.SetIcon(m_hIconLedRed), m_StateLed4.SetIcon(m_hIconLedGreen); }
if (receivedata[3] == 0x06) { m_StateLed1.SetIcon(m_hIconLedRed), m_StateLed2.SetIcon(m_hIconLedGreen), m_StateLed3.SetIcon(m_hIconLedGreen), m_StateLed4.SetIcon(m_hIconLedRed); }
if (receivedata[3] == 0x07) { m_StateLed1.SetIcon(m_hIconLedRed), m_StateLed2.SetIcon(m_hIconLedGreen), m_StateLed3.SetIcon(m_hIconLedGreen), m_StateLed4.SetIcon(m_hIconLedGreen); }
if (receivedata[3] == 0x08) { m_StateLed1.SetIcon(m_hIconLedGreen), m_StateLed2.SetIcon(m_hIconLedRed), m_StateLed3.SetIcon(m_hIconLedRed), m_StateLed4.SetIcon(m_hIconLedRed); }
if (receivedata[3] == 0x09) { m_StateLed1.SetIcon(m_hIconLedGreen), m_StateLed2.SetIcon(m_hIconLedRed), m_StateLed3.SetIcon(m_hIconLedRed), m_StateLed4.SetIcon(m_hIconLedGreen); }
if (receivedata[3] == 0x0A) { m_StateLed1.SetIcon(m_hIconLedGreen), m_StateLed2.SetIcon(m_hIconLedRed), m_StateLed3.SetIcon(m_hIconLedGreen), m_StateLed4.SetIcon(m_hIconLedRed); }
if (receivedata[3] == 0x0B) { m_StateLed1.SetIcon(m_hIconLedGreen), m_StateLed2.SetIcon(m_hIconLedRed), m_StateLed3.SetIcon(m_hIconLedGreen), m_StateLed4.SetIcon(m_hIconLedGreen); }
if (receivedata[3] == 0x0C) { m_StateLed1.SetIcon(m_hIconLedGreen), m_StateLed2.SetIcon(m_hIconLedGreen), m_StateLed3.SetIcon(m_hIconLedRed), m_StateLed4.SetIcon(m_hIconLedRed); }
if (receivedata[3] == 0x0D) { m_StateLed1.SetIcon(m_hIconLedGreen), m_StateLed2.SetIcon(m_hIconLedGreen), m_StateLed3.SetIcon(m_hIconLedRed), m_StateLed4.SetIcon(m_hIconLedGreen); }
if (receivedata[3] == 0x0E) { m_StateLed1.SetIcon(m_hIconLedGreen), m_StateLed2.SetIcon(m_hIconLedGreen), m_StateLed3.SetIcon(m_hIconLedGreen), m_StateLed4.SetIcon(m_hIconLedRed); }
if (receivedata[3] == 0x0F) { m_StateLed1.SetIcon(m_hIconLedGreen), m_StateLed2.SetIcon(m_hIconLedGreen), m_StateLed3.SetIcon(m_hIconLedGreen), m_StateLed4.SetIcon(m_hIconLedGreen); }
if (receivedata[4] == 0x00) { m_StateLed5.SetIcon(m_hIconLedRed), m_StateLed6.SetIcon(m_hIconLedRed), m_StateLed7.SetIcon(m_hIconLedRed), m_StateLed8.SetIcon(m_hIconLedRed); }
if (receivedata[4] == 0x01) { m_StateLed5.SetIcon(m_hIconLedRed), m_StateLed6.SetIcon(m_hIconLedRed), m_StateLed7.SetIcon(m_hIconLedRed), m_StateLed8.SetIcon(m_hIconLedGreen); }
if (receivedata[4] == 0x02) { m_StateLed5.SetIcon(m_hIconLedRed), m_StateLed6.SetIcon(m_hIconLedRed), m_StateLed7.SetIcon(m_hIconLedGreen), m_StateLed8.SetIcon(m_hIconLedRed); }
if (receivedata[4] == 0x03) { m_StateLed5.SetIcon(m_hIconLedRed), m_StateLed6.SetIcon(m_hIconLedRed), m_StateLed7.SetIcon(m_hIconLedGreen), m_StateLed8.SetIcon(m_hIconLedGreen); }
if (receivedata[4] == 0x04) { m_StateLed5.SetIcon(m_hIconLedRed), m_StateLed6.SetIcon(m_hIconLedGreen), m_StateLed7.SetIcon(m_hIconLedRed), m_StateLed8.SetIcon(m_hIconLedRed); }
if (receivedata[4] == 0x05) { m_StateLed5.SetIcon(m_hIconLedRed), m_StateLed6.SetIcon(m_hIconLedGreen), m_StateLed7.SetIcon(m_hIconLedRed), m_StateLed8.SetIcon(m_hIconLedGreen); }
if (receivedata[4] == 0x06) { m_StateLed5.SetIcon(m_hIconLedRed), m_StateLed6.SetIcon(m_hIconLedGreen), m_StateLed7.SetIcon(m_hIconLedGreen), m_StateLed8.SetIcon(m_hIconLedRed); }
if (receivedata[4] == 0x07) { m_StateLed5.SetIcon(m_hIconLedRed), m_StateLed6.SetIcon(m_hIconLedGreen), m_StateLed7.SetIcon(m_hIconLedGreen), m_StateLed8.SetIcon(m_hIconLedGreen); }
if (receivedata[4] == 0x08) { m_StateLed5.SetIcon(m_hIconLedGreen), m_StateLed6.SetIcon(m_hIconLedRed), m_StateLed7.SetIcon(m_hIconLedRed), m_StateLed8.SetIcon(m_hIconLedRed); }
if (receivedata[4] == 0x09) { m_StateLed5.SetIcon(m_hIconLedGreen), m_StateLed6.SetIcon(m_hIconLedRed), m_StateLed7.SetIcon(m_hIconLedRed), m_StateLed8.SetIcon(m_hIconLedGreen); }
if (receivedata[4] == 0x0A) { m_StateLed5.SetIcon(m_hIconLedGreen), m_StateLed6.SetIcon(m_hIconLedRed), m_StateLed7.SetIcon(m_hIconLedGreen), m_StateLed8.SetIcon(m_hIconLedRed); }
if (receivedata[4] == 0x0B) { m_StateLed5.SetIcon(m_hIconLedGreen), m_StateLed6.SetIcon(m_hIconLedRed), m_StateLed7.SetIcon(m_hIconLedGreen), m_StateLed8.SetIcon(m_hIconLedGreen); }
if (receivedata[4] == 0x0C) { m_StateLed5.SetIcon(m_hIconLedGreen), m_StateLed6.SetIcon(m_hIconLedGreen), m_StateLed7.SetIcon(m_hIconLedRed), m_StateLed8.SetIcon(m_hIconLedRed); }
if (receivedata[4] == 0x0D) { m_StateLed5.SetIcon(m_hIconLedGreen), m_StateLed6.SetIcon(m_hIconLedGreen), m_StateLed7.SetIcon(m_hIconLedRed), m_StateLed8.SetIcon(m_hIconLedGreen); }
if (receivedata[4] == 0x0E) { m_StateLed5.SetIcon(m_hIconLedGreen), m_StateLed6.SetIcon(m_hIconLedGreen), m_StateLed7.SetIcon(m_hIconLedGreen), m_StateLed8.SetIcon(m_hIconLedRed); }
if (receivedata[4] == 0x0F) { m_StateLed5.SetIcon(m_hIconLedGreen), m_StateLed6.SetIcon(m_hIconLedGreen), m_StateLed7.SetIcon(m_hIconLedGreen), m_StateLed8.SetIcon(m_hIconLedGreen); }
}
}
else MessageBox("串口数据有误,请清空接收区!");
}
return 0;
}