trying to send a Hex(00) to a serial device

KKW

Member
Joined
Nov 21, 2009
Messages
10
Programming Experience
10+
In VB6, I use CHR$(&H00) to create a Null Character.
In VB.Net VS2008 CHR(&H00) creats a Null.
I'm trying to send a Hex(00) to a serial device, but the VB.Net function only sends a null, not a null character.
How can I accomplish this with VB.Net?
 
Thanks for taking a look at this. Consider this:

If I use the Shift-F9 function in VB6 to see the resulting value of myString = Chr$(&H0), it will show a small square.
If I use the Shift-F9 function in .Net to see the resulting value of myString = Chr(&H0), it will show the value = 'Nothing'

My ultimate goal is to send Ascii characters that represent Hex values.
A typical Modbus command might look like this: 03 06 02 F1 00 05 18 60
Each pair of numbers are used with the CHR command to create a character that is appended to a string that will be the data output by/for a comm port.
The problem is in .Net 00 does not translate into a character but rather into a Null.

Incidently,
If I send this actual command from a VB6 program, the Modbus device responds correctly and immediately.
If I send this actual command from a .Net program, the Modbus device never responds.
 
Last edited:
I think you'll find that what you think is happening is not actually what's happening. The fact that the VB.NET IDE shows Nothing isn't really relevant. That's just how VS.NET represents a null character. Char is a value type so it's not actually possible for a Char variable to not have a value. If you don't assign a value then it is a null character. Try this code and check the value of each variable:
VB.NET:
Dim c1 As Char
Dim c2 As Char = Nothing
Dim c3 As Char = ControlChars.NullChar
Dim c4 As Char = Chr(0)
Dim c5 As Char = Convert.ToChar(0)
I think you'll find that the issue is how strings are handled. Show us all the relevant code.
 
Sending Modbus Commands via Serial Port in VB.Net

Since the VB6 version works, and the .Net version does not, I didn't post the .Net version. However, the .Net version was simply cut-and-paste from the functional VB6 version, then modified.

If you need the calculated CRC value I can post that later!

Again,
Thanks!

Here's the VB6 Original code:

VB.NET:
Dim a As Integer
Dim b As String
Dim c As String
Dim d As Integer
Dim e As String
Dim f As String
Dim g As String
Dim h As String

    a = 20
    b = CStr(Hex(a))
    d = 64

    c = Chr$(a) & _
    Chr$(&H3) & _
    Chr$(&H20) & _
    Chr$(&H18) & _
    Chr$(&H0) & _
    Chr$(d)

'CRC Calc Code
    e = "[" & b & "][03][20][18][00][" & Format(Hex(d), "00") & "]"
    f = Module3.CRC16(e)

    g = Chr$("&H" & (Right$(f, 2))) & Trim$(Chr$("&H" & (Left$(f, 2))))
'End CRC Calc code
    
    h = c & Trim$(g)

    MSComm1.Output = h
 
VB.NET and VB6 are different languages. Never assume that something that works in one will work in the other. VB.NET syntax is based on VB6 syntax, but that doesn't make them the same language, any more than C# or Java are the same as C/C++ because their syntax is based on that. There are numerous differences between VB.NET and VB6 on the surface and even more under the hood. In VB.NET you should be using a SerialPort object for a start, so your VB.NET code should be rather different.
 
Back
Top