Results 1 to 16 of 16

Thread: Serial Port

  1. #1

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Serial Port

    I have two computers connected to each other through the serial port with an RS232 cable. I tried with some snippets I found out there, but I cannot make anything going out of the port to the other computer (MSComm1.OutBufferCount is always 0 regardless what I wrote on its Output property). Does anybody have any example I can try? TIA,
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  2. #2
    New Member
    Join Date
    May 2001
    Location
    Slovenia
    Posts
    11

    Re: Serial Port

    Do you have the right cable? It should be "cross-over"

  3. #3

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Serial Port

    It's the same RS232 cable I've used before to transmit files from one machine to another (using Norton Commander, for example).
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  4. #4
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Serial Port

    Is your firewall blocking it?

  5. #5

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Serial Port

    Nope, both firewalls are disabled and stil the OutBufferCount remains equals to zero. Any ideas?
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  6. #6
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Serial Port

    Are you using any handshaking (RTS/CTS) in your code?
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  7. #7
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Serial Port

    Actually - can you post some code
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  8. #8

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Serial Port

    On the receiver:

    VB Code:
    1. Private Sub Form_Load()
    2.     ' Use COM2.
    3.     MSComm1.CommPort = 2
    4.     ' 9600 baud, no parity, 8 data, and 1 stop bit.
    5.     MSComm1.Settings = "9600,N,8,1"
    6. End Sub
    7. '*************
    8.  
    9. Private Sub MSComm1_OnComm()
    10.     Stop
    11. End Sub

    On the sender:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim sText As Variant
    5.    
    6.     With MSComm1
    7.         .PortOpen = True
    8.         sText = Chr$(Val(txtSend.Text))
    9.        
    10.         .Output = sText
    11.         Do
    12.             DoEvents
    13.         Loop Until .OutBufferCount = 0
    14.         .PortOpen = False
    15.     End With
    16. End Sub
    17.  
    18. Private Sub Form_Load()
    19.     ' Use COM1.
    20.     MSComm1.CommPort = 1
    21.     ' 9600 baud, no parity, 8 data, and 1 stop bit.
    22.     MSComm1.Settings = "9600,N,8,1"
    23. End Sub
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  9. #9

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Serial Port

    Quote Originally Posted by David.Poundall
    Are you using any handshaking (RTS/CTS) in your code?
    No. Handshaking = 0 comNone
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  10. #10

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Serial Port

    The cable is Ok, and the firewall is not blocking since I receive these events on the receiver:

    comEvDSR: When I open the port on the sender to send a data out (twice)
    comEventBreak: When I close the port after (not) sending the data.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  11. #11
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Serial Port

    OK - just a couple of points. I am referring to a comprehensive PLC comms routine that I have been successfully running for the past 5 years. But as you may imagine it was written five years ago. Anyway - enough of my problem and on to yours.

    Here are a few snippets of my code that works. Unlike yours it works in binary mode though.

    My receive code just unloads the Rxbuff array. The data received event fires when I receive the correct number of incoming bytes as PLC comms is quite predictable.

    VB Code:
    1. ' ---------------------------------------------------------------------
    2. ' Globals
    3. Public Rxbuff() As Byte
    4.  
    5. ' ---------------------------------------------------------------------
    6. ' Code behind the Form containing the comm control
    7. Private Sub MSComm1_OnComm()
    8.      Select Case MSComm1.CommEvent          
    9.           Case comEvReceive         ' Received RThreshold # of chars.
    10.                Rxbuff() = MSComm1.Input
    11.                Call HandleReceivedData              
    12.      End Select
    13. End Sub
    14.  
    15. ' ---------------------------------------------------------------------
    16. ' called at comms kickoff
    17. Sub InitialiseComms()
    18.      frmPlcComms.MSComm1.CommPort = ComPort
    19.      frmPlcComms.MSComm1.Settings = "9600,E,7,1"
    20.      frmPlcComms.MSComm1.PortOpen = True
    21.      frmPlcComms.MSComm1.DTREnable = True
    22.      frmPlcComms.MSComm1.RTSEnable = True
    23.      frmPlcComms.MSComm1.InputMode = comInputModeBinary
    24.      frmPlcComms.MSComm1.InputLen = 0       'Forces entire buffer read
    25. End Sub
    26.  
    27. ' ---------------------------------------------------------------------
    28. ' send code
    29. Sub Transmit()
    30.     Dim txbuff() As Byte, k as long
    31.     Dim SendBuffer(1) As Variant                       ' VIP for sending Binary
    32.  
    33.     frmPlcComms.MSComm1.InBufferCount = 0     'Flush the RX Buffer
    34.          
    35.     ' Dim the TX buffer
    36.     redim txbuff(0 to 100)
    37.  
    38.     k = 0
    39.     ' Send pre-amble
    40.     txbuff(k) = &H2   : k = k+1
    41.     txbuff(k) = &H31  : k = k+1
    42.     txbuff(k) = &H31  : k = k+1
    43.  
    44.     ' Add further text to the byte buffer as byte...
    45.  
    46.     ' Trim the TXbuffer
    47.     ReDim txbuff(0 to k-1)
    48.  
    49.     SendBuffer(1) = txbuff()
    50.     frmPlcComms.MSComm1.Output = SendBuffer(1)
    51.  
    52.     ' Set the  RX buffer length to the value expected from the PLC
    53.     frmPlcComms.MSComm1.RThreshold = 1          ' Note - Not typical.
    54.    
    55. End Sub

    Something in that lot may jog your thinking.
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  12. #12

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Serial Port

    It seems I made it work.
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  13. #13
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Serial Port

    Hmmm have you tried ....

    VB Code:
    1. sText = txtSend.Text
    ??
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  14. #14

    Thread Starter
    Need-a-life Member Mc Brain's Avatar
    Join Date
    Apr 2000
    Location
    Buenos Aires, Argentina
    Posts
    6,808

    Re: Serial Port

    Quote Originally Posted by David.Poundall
    OK - just a couple of points. I am referring to a comprehensive PLC comms routine that I have been successfully running for the past 5 years. But as you may imagine it was written five years ago. Anyway - enough of my problem and on to yours.

    Here are a few snippets of my code that works. Unlike yours it works in binary mode though.

    My receive code just unloads the Rxbuff array. The data received event fires when I receive the correct number of incoming bytes as PLC comms is quite predictable.

    VB Code:
    1. ' ---------------------------------------------------------------------
    2. ' Globals
    3. Public Rxbuff() As Byte
    4.  
    5. ' ---------------------------------------------------------------------
    6. ' Code behind the Form containing the comm control
    7. Private Sub MSComm1_OnComm()
    8.      Select Case MSComm1.CommEvent          
    9.           Case comEvReceive         ' Received RThreshold # of chars.
    10.                Rxbuff() = MSComm1.Input
    11.                Call HandleReceivedData              
    12.      End Select
    13. End Sub
    14.  
    15. ' ---------------------------------------------------------------------
    16. ' called at comms kickoff
    17. Sub InitialiseComms()
    18.      frmPlcComms.MSComm1.CommPort = ComPort
    19.      frmPlcComms.MSComm1.Settings = "9600,E,7,1"
    20.      frmPlcComms.MSComm1.PortOpen = True
    21.      frmPlcComms.MSComm1.DTREnable = True
    22.      frmPlcComms.MSComm1.RTSEnable = True
    23.      frmPlcComms.MSComm1.InputMode = comInputModeBinary
    24.      frmPlcComms.MSComm1.InputLen = 0       'Forces entire buffer read
    25. End Sub
    26.  
    27. ' ---------------------------------------------------------------------
    28. ' send code
    29. Sub Transmit()
    30.     Dim txbuff() As Byte, k as long
    31.     Dim SendBuffer(1) As Variant                       ' VIP for sending Binary
    32.  
    33.     frmPlcComms.MSComm1.InBufferCount = 0     'Flush the RX Buffer
    34.          
    35.     ' Dim the TX buffer
    36.     redim txbuff(0 to 100)
    37.  
    38.     k = 0
    39.     ' Send pre-amble
    40.     txbuff(k) = &H2   : k = k+1
    41.     txbuff(k) = &H31  : k = k+1
    42.     txbuff(k) = &H31  : k = k+1
    43.  
    44.     ' Add further text to the byte buffer as byte...
    45.  
    46.     ' Trim the TXbuffer
    47.     ReDim txbuff(0 to k-1)
    48.  
    49.     SendBuffer(1) = txbuff()
    50.     frmPlcComms.MSComm1.Output = SendBuffer(1)
    51.  
    52.     ' Set the  RX buffer length to the value expected from the PLC
    53.     frmPlcComms.MSComm1.RThreshold = 1          ' Note - Not typical.
    54.    
    55. End Sub

    Something in that lot may jog your thinking.
    Yes, the key was the "RThreshold" and I had found it (almost at the same time you posted it) at http://www.programmers-corner.com/sourcecode/111.


    VB Code:
    1. 'enable the oncomm event for every reveived character
    2.     .RThreshold = 1
    Emiliano F. Martín


    If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
    Encourage the person who helped you to keep doing it, and give him the points he deserves.


    MP3 Organizer: Freeware to logically organize all your MP3s.

  15. #15
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Serial Port

    Quote Originally Posted by Mc Brain
    It seems I made it work.
    That's good news. How did you do it ?
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

  16. #16
    Frenzied Member David.Poundall's Avatar
    Join Date
    Sep 2002
    Location
    Robin Hood Land
    Posts
    1,457

    Re: Serial Port

    Cool
    David

    Learn the Rules so that you know how to break them properly.

    Printing dll dBTools MZTools Winsock API WinsockVB More Winsock SGrid2 MSChart Mail2Web

    If you have found this thread useful then read this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width