[VB.NET Compact] SerialPort.write problem

Proposed Answer [VB.NET Compact] SerialPort.write problem

  • 08 Februari 2012 14:12
     
     

    Hello everybody,

    I'm developing/migrating an application from VB6 to a Windows CE 5 Pro device with visual studio 2008 and i'm trying to solve this serial problem for about 4 days without any luck.

    Here we go:

    I need to send a specific byte array via the serial port to get a response from a custom device.

    The byte array is this:  dim bytearray(6) as byte = {83, 49, 3, 64, 212, 13} that in hex is [53,31,03,40,D4,0D]

    I send it using the standard serialport: serialport1.write(bytearray,0,6).

    Unfortunately, using a serial port monitor, i checked the transimmision and find that it's sending this: 53 FF 31 FF 03 40 FF 3F 0D.

    Tryed multipple times and also discovered that the FF byte aren't fixed and shows up at random apparently.

    After a bit of search i managed to make it working on a normal pc changing the codepage:

    serialport.Encoding = System.Text.Encoding.GetEncoding(28591)

    Unfortunately that encoding isn't supported by the compact framework, I'have tryed all the encodings but no one is working.

    Someone have a solution or a suggestion? I think i'm a dead end since i have no idea on how to solve this...

    If this can help i'm using windows xp sp3 in italian with visual studio 2008 also in italian for the developing and the windows ce device is in english.

    Thanks in advance to who will help me.

Semua Balasan

  • 08 Februari 2012 18:45
     
     

    Hi,

    Encoding is not used when sending binary data using a byte array. 

    You must, however, set serialport.DataBits = 8 for binary data.  For example, I wrote this small demo.  It sends data correctly.

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            With SerialPort1
                .PortName = "Com1"  'use whatever name you need for your application
                .RtsEnable = True
                .DataBits = 8             'this is required for binary data
                .BaudRate = 9600      'use whatever speed is required
                .Parity = IO.Ports.Parity.None    'this is required for binary data
                .Open()
            End With
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim bytearray() As Byte = {83, 49, 3, 64, 212, 13}   'or {&H53, &H31, &H03, &H40, &HD4, &H0D}
            SerialPort1.Write(bytearray, 0, bytearray.Length)
        End Sub
    End Class

    Dick


    Dick Grier. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net.

  • 08 Februari 2012 18:47
     
     Saran Jawaban

    If you still have a problem with the code that I showed, then there may be a hardware fault.  The code does work correctly.

    Dick


    Dick Grier. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net.

    • Disarankan sebagai Jawaban oleh Dick GrierMVP 09 Februari 2012 17:20
    •  
  • 09 Februari 2012 10:24
     
     

    Hi,

    Thanks for your reply. I was already setting up the port correctly but the problem was still there.

    Infact, as you said, there was an hardware problem. I was testing all using a 5 meter cable (16 feet) but loks like my device (Bolymin BEGA220A to be specific) doesn't like that infact since i directly attach it to the serial port tester all the FF just vanished (no cable issue tryed a bunch of that). To attach a longer cable i have to force to put the connector of the serial on ground (plus the ground wire of course) or FF go wild.

    Thank you again.

  • 09 Februari 2012 17:23
     
     

    RS-232 can be tricky.  Distances greater than 15 feet usually are possible.  However, some modern adapters use very lower max/min voltage ranges (still within specification) that are somewhat more prone to noise.  So, gounding can be a serious issue. 

    Dick


    Dick Grier. Author of Visual Basic Programmer's Guide to Serial Communications 4. See www.hardandsoftware.net.