RS232 Dilema

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JohnHappy
    New Member
    • Jan 2010
    • 2

    RS232 Dilema

    Hello,

    The church I go to has a data projector (Toshiba TDP-S20) that can be controlled via a serial connection. When it was installed, we connected the projector to the nearest PC (runnings Windows XP) and I found a RS232 software program online that allowed me to control it successfully. To power up the projector, I found I could send the following string of characters:

    BlackSmileyFace + PON + BlackHeartSuit (without the plus signs and spaces)

    I was able to put the symbols into the transmit textbox by right-clicking within the transmit textbox and choose the symbols from a pop-up ASCII table. I'd click on the Send button and everything was fine.

    A few days ago I decided to build a windows app in VB.NET that would allow us to control the projector using various buttons, each button controlling one of the functions like POWER ON, SHUTDOWN, INPUT1, INPUT2, etc, etc. In each case the string of characters needed started with the Black Smiley Face and ended with the Black Heart Suit symbol.

    BlackSmileyFace + PON + BlackHeartSuit
    BlackSmileyFace + IN2 + BlackHeartSuit

    I'm developing the program from my Home Office computer using Visual Studio 2008. The PC used to control the projector and the projector are at a different location.

    Simply put, I thought I had it all figured out when I could click on my Power On button and the correct string of characters would appear in my "Code Sent" textbox. I would see the BlackSmileyFace , the string of three function characters (PON, for example) and the BlackHeartSuit symbol. I used the following VB.Net code to get the correct characters to display in my CodeSent textbox on my development PC.

    Code:
    Dim BlackSmileyFace As Char
    Dim BlackHeart As Char
    BlackSmileyFace = ChrW("9787")
    BlackHeart = ChrW("9829")
    CodeSent.Text = BlackSmileyFace + "PON" + BlackHeart
    I thought all I had to do was grab what was in the CodeSent textbox and write it to the serial port, something like the following:

    Code:
    If moRS232.IsOpen Then
         Dim outgoing as string      
         outgoing = CodeSent.Text
         moRS232.WriteLine(outgoing)
    End If
    This did not work. I tried to send the Turn On command (PON) and the projector did not turn on.

    Next, I decided to open the RS232 terminal program I had successfully used to control the projector, enter the Power On command in that program's transmit textbox (using the ASCII table to enter the BlackSmileyFace and BlackHeartSuit symbols in the proper place) and copy that string of characters to my clipboard. I did so.

    I pasted that string of characters from my clipboard into my program's CodeSent textbox, clicked on my SEND button and the projector turned on.

    I pasted what was in my clipboard into NOTEPAD, copied that, pasted that into my programs CodeSent textbox, clicked on my SEND button and the projector turned on. I then changed the three function command characters (between the two symbols) in my open notepad file to IN1 - to change the projectors input to Input #1 - copied the revised notepad code, pasted it into my CodeSent textbox, clicked on the SEND button and the projector responded appropriately.

    So, for some reason, when I try to send the start and stop part of my command code to the projector using ChrW() and sending the command as a string, it doesn't work.

    SUMMARY:

    This does not work, even though it appears to be display correctly on my development PC:

    Code:
    BlackSmileyFace + PON + BlackHeartSuit
    I generated the BlackSmileyFace using ChrW("9787") and the BlackHeartSuite using ChrW("9829").

    I can grab what appears to be exactly the same command string from the working RS232 terminal program and it does work.

    I can grab what was copied into or edited within NOTEPAD (the symbols do not appears as the Black Smiley Face and the Black Heart Suit) and that will work in my program.

    I wish I could see the code behind what was displaying in NOTEPAD and the working, third-party, RS232 terminal program.

    I know that the serial port is opening and closing correctly within my program. I am using the SerialPort class.

    Given the above, what VB.NET code would you suggest I use to write the correct command to the projector?

    Thanks for your help.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Any time you try sending text and characters you are open to having the operating system 'interpret' them for you. Unicode for example is a text encoding that uses 2 bytes for every character.

    Serial on the other hand is all about the bytes. There should be a set of commands that came with the project that tell you what bytes are expected as the commands.
    Bytes 96, 17, 155 is the command to power down for conversation sake.
    So you want to send bytes, not characters.

    When you convert the byte 96 to character 96, then stick it in a text box, then take it back out of a text box, it may no longer be a single byte of 96 - because Windows was 'helpful' to you and turned it into a 2-byte unicode character.

    Comment

    • alexis4
      New Member
      • Dec 2009
      • 113

      #3
      If I understood correctly then I'd suggest this:

      First of all be 100% sure that you have the proper configuration (baudrate, stopbits etc).
      Then try to send your frame not from your custom application, but from an existing one (not from windows hyper terminal, try silverlight). If you send the "proper" string and everything is OK, then you don't send what you thing you send from your application and your code needs to be corrected. If the problem remains, then this is not the proper string, or it's the proper string but you need a start and stop byte that you don't know. Either way silverlight will show what is going wrong, because you can then set it up to receive from the projector software so you will see the incoming frame.

      Comment

      • JohnHappy
        New Member
        • Jan 2010
        • 2

        #4
        OK, I want to send bytes, not characters, to my serial device.

        The projector's users manual states:

        Communication method - RS-232C: 9600bps, No Parity, Data Length: 8 bits; Stop Bit Length: 1 bit

        Communication format: STX (02h) Command (3Byte) ETX (03h)
        Only 1 command valid per communication.
        Data format: For input commands, only ASCII-compliant all-uppercase alphanumeric characters supported.

        It shows the most popular commands, i.e. Power On = PON

        So... if I wanted to create my command code within the program and NOT grab it from a textbox, would my code look something like this:

        Code:
        Dim WithEvents moRS232 As New SerialPort()
        
        With moRS232
                    .PortName = "COM1"
                    .BaudRate = 9600
                    .DataBits = 8
                    .StopBits = StopBits.One
                    .Parity = Parity.None
                    .Handshake = Handshake.None
                    .ReadTimeout = 1000
                    .WriteTimeout = 1000
        End With
        
        moRS232.Open
        Dim msg As Byte() = New Byte(5) {}
        msg(0) = &H02  ' start (STX) bit
        msg(1) = &H50  ' for "P"
        msg(2) = &H4f   ' for "O"
        msg(3) = &H4e  ' for "N"
        msg(2) = &H03  ' stop (ETX) bit
        moRS232.Write(msg, 0, 5)

        Comment

        • alexis4
          New Member
          • Dec 2009
          • 113

          #5
          At line 20 you should change msg(2) to msg(4). The rest of the code looks OK.
          If it won't work, I'm telling you again to verify serial traffic. You do it in 5 minutes and it could save you hours of work. For ASCII-compliant characters hyper terminal is fine.

          Comment

          Working...