Rading values from a Serial Port in VB2005

C

cmdolcet69

The sub below reads the value of position from my serial write
command. However i need to read only the (3) and (4) index of the byte
array and then need to convert it into a integer. When i get to the
step invalue = bitconverter.toint32(Newpacket,0) line i trigger an
error saying that the byte array is less than the read byte array.
How can i only grab position (3) and (4) and then return there values
in integer.

Public invalue as integer

Public Sub ProbeBitValues()
Dim Packet(7) As Byte
Dim NewPacket(2) As Byte

SerialPort2.Read(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.ToInt32(NewPacket, 0)
End Sub
 
A

Armin Zingler

cmdolcet69 said:
The sub below reads the value of position from my serial write
command. However i need to read only the (3) and (4) index of the
byte array and then need to convert it into a integer.

I told you several times that an Integer consists of 4 Bytes.

Integer = Int32 = 32 bits = 4 Bytes
Short = Int16 = 16 bits = 2 Bytes

Still.
When i get to
the step invalue = bitconverter.toint32(Newpacket,0) line i trigger
an error saying that the byte array is less than the read byte
array. How can i only grab position (3) and (4) and then return
there values in integer.

Public invalue as integer

Public Sub ProbeBitValues()
Dim Packet(7) As Byte


You know that Packet contains 8 Bytes?

Dim NewPacket(2) As Byte


You know that NewPacket contains 3 Bytes?

SerialPort2.Read(Packet, 0, 7)
NewPacket(0) = Packet(3)
NewPacket(1) = Packet(4)
indvalue = BitConverter.ToInt32(NewPacket, 0)
End Sub

Why do you put the two bytes into a new array? You can pass variable Packet
to BitConverter.ToInt32 and specify the index 3 as the start index. But
again, this will take bytes at position 3, 4, 5 and 6, not only 3 and 4.


Armin
 
C

cmdolcet69

I told you several times that an Integer consists of 4 Bytes.

Integer = Int32 = 32 bits = 4 Bytes
Short = Int16 = 16 bits = 2 Bytes

Still.




You know that Packet contains 8 Bytes?


You know that NewPacket contains 3 Bytes?


Why do you put the two bytes into a new array? You can pass variable Packet
to BitConverter.ToInt32 and specify the index 3 as the start index. But
again, this will take bytes at position 3, 4, 5 and 6, not only 3 and 4.

Armin

Is there any way i can only return byte position 3 and 4 only. I know
you have told me these several times, sorry
 
A

Armin Zingler

cmdolcet69 said:
Is there any way i can only return byte position 3 and 4 only. I
know you have told me these several times, sorry

Short/Int16 is 2 bytes. Conclusion: Use BitConverter.ToInt16


Armin
 
C

cmdolcet69

Short/Int16 is 2 bytes. Conclusion: Use BitConverter.ToInt16

Armin- Hide quoted text -

- Show quoted text -

Ok armin after apply the correct conversion above, thank by the way. I
have this sub

Public Sub ProbeBitValues()
SerialPort2.Read(Packet, 0, 7)
indvalue = BitConverter.ToInt16(Packet, 3)
Me.lblIndicatorValues.Text = indvalue
End Sub

I have the Serialport2 connect to a USB to serial converter cause i
only have (1) serial port on my PC. This shouldn;t matter but
anyways,
my issue comes that i should be getting values in the range of 3902 -
2036, however when i put my sub on a timer, there are time i return
wierd far out number such as 20111 and 30222. my indvalue variable is
a integer type.

Can you think of any way im getting those wierd number?
 
A

Armin Zingler

cmdolcet69 said:
Ok armin after apply the correct conversion above, thank by the way.
I have this sub

Public Sub ProbeBitValues()
SerialPort2.Read(Packet, 0, 7)
indvalue = BitConverter.ToInt16(Packet, 3)
Me.lblIndicatorValues.Text = indvalue
End Sub


You should enable Option Strict.

I have the Serialport2 connect to a USB to serial converter cause i
only have (1) serial port on my PC. This shouldn;t matter but
anyways,
my issue comes that i should be getting values in the range of 3902
- 2036, however when i put my sub on a timer, there are time i
return wierd far out number such as 20111 and 30222. my indvalue
variable is a integer type.

Can you think of any way im getting those wierd number?


Maybe the byte order is different. The lo byte is expected at index 3 and
the hi byte at index 4. Maybe your sender sends in reverse order. I don't
know if the values above (20111 and 30222) are real values you got or only
examples. If it is 30222, which is Hex 760E, this can be true. If you change
the bytes, it is 0E76, and this is 3702 which would be a valid value. In
this case you do have to extract the bytes first in reverse order, then use
BitConverter.ToInt16:

dim ShortBytes(1) as byte

shortbytes(0) = packet(4)
shortbytes(1) = packet(3)

indvalue = BitConverter.ToInt16(shortbytes, 0)



Armin
 
C

cmdolcet69

You should enable Option Strict.



Maybe the byte order is different. The lo byte is expected at index 3 and
the hi byte at index 4. Maybe your sender sends in reverse order. I don't
know if the values above (20111 and 30222) are real values you got or only
examples. If it is 30222, which is Hex 760E, this can be true. If you change
the bytes, it is 0E76, and this is 3702 which would be a valid value. In
this case you do have to extract the bytes first in reverse order, then use
BitConverter.ToInt16:

dim ShortBytes(1) as byte

shortbytes(0) = packet(4)
shortbytes(1) = packet(3)

indvalue = BitConverter.ToInt16(shortbytes, 0)

Armin- Hide quoted text -

- Show quoted text -

I see, I looked at the senario and noticed that when I put your logic
into the sub it wouldn;t return the wild crazy number anymore, but
this time it did do some unexpected stuff, now it will toggle between
the valid numbers (3002) and 0 which i think its because i have the
com1.write on a timer. I dont know, never of had to do such crazy
amounts of work for somthing to work.
 
A

Armin Zingler

cmdolcet69 said:
I see, I looked at the senario and noticed that when I put your
logic into the sub it wouldn;t return the wild crazy number anymore,
but this time it did do some unexpected stuff, now it will toggle
between the valid numbers (3002) and 0 which i think its because i
have the com1.write on a timer. I dont know, never of had to do such
crazy
amounts of work for somthing to work.


How do you verify that data is available from the serial port? You seem to
read max. 7 bytes, but how do you know that 7 bytes are available?


Armin
 
C

cmdolcet69

How do you verify that data is available from the serialport? You seem to
read max. 7 bytes, but how do you know that 7 bytes are available?

Armin- Hide quoted text -

- Show quoted text -

I actually don;t verify any data. All i do i read the com port each
time the timer is executed. Do you think that is why sometimes it will
read 0? and not a correct value, is because i rad the COM port too
early?
 
A

Armin Zingler

cmdolcet69 said:
I actually don;t verify any data. All i do i read the com port each
time the timer is executed. Do you think that is why sometimes it
will read 0? and not a correct value, is because i rad the COM port
too early?

Probably.


Armin
 
C

cmdolcet69

Probably.

Armin

So how would you control this. I can think of two ways. 1- have a
sleep on the main thread so that the timer is executed, it will sleep
for a sec then read the COM port. Or 2- slow down the timer that i
have the code placed in. Which one in your professional opinion would
you use?
 
A

Armin Zingler

cmdolcet69 said:
So how would you control this. I can think of two ways. 1- have a
sleep on the main thread so that the timer is executed, it will
sleep for a sec then read the COM port. Or 2- slow down the timer
that i have the code placed in. Which one in your professional
opinion would you use?

It depends on the transmission protocol. When does the port receive data?
Which class is SerialPort2? Is it System.IO.Ports.SerialPort (IIRC it is
not)? It has the DataReceived event.

Could you please describe in detail what you are trying to achieve?


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top