新人在学习C++串口通信的时候遇到了一些小问题,想向各位大牛们请教一下
谢桥秋池 2019-03-17 04:22:30 本人刚开始学习C++,有一个串口通信的任务,要通过串口与一电子器件进行通信。我上网查找了一段串口通信的程序,自己整理了一下,在这过程中遇到了一些问题。程序中有这么一个数组,其定义如下:
unsigned char *temp = new unsigned char[8];//动态创建一个数组
这个数组中的数据就是要往串口中写入的数据,写入数据的代码如下:
cout << mySerialPort.WriteData(temp, 8) << endl;//这个函数就是给串口发送数据的函数,temp就是要发送的数组。
关于mySerialPort.WriteData()这个函数的定义如下:
bool CSerialPort::WriteData(unsigned char *pData, int length)
{
int *pData1=new int;
BOOL bResult = TRUE;
DWORD BytesToSend = 0;
if (m_hComm == INVALID_HANDLE_VALUE)
{
return false;
}
/** 临界区保护 */
EnterCriticalSection(&m_csCommunicationSync);
/** 向缓冲区写入指定量的数据 */
bResult = WriteFile(m_hComm,/*文件句柄*/pData,/*用于保存读入数据的一个缓冲区*/ 8,/*要读入的字符数*/ &BytesToSend,/*指向实际读取字节数的指针*/ NULL);
if (!bResult)
{
DWORD dwError = GetLastError();
/** 清空串口缓冲区 */
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_RXABORT);
LeaveCriticalSection(&m_csCommunicationSync);
return false;
}
/** 离开临界区 */
LeaveCriticalSection(&m_csCommunicationSync);
return true;
}
问题就出在这里,我无法改变要传输的数组的值,我需要通过选择语句来决定向串口发送的数据,但是无论是if语句还是switch语句,我都无法对这个数组进行有效的赋值,而且我无法改变temp这个指针指向的地址,我创建了几个不同的暑促,尝试着令temp指向它们,但是行不通。还有就是我的理解是temp明明是一个指针,但是我在写入类似于以下代码:
unsigned char *temp = &a[10];//a[10]是定义好的unsigned char类型的数组
的时候会报错,错误原因是不能将不能将unsigned char*[10]的类型分配到unsigned char类型的实体。
由于我的目的是向器件发送各种控制指令,所以我也尝试过创建多个数组,每个数组分别储存不同的指令,如下所示:
unsigned char *temp1 = new unsigned char[9];
temp1[0] = 170, temp1[1] = 85, temp1[2] = 00, temp1[3] = 00, temp1[4] = 07, temp1[5] = 00, temp1[6] = 18, temp1[7] = 25, temp1[8] = 00;//动态创建一个数组
unsigned char *temp2 = new unsigned char[9];
temp2[0] = 170, temp2[1] = 85, temp2[2] = 00, temp2[3] = 00, temp2[4] = 07, temp2[5] = 00, temp2[6] = 65, temp2[7] = 72, temp2[8] = 00;
unsigned char *temp3 = new unsigned char[9];
temp3[0] = 170, temp3[1] = 85, temp3[2] = 00, temp3[3] = 00, temp3[4] = 07, temp3[5] = 00, temp3[6] = 64, temp3[7] = 71, temp3[8] = 00;
unsigned char *temp4 = new unsigned char[9];
temp4[0] = 170, temp4[1] = 85, temp4[2] = 00, temp4[3] = 00, temp4[4] = 07, temp4[5] = 00, temp4[6] = 254, temp4[7] = 05, temp4[8] = 00;
unsigned char *temp5 = new unsigned char[9];
temp5[0] = 170, temp5[1] = 85, temp5[2] = 00, temp5[3] = 00, temp5[4] = 07, temp5[5] = 00, temp5[6] = 193, temp5[7] = 200, temp5[8] = 00;
unsigned char *temp6 = new unsigned char[9];
temp6[0] = 170, temp6[1] = 85, temp6[2] = 00, temp6[3] = 00, temp6[4] = 07, temp6[5] = 00, temp6[6] = 141, temp6[7] = 148, temp6[8] = 00;
然后通过选择语句决定使用向串口发送哪一个temp,但还是失败了,第一次是使用if语句,代码如下:
int key;
cout<<"请输入指令"<<endl;
cin>>key;
if (key = 1) {
cout << mySerialPort.WriteData(temp1, 9) << endl;
cout << mySerialPort.GetBytesInCOM() << endl;
}
else if (key = 2) {
cout << mySerialPort.WriteData(temp2, 9) << endl;
cout << mySerialPort.GetBytesInCOM() << endl;
}
else if (key = 3) {
cout << mySerialPort.WriteData(temp3, 9) << endl;
cout << mySerialPort.GetBytesInCOM() << endl;
}
else if (key = 4) {
cout << mySerialPort.WriteData(temp4, 9) << endl;
cout << mySerialPort.GetBytesInCOM() << endl;
}
else if (key = 5) {
cout << mySerialPort.WriteData(temp5, 9) << endl;
cout << mySerialPort.GetBytesInCOM() << endl;
}
else if (key = 6) {
cout << mySerialPort.WriteData(temp6, 9) << endl;
cout << mySerialPort.GetBytesInCOM() << endl;
}
然后是使用switch语句,代码如下:
switch (key) {
case'1':temp=temp1; break;
case'2':temp=temp2; break;
case'3':temp=temp3; break;
case'4':temp=temp4; break;
case'5':temp=temp5; break;
case'6':temp=temp6; break;
//default:cout << "输入有误" << endl;
}
以上两个方式还是不能实现,无论是输入是什么,器件接收到的指令始终都是某个固定的值,且不能改变
希望有哪位老师知道我错在哪里的话能指点一下,多谢。
程序原文信息如下:
作者:追梦会发光
来源:CSDN
原文:https://blog.csdn.net/qq_41480046/article/details/82220155