Dim X As Integer
comPort.Output = X 'sends 00000000 00000011
X = 3
Dim Y As Long
Y = 3
comPort.Output = Y 'sends 00000000 00000000 00000000 00000011
Using either of these sequences is NOT
the way to send the length of the file. The comPort.Output function accepts a String
or a Byte Array
as its output value. So one of the assignments that you show is not allowed.
In order to get a string value and to work out getting the converted number value into a string that is always the same length you could consider something like this:
Dim Y As Long
Dim S As String
Y = <some value of some sort like a file size>
S = Format(Y, "000000000000")
comPort.Output = S
Here I show how you intentionally convert the Y variable value to a string that has a length of 12. So you know you will send 12 characters out the comm port.
By the way, if you were to range check the value of Y to be less than the full range of the value stored in a Long you could use a shorter template string in the Format statement. For example if you made sure that 0 <= Y <= 255
then the template string could be "000" with length 3 and you would know you send 3 bytes.
There are ways to manipulate sending the actual binary value of the Long variable. It involves separating the 32-bits of the variable into four bytes and then sending each of those bytes as a string byte to the port. However I would recommend the method shown here here for a couple of reasons. First off it is easy be able to read the file length in transmitted data sequence if it has been formatted to human readable format. Secondly, you only send the value once so there is little or no performance issue if you send four bytes or twelve. And on the receiving end it is very easy to accumulate the first 12 bytes into a string and convert that back into a long value using the CLng() function.