This explanations are for the device search in case device like PC. The PC attached device’s com port number assignment is not fixed. If connecting MCU and UART in Embedded System, it doesn’t need automatic search.
In case of device detecting COM port from host, there is case that user choose COM port to communicate in application program. This means bad products design regardless of user’s convenience. It has to be designed that the program search COM port connected with device automatically. This function can not be solved only by software. It has to set the function to search automatically in the device.
COM port searching method
In LXSDF T2A packet, PCD “Com port search information” which is PCD[31] and “LXDeviceID” which is PCD[30] is used to find proper COM port of the product for information to use device search.
You can find the device’s COM port easily if practicing the procures as the following explanation cycling all the Com port from host in order. Open one COM port temporarily and process the data received like the following table.
Flow Chart | Steps. | Description |
![]() |
Step 1. | If 254 is detected next to 255 : It’s possible device to communicate. Goto step 2.
If 254 is not detected next to 255 : it’s not LXSDF T2A packet. Start again opening another COM port. |
Step 2. | If PC (PACKET COUNT) value becomes number 31, it is sure of the device transmitting data to LXSDF T2A Format. However, it could transmit the same format data like LXSDF T2A in some products coincidentally. For occupying safely, if the PCD[31] is 109, it is sure that is LXSDF T2A packet. | |
Step 3. | If device is communicated by LXSDF T2A format, the next phase is to search device to communicate. At this time, check PCD[30] which is product’s LXDeviceID to communicate. |
Code Example : COM port automatic search. C#
Automatic search method to find the device. The method is the same regardless of language whether it is C# or C++.
int bytestoread = sp.BytesToRead; // occupied byte number in Com port buffer. Sp is serial port object. // OUTPUT 1. Whether it is our device or Not? Our device must have the data in COM port.. if (bytestoread == 0) { return; } // If there is no data which can be read in COM port, this is not LXSDF T2 format. LXSDF T2 transmits the data every time. /// If there is some data to read in COM port, it reads all the data. byte[] rbuf = new byte[bytestoread]; // created the memory size dynamically. bool find_sync = false; sp.Read(rbuf, 0, bytestoread); // received in rbuf tentatively.. // OUTPUT 2. Check sync . for (int i = 0; i < bytestoread-1; i++) // { if (rbuf[i] == 255 && rbuf[i + 1] == 254) // Found the sync spot. { find_sync = true; break; // break the loop } } if (find_sync == false) return; // If there is no data in order of 255, 245, this is not LXSDF T2. ///OUTPUT 3. Check the packet cyclic data in case of detecting some sync. Must receive over certain time data continuously to check it. byte[] cbuf = new byte[4096]; int bytetoreadlimit =0; int readbytenum = 0; int sum_readbytenum = 0; bool while_continue = true; byte Packet_Count =0; byte PacketCyclicData = 0; bool find_109 = false; byte find_ComDeviceID = 0; // ComDeviceID allots more than value 1. byte find_NumChannel = 0; byte find_NumSample = 0; byte find_firmversion = 0; while (while_continue) { if(sp.BytesToRead > 4096) bytetoreadlimit = 4096; else bytetoreadlimit = sp.BytesToRead; readbytenum = sp.Read(cbuf, 0, bytetoreadlimit); // read the data and figure the byte cumulative sum. sum_readbytenum += readbytenum; for (int i = 0; i < readbytenum-3; i++) { if (cbuf[i] == 255 && cbuf[i + 1] == 254) // detected sync spot. { Packet_Count = cbuf[i + 4]; // occupied packet count value. PacketCyclicData = cbuf[i + 6]; // occupied packet cyclic data. if (Packet_Count == 31 && PacketCyclicData == 109)// If packet count is 31 and packet cyclic data is 109, it is surely LXSDF T2 Type. find_109 = true; else if(Packet_Count == 30) // This spot is for Product ID. find_ComDeviceID = PacketCyclicData; else if (Packet_Count == 29) // This spot is for firmware version number. It is necessary if updating firmware by UART. find_firmversion = PacketCyclicData; else if (Packet_Count == 28) // Channel number transmitted into stream data. find_NumChannel = PacketCyclicData; else if (Packet_Count == 27) find_NumSample = PacketCyclicData; if (find_109 && find_NumSample > 0) // This means loof break because find_NumSample is in packet count 27 and find_109 is in packet count 31. If both value were found , Medium value could be found. { while_continue = false; break; } } } /// Designate the maximum value to review how many data can be received in COM port. If this value is too big, it takes very long time to search the device. So it’s good to set the small value. /// To search the device by LXSDF T2 type , The minimum needed data capacity must be at least 32 packets. In other words, 68bytes ( byte capacity of 1 packet) x 32 = 2176 bytes. It’s possible to exam device search information because it has 3000 bytes enough to be 32 packets. /// Formula : byte capacity of 1 packet can find the answer as 8 bytes + 64 bytes . /// 8 bytes : 1 packet is 8 bytes from Tx Index 0 to 6 /// 64 bytes : Stream area is channel number * 2(bytes) * sample number, though it has different value by each product . Because the maximum channel number allotted from LXSDF T2 is 8 and sample number is within 4, the maximum is 64 bytes. /// x 32 : must receive 32 packets to communicate packet count 0 to 31. if (sum_readbytenum > 3000) // Forcing Loof break condition. { while_continue = false; break; } } // while