Home > Back-end >  Newcomer when learning C serial communication to encounter a few problems, want to ask you recognize
Newcomer when learning C serial communication to encounter a few problems, want to ask you recognize

Time:11-29

I just started to learn c + +, there is a serial communication task, via a serial port to communicate with an electronic device, I surf the Internet to find a serial communication program, adjusted, met with some problems in the process, so an array in the program, which are defined as follows:
unsigned char * temp=new unsigned char [8].//create a dynamic array
The array of data is to write data to the serial port, write data code is as follows:
cout & lt;
About mySerialPort. WriteData () this function are defined as follows:
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;
}

/* * critical region protect */
The EnterCriticalSection (& amp; M_csCommunicationSync);

/* * */data to the buffer specified amount
BResult=WriteFile (m_hComm,/* */pData file handle,/* is used to store a read buffer */8,/* to read the number of characters */& amp; BytesToSend,/* */NULL pointer to the actual reading bytes);
if (! BResult)
{
DWORD dwError=GetLastError ();
/* * */empty serial buffer
PurgeComm (m_hComm, PURGE_RXCLEAR | PURGE_RXABORT);
LeaveCriticalSection (& amp; M_csCommunicationSync);

return false;
}

Critical region */* * leave/
LeaveCriticalSection (& amp; M_csCommunicationSync);

return true;
}

That's the problem, I can't change to transmit an array by the values, I need to select statement to decide to send data to the serial port, but whether the if statement or a switch statement, I wasn't able to value assigned to the array effectively, and I can't change the temp the pointer to the address, I created a few different summer promotion, trying to make the temp to them, but it won't work, and my understanding is temp clearly is a pointer, but I'm writing is similar to the following code:
Unsigned char * temp=& amp; A, [10].//a [10] is defined type unsigned char array
When complains, the reason for the error can't be will not be able to allocate the type unsigned char * [10] to unsigned char types of entities,

Because of my purpose is to send all kinds of control instruction to the device, so I also tried to create multiple arrays, each array to store different instructions, as follows:
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;//create a dynamic array
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;

And then to the serial port to send through select statement decided to use which one temp, but failed, for the first time is to use an if statement, the code is as follows:
int key;
Cout<& lt;" Please enter the command "& lt; Cin> The key;
If (key=1) {
Cout & lt; Cout & lt; }
Else if (key=2) {
Cout & lt; Cout & lt; }
Else if (key=3) {
Cout & lt; Cout & lt; }
Else if (key=4) {
Cout & lt; Cout & lt; }
Else if (key=5) {
Cout & lt; Cout & lt; }
Else if (key=6) {
Cout & lt; Cout & lt; }

Then is to use a switch statement, the code is as follows:
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 & lt; <"I" & lt; }

Above two way or not, whether the input is, device receives instructions are always a fixed value, and can't change
Hope to have a teacher who knew what was wrong with my words can give directions, thank you,
The original application information is as follows:
The author: the dream-seeker glows
Source: CSDN
Original: https://blog.csdn.net/qq_41480046/article/details/82220155

CodePudding user response:

.

CodePudding user response:

We do almost the same

CodePudding user response:

If (key=1) statement is wrong, you should be the if (key==1)

CodePudding user response:

Thank you very much, can now, could you tell me how did you put a serial port to process the data received? I now need to analyze data returned by the serial port, want to ask what to do

CodePudding user response:

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related