Home > Net >  Serial assistants don't get the data
Serial assistants don't get the data

Time:10-13

Bought a high interest, UT61 - E series digital multimeter want to secondary development, using serial assistants can receive multimeter to send data, write their own serial assistants not receive! Seek help from a great god

CodePudding user response:

//data reception function
Void serialPort1_DataReceived_1 (object sender, SerialDataReceivedEventArgs e)
{
If (closing) return;//if you are close, ignore operation, direct return, as soon as possible the completion of a serial port to monitor the thread of a cycle
Try
{
Listening=true;//set the tag that I've started to process the data, for a moment to use the system of the UI,
Int n=serialPort1. BytesToRead;//record first, avoid some reason, the human reason, long operation time between a few times, the cache inconsistency
Byte [] buf=new byte [n].//declare a temporary array to store the data to the serial port of
ReceiveCount +=n;//add receive count
SerialPort1. Read (buf, 0, n);//read buffer data
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//& lt; Protocol parsing & gt;
Bool data_1_catched=false;//whether the cache data capture
//1. The cache data
Buffer. AddRange (buf);
//2. Integrity judgment
While (buffer. Count & gt;=4)//must be at least include the header (2 bytes) + length (1 byte) + check (1 byte)
{
//please don't worry about using & gt;=because & gt;=have and & gt; , & lt; ,=, is a independent operators, not parsed into & gt; Two symbols and=
//2.1 find data head
If (buffer [0]==0 xaa & amp; & Buffer [1]==0 x44)
{
//2.2 to detect the cache data to see if there is a byte of data, if not, just don't do other test and verify the
//the front has limited the rest length & gt;=4, then we will be able to visit here to buffer [2] the length
Int len=buffer [2].//data length
//data complete judgment as a first step, the length will be enough.//len is data length, 4 bytes length is 3 part of the line while the annotation
If (buffer. Count & lt; Len + 4) break;//data not enough to do nothing when
//enough here to ensure that the data length, data found head mark, we began to calculate calibration
//2.3 calibration data, make sure the data correct
//xor check, one by one byte exclusive or get check code
Byte checksum=0;
for (int i=0; I & lt; Len + 3; I++)//len + 3 said before check the location of the
{
Checksum ^=buffer [I];
}
If (checksum!=buffer/len + 3)//if data validation fails, discard the packet of data
{
Buffer. RemoveRange (0, len + 4);//deleted from the cache error data
continue;//continue to the next cycle
}
//at this point, have been found a complete data, we will direct analysis of data, or cached analysis together
//we used here is the cache time, advantage is that if you have any reason, data accumulation in the cache buffer
//have a lot of, that you need to cycle to find the final group, the latest data analysis, only the past data has not been timely processing you
//, don't waste more time, it is also considering the system load can reduce,
Buffer. CopyTo (binary_data_1 0, 0, len + 4);//copy a complete data to a specific data cache
Data_1_catched=true;
Buffer. RemoveRange (0, len + 4);//a correct analysis data, removed from the cache data,
}
The else
{
//this is very important, if the data start not head, the delete data
Buffer. The RemoveAt (0);
}
}
//analysis data
If (data_1_catched)
{
//data are set format to us, so when we find out the data of 1, knew a fixed position is these data, we just show
String data=https://bbs.csdn.net/topics/binary_data_1 [3]. The ToString () "X2" + "" + binary_data_1 [4]. ToString (" X2") + "" +
Binary_data_1 [5]. The ToString () "X2" + "" + binary_data_1 [6]. ToString ()" X2 "+" + "
Binary_data_1 [7]. ToString (" X2 ");
//update interface
This. Invoke ((EventHandler) (delegate {txtReceive. Text=data; }));
}
//if you need any other agreement, if you can have it extended the data_n_catched, often more than our agreement, will also contain Numbers, data to the data for the
//number, is after the optimization of the agreement: the head length + number + + data + check
//& lt;/protocol parsing & gt;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
Builder. The Clear ();//remove the contents of the string constructor
//to access the UI resources, so need to use the invoke method synchronous UI,
This. Invoke ((EventHandler) (delegate
{
//it is displayed as 16 banned
If (chbHEXPlay. Checked)
{
//in turn of joining together the hexadecimal string
Foreach (byte b in buf)
{
Builder. Append (b.T oString (" X2 ") + "");
}
}
The else
{
//directly according to the rules of the ASCII converted to string
Builder. Append (Encoding. The ASCII. Get string (buf));
}
//additional form to be added to the end of the text box, and scroll to the end,
This. TxtReceive. AppendText (builder. The ToString ());
//modify receiving count
LblReceiveCount. Text="Get:" + receiveCount. ToString ();
}));
}
The finally
{
Listening=false;//I ran out, the UI can close a serial port,
}
}

CodePudding user response:

Great god help genuflect is begged! Thank you very much,,,,

CodePudding user response:

A serial port receive with baud rate data parity configuration are also related, such as watching your complete configuration

CodePudding user response:

Put protocol data simple text inside, sent directly to see, general agreement are checking, a serial port Settings to and instrument,

CodePudding user response:

You want to power a four, five grounded

CodePudding user response:

Using device - monitoring - studio. 7.25 the serial port monitoring software to look at, a serial port of open, closed, send and receive data can see,
It is useful to
  •  Tags:  
  • C#
  • Related