Shop OBEX P1 Docs P2 Docs Learn Events
Quick question about VB coding — Parallax Forums

Quick question about VB coding

GalacticGalactic Posts: 6
edited 2005-10-29 04:23 in General Discussion
Hey guys,

I wrote a simple program in VB6 to send out signals to my basic stamp VIA serial port. The program works well, but I actually got it to work completly by messing around with it and there is one part I don't understand about it.
Heres my code:
Private Sub btnLEDoff_Click()
MSComm1.PortOpen = True
MSComm1.Output = Chr$(2)
MSComm1.PortOpen = False
End Sub

Private Sub btnLEDon_Click()
MSComm1.PortOpen = True
  MSComm1.Output = Chr$(1)
  MSComm1.PortOpen = False
End Sub

Private Sub Form_Load()
Dim data As Integer
  MSComm1.DTREnable = False
  MSComm1.Settings = "9600,n,8,1"  ' 9600 baud, No Parity, 8 databits
  MSComm1.CommPort = 1
  MSComm1.RThreshold = 1 
End Sub



This is just a simple program that turns an LED on and OFF - works fine - I just have one question.
What is Chr$(1) & Chr$(2) ? Is that some sort of representation of the integer 2? What is the purpose of "$"? Or is it something more?

Any ideas would be helpful - thanks - Jonathan

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-10-29 00:11
    Chr$ sends the ascii code in parenthesis -- if you send Chr$(65) you'll see "A"

    Please keep in mind that questions related specifically to VB programmin belong in the Sandbox, so I've moved this thread.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • SteelSteel Posts: 313
    edited 2005-10-29 00:16
    The "Char$" converts the number in the parenthesis (which is an integer) into it's ascii equivalent for comm transfer.

    This is so that when you want to send a string, such as Char$("I want to send this string to my BS2"), the Char$ command sends this over the line:

    49 20 77 61 6E 74 20 74 6F 20 73 65 6E 64 20 74 68 69 73 20 73 74 72 69 6E 67 20 74 6F 20 6D79 20 42 53 32 00

    ·
  • GalacticGalactic Posts: 6
    edited 2005-10-29 00:18
    Oh - Thanks so much
  • John R.John R. Posts: 1,376
    edited 2005-10-29 00:31
    Because your using the serial port on the PC side as an ASCII connection (as opposed to a binary connection), you are sending out strings or characters.

    If you tried sending out "1" or "2" you would actually be sending out the numeric values 49 and 50. (Assuming VB would do an automatic cast from the decimal value to a "string". I'd have to do some testing, I'm not sure if VB would cast these, or send out the numeric value.)


    The CHR$, as Jon pointed out, converts the ASCII code to a character. In this case [noparse][[/noparse]CHR$(1) and CHR$(2)] these are "special" non-printable charcters that are/were used as control codes in "antique" printers, ttys, etc. (See an ASCII chart, like the one in Appendix A of the Basic Stamp Manual.)

    In your application, the CHR$(1) and CHR$(2) will look like numeric values 1 and 2 from the stamp end.

    You could get around this by creating an array variable to hold your output on the VB side, but given the task at hand, the code you've got is probably a better choice.

    As a summary, this is a way of passing the "raw" numeric values 1 (binary 00000001) and 2 (binary 00000002) out the serial port.

    If you need more explanation, let us know.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    John R.

    8 + 8 = 10
  • GalacticGalactic Posts: 6
    edited 2005-10-29 04:23
    Thanks for clearing it all up guys!
Sign In or Register to comment.