VB6 and VB.NET Serial (COM1) data error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: VB6 and VB.NET Serial (COM1) data error

  1. #1
    Join Date
    Feb 2011
    Posts
    9

    Unhappy VB6 and VB.NET Serial (COM1) data error

    Hello,
    I'm new to .NET. I have an application that is running in VB6. It sends text through COM1 port to another device.

    When I converted the VB6 code to .NET the string it sends out is different! I have tried so many things and don't know what to do. I used a serial data analyzer and found the two strings are different even the the source code is the same.

    VB6 source code:
    MSComm1.CommPort = 1
    MSComm1.Settings = "9600,O,8,1"
    MSComm1.PortOpen = True
    MSComm1.Output = Chr(&H2B) & _
    Chr(&H4) & _
    Chr(&H3) & _
    Chr(&HE8) & _
    Chr(&H0) & _
    Chr(&H2) & _
    Chr(&HF6) & _
    Chr(&H71)
    MSComm1.PortOpen = False

    The analyzer gets the following string in hex: 2B 04 03 E8 00 02 F6 71

    VB.NET source code:
    Dim Port As SerialPort = New SerialPort("COM1", 9600, Parity.Odd, 8, StopBits.One)
    Port.Open()
    Port.Write(System.Convert.ToChar(&H2B) & _
    System.Convert.ToChar(&H4) & _
    System.Convert.ToChar(&H3) & _
    System.Convert.ToChar(&HE8) & _
    System.Convert.ToChar(&H0) & _
    System.Convert.ToChar(&H2) & _
    System.Convert.ToChar(&HF6) & _
    System.Convert.ToChar(&H71))
    Port.Close()

    The analyzer gets the following string in hex: 2B 04 03 0F 00 02 3F 71

    Why is there a difference in the string?
    Please help me correct it...
    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: VB6 and VB.NET Serial (COM1) data error

    Why not just create HEX?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2011
    Posts
    9

    Re: VB6 and VB.NET Serial (COM1) data error

    What do you mean by create hex?
    is it like this?

    Port.Write(System.Convert.ToChar(Hex(42)) & _
    System.Convert.ToChar(Hex(4)) & _
    System.Convert.ToChar(Hex(3)) & _
    System.Convert.ToChar(Hex(232)) & _
    System.Convert.ToChar(Hex(0)) & _
    System.Convert.ToChar(Hex(2)) & _
    System.Convert.ToChar(Hex(246)) & _
    System.Convert.ToChar(Hex(113)))

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: VB6 and VB.NET Serial (COM1) data error

    I would create a string of characters, then convert THAT to Hex
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Feb 2011
    Posts
    9

    Re: VB6 and VB.NET Serial (COM1) data error

    I don't think I can do that.
    I think there is a difference in the new .NET control itself

  6. #6
    Join Date
    Feb 2011
    Posts
    9

    Angry Re: VB6 and VB.NET Serial (COM1) data error

    I found something interesting.

    when I convert a number using System.Convert.ToChar(X) and sends it through serial the X can only be hex: 0 to hex:3F if I want to get the correct value from the other side. Any value greater than hex:3F will not be hex:3F.

    for example if I send
    hex:64 you will get hex:3F
    send hex:E8 you will get hex:3F

    It seems like it max out with a 6 bit register value,
    Please help me correct this mess ...

  7. #7
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: VB6 and VB.NET Serial (COM1) data error

    What data type are you converting it to? Long?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  8. #8
    Join Date
    Feb 2011
    Posts
    9

    Re: VB6 and VB.NET Serial (COM1) data error

    it needs be all characters

  9. #9
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: VB6 and VB.NET Serial (COM1) data error

    What TYPE are you casting a Hex number to? FF is only 255.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  10. #10
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: VB6 and VB.NET Serial (COM1) data error

    Hmm... I have did a bit of serial in both. I found I needed to use a bytearray to send in several cases. If you send the binary values you should get the results that would would have gotten in VB6
    Always use [code][/code] tags when posting code.

  11. #11
    Join Date
    Feb 2011
    Posts
    9

    Re: VB6 and VB.NET Serial (COM1) data error

    yes you are right ToChar() works on 8 bit values and I need to convert values grater than FF into characters.

  12. #12
    Join Date
    Feb 2011
    Posts
    9

    Re: VB6 and VB.NET Serial (COM1) data error

    I did this:

    Dim Port As SerialPort = New SerialPort("COM1", 9600, Parity.Odd, 8, StopBits.One)
    Port.Open()
    Port.Write(System.Convert.ToByte(&H2B) & _
    System.Convert.ToByte(&H4) & _
    System.Convert.ToByte(&H3) & _
    System.Convert.ToByte(&HE8) & _
    System.Convert.ToByte(&H0) & _
    System.Convert.ToByte(&H2) & _
    System.Convert.ToByte(&HF6) & _
    System.Convert.ToByte(&H71))
    Port.Close()

    but did not get a responce from the device. The analyzer read that string to be
    34 34 30 32 33 32 30 32 32 34 36 31 31 33

  13. #13
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,949

    Re: VB6 and VB.NET Serial (COM1) data error

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  14. #14
    Join Date
    Feb 2011
    Posts
    9

    Talking Re: VB6 and VB.NET Serial (COM1) data error

    I found the answer and got it working ....

    From: http://msdn.microsoft.com/en-us/library/y2sxhat8.aspx

    By default, SerialPort uses ASCIIEncoding to encode the characters. ASCIIEncoding encodes all characters greater then 127 as (char)63 or '?'. To support additional characters in that range, set Encoding to UTF8Encoding, UTF32Encoding, or UnicodeEncoding.

  15. #15
    DataMiser is offline Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,813

    Re: VB6 and VB.NET Serial (COM1) data error

    TCP Sockets do the same thing.
    If you read the data from a file using Binaryreader and send it to the port as binary no encoding takes place.

    I struggled with this for a while before I found the binary method. I have not tryed the convert to byte method, Instead I had used read bytes and sent the bytes to the port. I was able to get an exact file copy through the port using that method whereas in VB6 there was no need to read the file as binary.
    Always use [code][/code] tags when posting code.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center