Click here to Skip to main content
15,039,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Friends,
I'm writing you to try to solve a problem,I'm sending some hex value using a serial port to an external device.
It answer to my status request , but instead to show the value of 0x01 in the middle of the data packet it show 0x00.
I'm using a simple vb.net winform on VS2012.
Here the code i'm using:

Imports System
Imports System.IO
Imports System.IO.Ports
Imports System.Text
Imports System.Threading


Public Class Form1

    
    Dim value As Byte
    
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    SerialPort1.Encoding = System.Text.Encoding.GetEncoding(1252)
    SerialPort1.Open()

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        SerialPort1.Close()
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim array(6) As Byte
        array(0) = &H3
        array(1) = &H1
        array(2) = &H1
        array(3) = &HA4
        array(4) = &HA5
        array(5) = &HB2
        SerialPort1.Write(array, 0, array.Length)
        Label1.Text = ""
      
        For i = 0 To 10
            value = SerialPort1.ReadByte()
            Label1.Text += value.ToString("x2") + " "
        Next
    End Sub



I'm receiving that :
03 01 01 A4 A5 B2 00 00 03 00 FC

But as from the device datasheets I must receive:
03 01 01 A4 A5 B2 01 00 03 00 FC


Using a port sniffer I see that the device answer correctly with the value of 0x01.
Somebodys has an idea why from my application I read the byte 6 as 0x00 and not 0x01?
The problem seems to be only with the value 0x00 and 0x01 ....others value are readed correctly(I suppose).

Thanks in advance

Maurizio

What I have tried:

Using a serial port com sniffer the bytes received are correctly.
Posted
Updated 10-Nov-20 20:17pm
Comments
Richard MacCutchan 10-Nov-20 4:02am
   
It is impossible to guess what may be going wrong. You need to use your debugger to capture more information.
Richard Deeming 10-Nov-20 4:48am
   
REPOST
You have already posted this in the VB forum:
Problem receiving hex value on serialport - Visual Basic Discussion Boards[^]
Richard MacCutchan 10-Nov-20 4:55am
   
Well spotted. I knew I had seen this somewhere before, but OP's home page message count is zero.
Member 10201357 10-Nov-20 5:02am
   
Sorry, I supposed to post it in a wrong section.

Best guess - and without your entire system, that's all it can be - it's pre-buffered data.
Serial ports buffer up received bytes, so if your remote device sends a message that you don't process it will remain in the SerialPort class buffer until you do read it.

Then, when you send your new message, the response you read was the earlier message, not the latest.
Try clearing the buffer before you send your data to the device, and see if that helps.
   
Comments
Member 10201357 10-Nov-20 4:19am
   
Thanks for the answer,but the buffer is completly clear, at the moment I start the application and I send only one time the device status request...no more commands.

Today afternoon I will use the debugger as suggested from Richard MacCutchan to capture more informations.
Using a VS debugger as suggested I have found the problem,from my code the serial port sent 7 byte (latest with the value of 0x00) and not 6 bytes as declared.
The right code is:
Dim array(5) As Byte
array(0) = &H3 
array(1) = &H1 
array(2) = &H1 
array(3) = &HA4 
array(4) = &HA5 
array(5) = &HB2 
SerialPort1.Write(array, 0, array.Length)


Is quite strange...from 0 to 5 there are 6 bytes...and not 5 as I must declare...
.
The device used a single wire for the communication in half duplex mode..the master sent 0×00 and on the same time the slave answer with the value of 0x01...result 0x00 then the rest of the status packet....
....

Thank you for the help

Regards
Maurizio
   
Comments
Richard Deeming 11-Nov-20 4:25am
   
Arrays in VB.NET are declared using the upper bound of the array, not the number of elements in the array. This was done for backwards-compatibility with VB6 code, even though it makes less sense when you can't change the lower bound of the array.

array(5) is an array of 6 elements, with indices from 0 to 5.
Using a VS debugger as suggested I have found the problem,from my code the serial port sent 7 byte (latest with the value of 0x00) and not 6 bytes as declared.
The right code is:

Dim array(5) As Byte
array(0) = &H3 
array(1) = &H1 
array(2) = &H1 
array(3) = &HA4 
array(4) = &HA5 
array(5) = &HB2 
SerialPort1.Write(array, 0, array.Length)


Is quite strange...from 0 to 5 there are 6 bytes...and not 5 as I must declare...
.
The device used a single wire for the communication in half duplex mode..the master sent 0×00 and on the same time the slave answer with the value of 0x01...result 0x00 then the rest of the status packet....
....

Thank you for the help

Regards
Maurizio
   
Comments
Richard Deeming 11-Nov-20 4:23am
   
You've managed to post the same solution twice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900