Home > Net >  C # serial communication tools, the return value transformation, motor speed feedback
C # serial communication tools, the return value transformation, motor speed feedback

Time:09-23

Byte [] byteRead=new byte [serialPort BytesToRead];
SerialPort. Read (byteRead, 0, byteRead. Length);
SerialPort. DiscardInBuffer ();
String strRcv=null;

If (byteRead. Length & gt; 20)
{
For (int I=21; I & lt; 25. I++)//total 44
{
StrRcv +=byteRead [I]. ToString (" X2 ");//hexadecimal display 21-24

}
Seri_mes_reci. Text +=strRcv + "\ r \ n";
}

This is done by c # serial code to obtain the return value, returns the 44 bytes of data, one is I want to speed information,
Is a string of four bytes in strRcv corresponding hexadecimal number, how can I put it into actual speed,
The following is my of the methods used in c + +, try a feasible
Float the get;
char *p;
P=(char *) & amp; The get;

CodePudding user response:

Use MFC to do the speed of extraction, want to use c # do a
Void CMFCApplication1Dlg: : OnOncommMscomm1 ()
{
The VARIANT data;
COleSafeArray data2.
Cstrings strtemp, buffer;//, buffer2;
LONG len, I;
BYTE Inbyte [2048], temp;
//the UpdateData (TRUE);//read the edit box content
If (m_mscomm GetCommEvent ()==2)//event value of 2 signifies a character
the receive buffer{
Data=(https://bbs.csdn.net/topics/m_mscomm.GetInput);//read buffer
Data2=data;//the VARIANT type variables into ColeSafeArray type
Len=data2. GetOneDimSize (); To effectively data length////

If (len & gt; 0)
{
for (i=0; I & lt; Len. I++)
Data2. GetElement (& amp; I, Inbyte + I);//type into a BYTE array
for (i=0; I & lt; Len. I++)//converts an array type cstrings variable
{
Temp=* (char *) (Inbyte + I);//character
Strtemp. The Format (" % 2 "x, temp);//the character into temporary variables strtemp store
Strtemp. MakeUpper ();//used to convert cstrings character to an uppercase string
Strtemp. Insert (2, "");//insert Spaces between adjacent data
Buffer +=strtemp;//the string into temporary variable buffer storage
}
}
If (buffer. GetLength () & gt; 120)//44 bytes, length of 132
{
Float the get;
char *p;
Int SSS=buffer. GetLength ();
P=(char *) & amp; The get;//casting F0 FB 9 f 46
For (int ii=0; Ii & lt; 4. Ii++)
{
String pre={buffer [60 + 2 * 3]}.
String pre2={buffer [61 + 2 * 3]}.
The pre +=pre2;
//cout & lt; <"The pre:" & lt; int result=0;
for (int i=0; I & lt; 2; I++)
{
The result +=((charToDigit (pre) [I]) * pow (16, 1 - I));
}
//cout & lt; <"The result:" & lt; P [ii]=the result;
}
//buffer2. The Format (" % 3 "f, get);
M_edit10=get; After the get//variable for transcoding speed value
The UpdateData (FALSE);//send the data to control
}

CodePudding user response:

Heard this delicious
Int a=the Convert. ToInt32 (" 12345678 ", 16);

CodePudding user response:

refer to the second floor huakai2418 response:
heard this delicious
Int a=the Convert. ToInt32 (" 12345678 ", 16);


Float the get;
char *p;
P=(char *) & amp; The get;//casting F0 FB 9 f 46
As above, I am in c + + is to float a pointer to a CHAR, is to float the four bytes, only took the first byte to CHAR,
So will in c #

CodePudding user response:

My serial port, the return value is a structure containing the voltage and current, motor speed information, such as a total of 44 bytes, speed information of 4 bytes, such as the speed of information at a certain moment I now extracted for ACFC44E4, want to give it to me to see more understand speed, conversion should be 2048 or so, speed information is in the structure of the float type

CodePudding user response:

reference lc1255694986 reply: 3/f
Quote: refer to the second floor huakai2418 response:

Heard this delicious
Int a=the Convert. ToInt32 (" 12345678 ", 16);


Float the get;
char *p;
P=(char *) & amp; The get;//casting F0 FB 9 f 46
As above, I am in c + + is to float a pointer to a CHAR, is to float the four bytes, only took the first byte to CHAR,
In c # are so

In fact, I think it is not necessary, simply question: is to hand over the serial port of four bytes of the string to an int type

CodePudding user response:

reference 5 floor huakai2418 reply:
Quote: refer to the third floor lc1255694986 response:

Quote: refer to the second floor huakai2418 response:

Heard this delicious
Int a=the Convert. ToInt32 (" 12345678 ", 16);


Float the get;
char *p;
P=(char *) & amp; The get;//casting F0 FB 9 f 46
As above, I am in c + + is to float a pointer to a CHAR, is to float the four bytes, only took the first byte to CHAR,
In c # are so

In fact, I think it is not necessary, simply question: is to hand over the serial port of the four bytes of the string to an int type

Such as I am ACFC44E4 serial returned, it corresponds to the actual motor speed is about 2048, according to you that is more than that to int
  •  Tags:  
  • C#
  • Related