Wit-motion private protocol: Wit-motion protocol for short, is the protocol used by Wit-motion sensors, usually TTL or 232 level sensors use Wit-motion protocol; the agreement stipulates that the sensor returns the data packet starting with 55, and the host computer sends the data packet starting with FF AA;
Routine introduction
This routine introduces how to use C# to develop the upper computer to connect to the Wit-motion protocol sensor, receive sensor data and communicate with the sensor;
Before viewing this routine, please read the relevant sensor manual to understand the protocol used by the sensor and the basic functions of the sensor
Routine directory
The routine project directory is as follows
Dll: The dependent files of the project, please import these dlls into your project before running the project.
Form1:There is only one Form window in the routine, all logic codes are in the Form window file, and there are no other files
Turn on the device
The JY901 object represents the JY901 device in the program, and you can communicate with the device through it; when opening the device, you need to specify the serial port number and baud rate of the sensor, and call the JY901.Open() method after specifying
/// <summary>
/// open device
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void openButton_Click(object sender, EventArgs e)
{
// Get the serial port number and baud rate of the connection
string portName;
int baudrate;
try
{
portName = (string)portComboBox.SelectedItem;
baudrate = (int)baudComboBox.SelectedItem;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
// Do not open repeatedly
if (JY901.IsOpen())
{
return;
}
// turn on the device
try
{
JY901.SetPortName(portName);
JY901.SetBaudrate(baudrate);
JY901.Open();
// Implement logging data events
JY901.OnRecord += JY901_OnRecord;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
turn off the device
Just call the JY901.Close() method to close the device
/// <summary>
/// turn off the device
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void closeButton_Click(object sender, EventArgs e)
{
try
{
// Turn off the device if it is already on
if (JY901.IsOpen())
{
JY901.OnRecord -= JY901_OnRecord;
JY901.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
Receive sensor data
Get data
The JY901 object will automatically calculate the sensor data and save it on itself. The sensor data can be obtained through the JY901.GetDeviceData() method. JY901.GetDeviceData() needs to pass in a key to get sensor data. Please check the key that needs to be used in the routine, the key is stored in the WitSensorKey class
/// <summary>
/// Get device data
/// </summary>
private string GetDeviceData(WT901C485 WT901C485)
{
StringBuilder builder = new StringBuilder();
builder.Append(WT901C485.GetDeviceName()).Append("\n");
// acceleration builder.Append("AccX").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AccX)).Append("g \t");
builder.Append("AccY").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AccY)).Append("g \t");
builder.Append("AccZ").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AccZ)).Append("g \n");
// angular velocity
builder.Append("GyroX").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AsX)).Append("°/s \t");
builder.Append("GyroY").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AsY)).Append("°/s \t");
builder.Append("GyroZ").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AsZ)).Append("°/s \n");
// angle
builder.Append("AngleX").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AngleX)).Append("° \t");
builder.Append("AngleY").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AngleY)).Append("° \t");
builder.Append("AngleZ").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.AngleZ)).Append("° \n");
// magnetic field
builder.Append("MagX").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.HX)).Append("uT \t");
builder.Append("MagY").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.HY)).Append("uT \t");
builder.Append("MagZ").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.HZ)).Append("uT \n");
// latitude and longitude
builder.Append("Lon").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.Lon)).Append("′ \t");
builder.Append("Lat").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.Lat)).Append("′ \n");
// port number
builder.Append("D0").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.D0)).Append("\t");
builder.Append("D1").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.D1)).Append("\t");
builder.Append("D2").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.D2)).Append("\t");
builder.Append("D3").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.D3)).Append("\n");
// Quaternion
builder.Append("Q0").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.Q0)).Append("\t");
builder.Append("Q1").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.Q1)).Append("\t");
builder.Append("Q2").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.Q2)).Append("\t");
builder.Append("Q3").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.Q3)).Append("\n");
// barometric pressure
builder.Append("P").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.Q1)).Append("Pa \t");
builder.Append("H").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.Q2)).Append("m \t");
// temperature
builder.Append("T").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.T)).Append("℃ \n");
// GPS
builder.Append("GPSHeight").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.GPSHeight)).Append(" m \t");
builder.Append("GPSYaw").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.GPSYaw)).Append("° \t");
builder.Append("GPSV").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.GPSV)).Append("km/h \n");
// positioning accuracy
builder.Append("PDOP").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.PDOP)).Append("\t");
builder.Append("VDOP").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.VDOP)).Append("\t");
builder.Append("HDOP").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.HDOP)).Append("\n");
// version number
builder.Append("VersionNumber").Append(":").Append(WT901C485.GetDeviceData(WitSensorKey.VersionNumber)).Append("\n");
return builder.ToString();
}
Record data
The data of the sensor can be obtained through the JY901 object, but usually the upper computer needs to record the data of the sensor. JY901 has an OnRecord event that will notify you when to record the data, and the OnRecord event can be realized when the device is turned on; and then through cooperation with JY901.GetDeviceData( ) method to record the data
/// <summary>
/// turn on the device
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void openButton_Click(object sender, EventArgs e)
{
// Get the serial port number and baud rate of the connection
string portName;
int baudrate;
try
{
portName = (string)portComboBox.SelectedItem;
baudrate = (int)baudComboBox.SelectedItem;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
// Do not open repeatedly
if (JY901.IsOpen())
{
return;
}
// turn on the device
try
{
JY901.SetPortName(portName);
JY901.SetBaudrate(baudrate);
JY901.Open();
// Implement logging data events
JY901.OnRecord += JY901_OnRecord;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
/// <summary>
/// This is called when the sensor data is refreshed, you can log the data here
/// </summary>
/// <param name="jY901"></param>
private void JY901_OnRecord(JY901 jY901)
{
string text = GetDeviceData(JY901);
Debug.WriteLine(text);
}
set sensor
The sensor can be operated by the method of JY901
JY901.UnlockReg() Send unlock register command
JY901.AppliedCalibration() Send meter calibration command
JY901.StartFieldCalibration() Send start magnetic field calibration command
JY901.EndFieldCalibration() Send end magnetic field calibration command
JY901.SendProtocolData() Send other commands
Accelerometer Calibration
Apply accumulative calibration to the sensor by calling the JY901.AppliedCalibration() method
/// <summary>
/// Accelerometer Calibration
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void appliedCalibrationButton_Click(object sender, EventArgs e)
{
if (JY901.IsOpen() == false)
{
return;
}
try
{
// Unlock registers and send commands
JY901.UnlockReg();
JY901.AppliedCalibration();
// The following two lines are equivalent to the above, it is recommended to use the above
//JY901.SendProtocolData(new byte[] { 0xff, 0xaa, 0x69, 0x88, 0xb5 });
//JY901.SendProtocolData(new byte[] { 0xff, 0xaa, 0x01, 0x01, 0x00 });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Magnetic Field Calibration
Calibrate the magnetic field of the sensor by calling the JY901.StartFieldCalibration() method and the JY901.EndFieldCalibration() method
/// <summary>
/// Start Magnetic Field Calibration
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void startFieldCalibrationButton_Click(object sender, EventArgs e)
{
if (JY901.IsOpen() == false)
{
return;
}
try
{
// Unlock registers and send commands
JY901.UnlockReg();
JY901.StartFieldCalibration();
// The following two lines are equivalent to the above, it is recommended to use the above
//JY901.SendProtocolData(new byte[] { 0xff, 0xaa, 0x69, 0x88, 0xb5 });
//JY901.SendProtocolData(new byte[] { 0xff, 0xaa, 0x01, 0x07, 0x00 });
MessageBox.Show("To start the magnetic field calibration, please turn around the sensor's XYZ three axes, and click [End Magnetic Field Calibration] after turning】");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// End magnetic field calibration
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void endFieldCalibrationButton_Click(object sender, EventArgs e)
{
if (JY901.IsOpen() == false)
{
return;
}
try
{
// Unlock registers and send commands
JY901.UnlockReg();
JY901.EndFieldCalibration();
// The following two lines are equivalent to the above, it is recommended to use the above
//JY901.SendProtocolData(new byte[] { 0xff, 0xaa, 0x69, 0x88, 0xb5 });
//JY901.SendProtocolData(new byte[] { 0xff, 0xaa, 0x01, 0x00, 0x00 });
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
More
For other operations, please refer to the sensor manual
read sensor register
The register of the sensor can be read through the JY901.SendReadReg() method, or the JY901.SendProtocolData() method can be used
After sending the read command, the register value will be saved in JY901, you need to get the register data through JY901.GetDeviceData()
/// <summary>
/// Read 03 register
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void readReg03Button_Click(object sender, EventArgs e)
{
if (JY901.IsOpen() == false)
{
return;
}
try
{
// waiting time
int waitTime = 150;
// Send a read command and wait for the sensor to return data. If it is not read, you can extend the waitTime, or read it several times
JY901.SendReadReg(0x03, waitTime);
// The following line is equivalent to the above. It is recommended to use the above
//JY901.SendProtocolData(new byte[] { 0xff, 0xaa, 0x27, 0x03, 0x00 }, waitTime);
string reg03Value = JY901.GetDeviceData("03");
MessageBox.Show($"Register 03 value is : {reg03Value}");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
JY901 API
Method
illustrate
Parameter introduction
Return value
void SetPortName(string portName)
Set the serial port to open
PortName: serial port number
void
void SetBaudrate(int baudRate)
Specify the baud rate to open
BaudRate: baud rate
void
void Open()
Turn on the device
None
void
bool IsOpen()
Is the device turned on
None
Return whether to open open: true close: false
void Close()
turn off the device
无
void
void SendData(byte[] data, out byte[] returnData, bool isWaitReturn, int waitTime , int repetition)
send data
data: the data to be sent
returnData: the data returned by the sensor
isWaitReturn: Whether the sensor needs to return data
waitTime: wait for the sensor to return data time, unit ms, default 100ms
repetition: the number of times to repeat sending
void
void SendProtocolData(byte[] data)
send protocol data
data: the data to be sent
void
void SendProtocolData(byte[] data, int waitTime)
Send data with protocol, and specify the waiting time