Convert value into 4 bytes array


Recommended Posts


I want an array of 4 bytes with most significant first such as 128 will return {0,0,0,128}

I have tried from.urbLong and all others but the return array is not as I expected. is there another command to this conversion?


Link to comment
Share on other sites

Function E13_NodeRead1Sensor(N_Address, R_Chan)

   NodeResponsePacket.ClearHistory()                                   // Clear global variable                  

   Private Node_Address = From.urWord(N_Address)      // Convert node address in decimal number into 2 bytes array

   Private Read_Channel = From.uByte(R_Chan)          // Convert node channel in decimal number into 1 byte data




      device.COMDevice.Write(Chr(0x03))               // Send 3 bytes header to wireless node

      device.COMDevice.Write(ChrA(Node_Address))      // Send 2 bytes node address to wireless node

      device.COMDevice.Write(Chr(0x01))               // Send 1 command byte to wireless node

      device.COMDevice.Write(Chr(Read_Channel))       // Send 1 byte of channel number to wireless node

      Private String DataIn = Device.COMDevice.Read(0)// Read all the received bytes from Node

      ? showhidden(DataIn)

      // Parse the responsed data from Node and convert to ASCII value

      NodeResponsePacket[0] = AscA(Mid(DataIn,0,1))

      If (NodeResponsePacket[0] == 33)                // If the first responsed byte is 0x21

         Return 0                                     // return 0 to indicate fail response


         NodeResponsePacket[2] = To.urWord(AscA(Mid(DataIn,1,2)))

         NodeResponsePacket[3] = To.urWord(AscA(Mid(DataIn,3,2)))

         Return 1                                     // return 1 indicates data successfully received


   catch()                                            // If an error at the COM port or no response from Node

      return 0                                        // return 0

      ? strLastError



Top picture: 

Private String DataIn = Device.COMDevice.Read(5)// Read all the received bytes from Node

With the red line of code is change to Read(5), I was able to read the packet. The showhidden(Datain) is show below the command line.

Bottom picture: 

With the red line of code is Read(0), I could not parse the packet. The showhidden(Datain) did not appear on the Command/Alert panel. COM port showing all 5 bytes were received.




Link to comment
Share on other sites

That's because read(0) reads whatever has ALREADY been received from the device and is sitting in the comm buffer.  It is very unlikely that your device would have responded to your write() requests before you got to the read(0) and so there is no data in the buffer.  read(0), unlike the other read functions, returns immediately, either with the data in the buffer, or with an empty string.  It does not wait around for data to arrive.

Often if you are unsure of the length of data being received, but you don't want to put some arbitrary delay in, you can tell DAQFactory to read(1), then read(0) after a very short delay:

Private String dataIn =
datain +=

OK, so there is still an arbitrary delay(), but it can be really short.  It just needs to be long enough for the device to have sent all its data, and that you can calculate.  This is much more reliable, and faster than:

datain +=

or other alternatives.


Link to comment
Share on other sites


This topic is now archived and is closed to further replies.