No announcement yet.

Sending & Receiving Via Com1

  • Filter
  • Time
  • Show
Clear All
new posts

    Sending & Receiving Via Com1

    I am having trouble figuring out how to receive serial data via COM1. I have searched theforum but have yet to find
    anything that will help me. For academic purposes I modified Comm.bas (PB/Dll sample program) so that a known string
    of bytes would be sent whenever the "Send Text" button is pressed. (code from modified comm.bas follows)
    >>>  CALLBACK FUNCTION Send_Callback() AS LONG
    >>>  COMM Send #hComm, CHR$(02, 04, 05, 06, 17, 30, 39, 41, 46)
    >>>  FUNCTION = %TRUE
    I compiled this code and moved it over to a different machine sitting less than 2 feet away.The two machines are connected
    together via a 9-pin null-modem cable (2-3, 3-2, 5-5). (The cable works fine when using HyperTerminal between the machines
    at 9600)

    The "receiving" machine is running the following:
    >>>  ' Create A "Listen" Thread To Monitor Input From Console
    >>>  Thread Create ReceiveData(hDlg) To hThread&
    >>>  Function ReceiveData(ByVal hWnd As Long) As Long
    >>>  Dim DataIn As String
    >>>  DataIn$ = ""
    >>>  While IsFalse ThreadClose
    >>>     If (Comm(#hComm, RxQue) < 9 ) Then
    >>>           Sleep 1
    >>>           Iterate Loop
    >>>        End If
    >>>     Comm Recv #hComm, 9, DataIn$
    >>>     Control Set Text hDlg, %MesgBox, DataIn$
    >>>  Wend
    >>>  Function = %TRUE
    >>>  End Function
    This set-up started out being much more complicated. But even after reducing it to what you see here, I still can't get it
    to work. What I expected was for the ASCII values of "02, 04, 05, 06, 17, 30, 39, 41, 46" to show up in the "%MesgBox"
    TextBox after pressing "Send Text" on the machine running Comm.bas . What I get, after pressing "Send Text" on theComm.bas
    program 12 times (sometimes 11, sometimes 13), is "ÿÿÿÿÿÿÿÿÿ". On the surface it certainly looks like a baud rate is one
    of the problems; among some of the things that I've tried is lowering the baud rate down to 300 (on both machines of
    course). I have checked and re-checked the port settings and they are correct. About the only thing that is even slightly
    encouraging is that there are 9 of the ÿ's showing up.

    If it makes any difference, when the academics are over the "real" program will be sending and receiving binary data.

    Thanks for taking the time to read this. Any help would be much appreciated.

    Wesley Brown

    Last edited by Gary Beene; 12 Jul 2014, 07:55 PM. Reason: Code: tags

    You say the settings are correct, but how have you actually configured the comm ports? Did you retain the COMM SET statements used in the original example, or have you omitted or changed them?

    Without seeing the complete code you are using, it is a bit difficult to offer exact answers... would it be possible for you to post the code? Thanks!

    PowerBASIC Support
    mailto:[email protected][email protected]</A>
    mailto:[email protected]


      I solved the problem by further modifying Comm.bas so that it would transmit displayable characters rather than
      control characters as follows . . .
      >>> CallBack Function Send_Callback() As Long
      >>> Comm Send #hComm, Chr$(&H41) 'A
      >>> Comm Send #hComm, Chr$(&H42) 'B
      >>> Comm Send #hComm, Chr$(&H43) 'C
      >>> Comm Send #hComm, Chr$(&H44) 'D
      >>> Comm Send #hComm, Chr$(&H45) 'E
      >>> Comm Send #hComm, Chr$(&H46) 'F
      >>> Comm Send #hComm, Chr$(&H47) 'G
      >>> Comm Send #hComm, Chr$(&H48) 'H
      >>> Comm Send #hComm, Chr$(&H49) 'I
      >>> Comm Send #hComm, Chr$(&H4A) 'J
      >>> Comm Send #hComm, Chr$(&H4B) 'K
      >>> Comm Send #hComm, Chr$(&H4C) 'L
      >>> Function = %True
      >>> End Function
      I'm still not clear as to why I wasn't at least getting trash on the first click of "Send Text' rather than the 12th
      (or 11th or 13th); but I am willing to accept that it has something to do with the warning in the PowerBasic help
      files concerning 'COMM SEND' which states:

      "Be careful when using fixed length strings, as trailing padding in the string may give unexpected results in your
      data flow. This is especially true is you are performing a binary file transfer through the serial port, the
      padding bytes will produce a binary file at the “remote end” with interspersed spaces. Also, avoid using ASCIIZ
      strings with binary data, as the first embedded zero byte will cause the data sent to the serial port to be
      prematurely truncated, due to zero-byte termination of such strings".

      Thanks for responding Lance. I apologize for posing a question to the forum after having worked for 17 hours
      straight without first getting some sleep and pondering things longer.

      Wesley Brown

      Last edited by Gary Beene; 12 Jul 2014, 07:54 PM. Reason: Code: tags