Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

MsComm help

  • 13-01-2004 7:35pm
    #1
    Registered Users, Registered Users 2 Posts: 1,368 ✭✭✭


    Im interfacing with a serial device. Im using the MSComm component. Ive wrote a test app.
    I'm new to VB so the code is a bit crude:


    Private Sub Command1_Click()

    MSComm1.CommPort = 1

    ' 9600 baud, no parity, 8 data, and 1 stop bit.
    MSComm1.Settings = "9600,N,8,1"

    ' Tell the control to read entire buffer when Input
    ' is used.
    MSComm1.InputLen = 0

    ' Open the port.
    MSComm1.PortOpen = True

    'the command to set the baud rate is the following hex value:
    '01 0A 00 00 00 00 FF 06 F2 0D (hex)
    'I sent it out as follows, just to ensure it wa the proper hex reprentation:

    hexout = hexout + Hex(0)
    hexout = hexout + Hex(1)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(10)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(15)
    hexout = hexout + Hex(15)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(6)
    hexout = hexout + Hex(15)
    hexout = hexout + Hex(2)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(13)
    MSComm1.Output = hexout

    'The next code is crude code to see if there's any responce from the serial device
    Select Case MSComm1.CommEvent
    Case comEvReceive
    Text3.Text = "Received"
    Case comEventBreak
    Text3.Text = "' A Break was received."
    Case comEventFrame
    Text3.Text = "' Framing Error"
    Case comEventOverrun
    Text3.Text = "' Data Lost."
    Case comEventRxOver
    Text3.Text = "' Receive buffer overflow."
    Case comEventRxParity
    Text3.Text = "' Parity Error."
    Case comEventTxFull
    Text3.Text = "' Transmit buffer full."
    Case comEventDCB
    Text3.Text = "' Unexpected error retrieving DCB]"

    Case comEvCD
    Text3.Text = "'' Change in the CD line."
    Case comEvCTS
    Text3.Text = "'' Change in the CTS line."
    Case comEvDSR
    Text3.Text = "' Change in the DSR line."
    Case comEvRing
    Text3.Text = "' Change in the Ring Indicator."
    Case comEvReceive
    Text3.Text = "' Received RThreshold # of chars."
    Case comEvSend
    Text3.Text = "' There are SThreshold number of characters in the transmit"
    Case comEvEOF
    Text3.Text = "' EOF ERROR"

    End Select

    End Sub






    The problem in that I'm getting no responce from the serial device at all.

    Hope someone can help,

    Thanks :)
    Private Sub Command1_Click()


Comments

  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Probably no help at all but try adding a case else to the end of your switch e.g.
    Case Else
        MsgBox MSComm1.CommEvent
        '// or use debug.print MSComm1.CommEvent
    

    This way if the serial device is returning a value uncatered for your existing code you'll get a message box giving you the return value. If this doesn't happen then you know for sure that nothing is being returned.


  • Registered Users, Registered Users 2 Posts: 79 ✭✭tendofan


    -
    Sorry I'm editing this 'cos I forgot to get rid of the Hex
    function
    -

    One of the problems is that you're not actually sending what you think you're sending.

    The serial device expects to see the following string of bytes:

    01 0A 00 etc.

    which is

    But when you do this

    hexout = hexout + Hex(0)
    hexout = hexout + Hex(1)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(10)
    hexout = hexout + Hex(0)
    hexout = hexout + Hex(0)

    and send it, what you're sending is a string that looks like this

    "0001000A0000"

    which is totally wrong because the first three bytes of that character is actually 30 hex, or the character '0', not the byte value 01.

    This is because the hex function takes the number and turns it into a string. Each number, 0, 1, 0, 10 etc., gets converted into a string of characters.

    Say you want to send the following bytes:

    01 FF 0A

    you'd do this:

    hexout = hexout + chr(&H01)
    hexout = hexout + chr(&HFF)
    hexout = hexout + chr(&H0A)

    (the &H is so you can put hex numbers in directly, in case you didn't know.)

    If you look at hexout in the Watch Window it will look very strange, but that's okay.

    As for anything else, are you sure that you're setting up the baud rate, parity etc. correctly?

    Also, from what I remember, the CommEvent property will only report on the most recent comms event. There may not have been an event when you test the value of that property. You should put the select statement into the OnComm event, because this event occurs whenever something happens to the communication, and then you can test CommEvent to see what's happened.

    Hope this helps.

    Tendofan


  • Registered Users, Registered Users 2 Posts: 1,368 ✭✭✭king_of_inismac


    Thanks guys,

    Will try your suggestions tonight,

    The help is much appreciated :)


  • Registered Users, Registered Users 2 Posts: 1,368 ✭✭✭king_of_inismac


    ok Problem:

    When I enter

    hexout = hexout +chr(&H01)

    vb automatically changes this to

    hexout = hexout + chr(&H1)

    so the '0' never gets sent....


  • Registered Users, Registered Users 2 Posts: 79 ✭✭tendofan


    First off, &H1 is exactly the same as &H01, it's just a number, the exact same as 5 is the same as 05. I think you might be confused between a character and the byte value it represents.

    I think I've misunderstood what you're doing and may have given you the wrong advice. I use MSComm from C++ (or more often than not write my own Serial handling stuff). From the documentation on MSComm in VB:

    "The Output property can transmit text data or binary data. To send text data using the Output property, you must specify a Variant that contains a string. To send binary data, you must pass a Variant which contains a byte array to the Output property."

    From what you wrote, I think you want to send a stream of bytes. If for example you wanted to send the following bytes &H55, &HAA, &HBB, &H04 then you'd do the following:

    Dim hexout as string

    hexout = chr(&H55)
    hexout = hexout + chr(&HAA)
    hexout = hexout + chr(&HBB)
    hexout = hexout + chr(&H4)

    this would give you a string with four characters that looks like this in the debug window:

    Uª»

    Okay, it looks like junk, but when you send that down to the device the device sees 4 bytes with values 55, AA, BB and 4. If you don't believe me, download Portmon from sysinternals.com. It lets you see what's happening on the com port. Just pick the "Show Hex" option from the Options menu.

    Technically, if you're sending bytes, you really should do this:

    Option base 0

    ...

    Dim byteArray(3) as Byte

    byteArray(0) = &H55
    byteArray(1) = &HAA
    byteArray(2) = &HBB
    byteArray(4) = &h4

    MSComm1.Output = byteArray

    the other way works on my machine, but may not depending on how characters are internally represented by VB.

    If you're sending byte values, but the device expects to see them as ASCII, (sometimes called Hex-ASCII) you can just set up a string like so:

    Dim hexout as string

    hexout = "55AABB04" and send it, but from what you said first, your device doesn't expect to see things like that.

    I hope this helps, and I can't recommend Portmon highly enough for letting you see what is going out and coming in on the COM port.

    Tendofan


  • Advertisement
Advertisement