Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send 8-bit data over serial port; values over 3F are being set to 3F

Status
Not open for further replies.

cdck

Programmer
Nov 25, 2003
281
US
As described in the subject, I'm attempting to send 8-bit data to an external component using the serial port. However, it appears that the top two bits are being forced to 0; any value I attempt to send above 3F is 3F at the other end. Can anyone see what's wrong based on the code below, which is the entirety of the code I'm using to do this (no other setup or call outs in the application)?
Code:
Dim strBackLevel As String = Me.cbPhase.Text
        Dim strBackString As String = ""

        Dim BS As New IO.Ports.SerialPort
        BS.PortName = Me.txtBackCOM.Text 'sets the COM port based on the selection on the main form
        BS.BaudRate = 9600
        BS.StopBits = IO.Ports.StopBits.One
        BS.DataBits = 8
        BS.Parity = IO.Ports.Parity.None

        If BS.IsOpen = False Then
            BS.Open()
        End If
                Select Case strBackLevel 'sets the hex values based on the combobox selection on main form
            Case "Day Set"
                strBackString = Chr(&H7) & Chr(&HFF) & Chr(&H42) & Chr(&H52) & Chr(&H54) _
                & Chr(&H1) & Chr(&H10) & Chr(&H82) & Chr(&H0)
                BS.Write(strBackString)
            Case "Dusk Set"
                strBackString = Chr(&H7) & Chr(&HFF) & Chr(&H42) & Chr(&H52) & Chr(&H54) _
                & Chr(&H1) & Chr(&H10) & Chr(&HDF) & Chr(&H20)
                BS.Write(strBackString)
            Case "Night Set"
                strBackString = Chr(&H7) & Chr(&HFF) & Chr(&H42) & Chr(&H52) & Chr(&H54) _
                & Chr(&H1) & Chr(&H10) & Chr(&H60) & Chr(&H9F)
                BS.Write(strBackString)

        End Select
        BS.Close()

When any of the three strings above are sent, all values read correctly on the other end with the exception of the values above 3F, all of which read 3F.

I'm working in Visual Studio 2008 with VB.net

Cheryl dc Kern
 
Found the answer, and it was hard to find. Here's a link to the article:


Apparently sending Hex data is really not done the way it used to be, for whatever reason. UTF8 encoding on the serial port doesn't work. what it came down to was using System.Text.Encoding.GetEncoding(1252)

My final code looks like:
Code:
Public Sub SetBacklight(ByVal strPhase As String)
        Dim strBackLevel As String = strPhase
        Dim strBackString As String = ""

        Dim BS As New IO.Ports.SerialPort
        BS.PortName = Me.txtBackCOM.Text 'sets the COM port based on the setting on the main form
        BS.BaudRate = 9600
        BS.StopBits = IO.Ports.StopBits.One
        BS.DataBits = 8
        BS.Parity = IO.Ports.Parity.None
        BS.Encoding = System.Text.Encoding.GetEncoding(1252)

        If BS.IsOpen = False Then
            BS.Open()
        End If
        Select Case strBackLevel 'sets the hex values based on the phase sent to the function
            Case "D"
                strBackString = Chr(&H7) & Chr(&HFF) & Chr(&H42) & Chr(&H52) & Chr(&H54) _
                & Chr(&H1) & Chr(&H10) & Chr(&HFF) & Chr(&H0)
                BS.Write(strBackString)
            Case "U"
                strBackString = Chr(&H7) & Chr(&HFF) & Chr(&H42) & Chr(&H52) & Chr(&H54) _
                & Chr(&H1) & Chr(&H10) & Chr(&HDF) & Chr(&H20)
                BS.Write(strBackString)
            Case "N"
                strBackString = Chr(&H7) & Chr(&HFF) & Chr(&H42) & Chr(&H52) & Chr(&H54) _
                & Chr(&H1) & Chr(&H10) & Chr(&H60) & Chr(&H9F)
                BS.Write(strBackString)
        End Select
        BS.Close()
        System.Threading.Thread.Sleep(500)

    End Sub

Cheryl dc Kern
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top