Results 1 to 28 of 28

Thread: [RESOLVED] COM1 Problems

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Resolved [RESOLVED] COM1 Problems

    i wrote this code to send a byte the the COM1 when an arrow is pressed on the keyboard

    Option Explicit

    Private Sub Command1_Click()
    MSComm1.PortOpen = False
    End
    End Sub

    Private Sub Form_Load()
    MSComm1.CommPort = 1
    MSComm1.Settings = "2400,N,8,1"
    MSComm1.DTREnable = True
    MSComm1.PortOpen = True
    Dim oApp As Object
    Dim oDoc As Object
    Set oApp = CreateObject("Word.Application")
    Set oDoc = oApp.documents.open("C:\Test.doc")
    oApp.Visible = True
    Set oDoc = Nothing
    Set oDoc = Nothing

    Me.KeyPreview = True
    End Sub
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    Select Case KeyCode
    Case 37

    MSComm1.Output = "11110010"
    Text1.Text = 11110010


    Case 38

    MSComm1.Output = "11110001"
    Text1.Text = 11110001
    Text2.Text = MSComm1.OutBufferCount


    Case 39

    MSComm1.Output = "11111000"
    Text1.Text = 11111000
    Text2.Text = MSComm1.OutBufferCount


    Case 40

    MSComm1.Output = "11110100"
    Text1.Text = 11110100
    Text2.Text = MSComm1.OutBufferCount


    End Select
    End Sub

    i tested the COM out buffer by using
    Text2.Text = MSComm1.OutBufferCount

    but it returned nottin which means the serial port isnt getting the byte!

    can anyone help with this?
    Last edited by Elios115; Nov 5th, 2005 at 06:56 PM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    any helpp

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

    Re: COM1 Problems

    What are you trying to send data to? Are you sure the parameters are correct?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    to the serial port to a microcontroller
    yeah i think they r right

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    what do u think?

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Thumbs down Re: COM1 Problems

    does anyone in this forum know what should be done to make this code work?
    Last edited by Elios115; Nov 6th, 2005 at 05:24 AM.

  7. #7
    Fanatic Member namrekka's Avatar
    Join Date
    Feb 2005
    Location
    Netherlands
    Posts
    639

    Re: COM1 Problems

    MSComm1.Output = "11110010"
    Do you mean that this is 1 Byte or 8 bytes?

  8. #8
    Frenzied Member trisuglow's Avatar
    Join Date
    Jan 2002
    Location
    Horsham, Sussex, UK
    Posts
    1,536

    Re: COM1 Problems

    With reference to post #1:

    As I understand it, OutBufferCount returns the number of bytes in the output buffer. If it is zero this means the bytes have been sent .
    This world is not my home. I'm just passing through.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    i m trying to send it as one byte, bit by bite

  10. #10
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: COM1 Problems

    Quote Originally Posted by namrekka
    Do you mean that this is 1 Byte or 8 bytes?
    That is 8 BYTES....

    You cannot send one bit... you always send a byte (8 bits) at one time.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    so each 1 or 0 sent is a byte?
    so how can i send a byte like 11110100?
    mscomm.output="????"

  12. #12
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: COM1 Problems

    Use this function like this:

    mscomm.output = BinToByte("11110100")
    VB Code:
    1. Private Function BinToByte(ByVal Bin As String) As String
    2.     Dim B As Byte, K As Long
    3.    
    4.     For K = 1 To 8
    5.         If Mid$(Bin, K, 1) = "1" Then
    6.             B = B Or 2 ^ (K - 1)
    7.         End If
    8.     Next K
    9.    
    10.     BinToByte = Chr(B)
    11. End Function

  13. #13
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: COM1 Problems

    Quote Originally Posted by Elios115
    Text2.Text = MSComm1.OutBufferCount

    but it returned nottin which means the serial port isnt getting the byte!

    can anyone help with this?
    It's probably because you are not sending a lot of data, and it's sending the data right away. So when you call MSComm1.OutBufferCount, you always get 0 becasue the data is already sent.

    Send a couple of kilo bytes (more than 1024 bytes), and then you will see that the MSComm1.OutBufferCount will slowly go to 0 as it's sending...

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    man u r totally right , i sent out a sentence and it showed the buffer size.

    so this function sends the input code (111101000) as bits

  15. #15
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: COM1 Problems

    Quote Originally Posted by Elios115
    man u r totally right , i sent out a sentence and it showed the buffer size.

    so this function sends the input code (111101000) as bits
    Yup...

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    thnks man

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    but there s soemthing wrong cuz when i wrong sentence to send the outputbuffer size returned 0 no matter how long the sentence was

  18. #18
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: COM1 Problems

    But did the data got sent or not ?

    I still think it's sending fast, so by the time you read the output count, it's already sent...

    A sentence is not much different then one byte, as I was saying bere send A LOT of data, over 1000 characters, even then it might be too fast.

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    i tried to send 1000 characters but the count was still 0.
    it worked when i just used the
    MScomm.output="1001010100" so
    is there any equivalent to send those bits?
    11111000,11110100,11110010,11110001 without using the BinToByte function?

  20. #20
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: COM1 Problems

    First of all there is a small error in Michael's code
    VB Code:
    1. 'This line
    2. ' B = B Or 2 ^ (K - 1)
    3. 'Should be
    4. B = B Or 2 ^ (8 - K)
    Try this to send all four bytes at once
    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Dim sdata As Variant
    5.     sdata = Array("11111000", "11110100", "11110010", "11110001")
    6.     MSComm1.Output = BinToByte(sdata)
    7. End Sub
    8.  
    9. Private Function BinToByte(ByVal Bin As Variant) As Variant
    10.     Dim B As Byte, K As Long, j As Long
    11.     Dim s As String
    12.     s = ""
    13.     For j = LBound(Bin) To UBound(Bin)
    14.         B = 0
    15.         For K = 1 To 8
    16.         If Mid$(Bin(j), K, 1) = "1" Then
    17.             B = B Or 2 ^ (8 - K)
    18.         End If
    19.         Next K
    20.         s = s & Chr(B)
    21.     Next j
    22.     BinToByte = s
    23. End Function
    msdn says that the output should be a variant.

  21. #21
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: COM1 Problems

    Quote Originally Posted by moeur
    First of all there is a small error in Michael's code
    VB Code:
    1. 'This line
    2. ' B = B Or 2 ^ (K - 1)
    3. 'Should be
    4. B = B Or 2 ^ (8 - K)
    It's not really an error, it just depends on where you want the first byte to be, at the end or beginning... I don't think there are any rules on how binary is represented.

    The Array thing can be made a little shorter with ParamArray:
    VB Code:
    1. Private Function BinToByte([b]ParamArray Bin() As Variant[/b]) As Variant
    2.     Dim B As Byte, K As Long, j As Long
    3.     Dim s As String
    4.     s = ""
    5.    
    6.     For j = LBound(Bin) To UBound(Bin)
    7.         B = 0
    8.        
    9.         For K = 1 To 8
    10.             If Mid$(Bin(j), K, 1) = "1" Then
    11.                 B = B Or 2 ^ (8 - K)
    12.             End If
    13.         Next K
    14.        
    15.         s = s & Chr(B)
    16.     Next j
    17.    
    18.     BinToByte = s
    19. End Function
    20.  
    21. Private Sub Form_Load()
    22.     Debug.Print BinToByte("11111000", "11110100", "11110010", "11110001")
    23. End Sub

  22. #22

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    guys the matter is that i need to send a byte (8 bits) to the serialport when i press one of the four arrows on the keyboard. this byte will control a device. so i just need to send 8 bits on each arrow press, thats it. i used the case with the keydown function for each of the four arrows to send a specific byte. did u get the idea? thx

  23. #23
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: COM1 Problems

    I think we've shown you a couple ways to do this.
    You need some method of determining that the bytes are arriving on the other end.

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

    Re: COM1 Problems

    As long as you are only using those four codes, just use this:
    VB Code:
    1. Select Case KeyCode
    2. Case 37
    3.   MSComm1.Output = "ò"
    4.   Text1.Text = 11110010
    5. Case 38
    6.   MSComm1.Output = "ñ"
    7.   Text1.Text = 11110001
    8.   Text2.Text = MSComm1.OutBufferCount
    9. Case 39
    10.   MSComm1.Output = "ø"
    11.   Text1.Text = 11111000
    12.   Text2.Text = MSComm1.OutBufferCount
    13. Case 40
    14.   MSComm1.Output = "ô"
    15.   Text1.Text = 11110100
    16.   Text2.Text = MSComm1.OutBufferCount
    17. End Select

    Debug.Print BinToByte("11111000", "11110100", "11110010", "11110001")
    øôòñ

  25. #25
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: COM1 Problems

    I don't think there are any rules on how binary is represented.
    Yes there are, just like representing decimal numbers, the most significant digit goes on the left, least significant on the right.

  26. #26

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    i c so those characters are the equivalent byte i need to send. thats what i needed thx guys for ur help. you have the best forum

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Nov 2005
    Posts
    103

    Re: COM1 Problems

    but VB isnt accepting the characters! it is putting a question mark instead. any ideas plz

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

    Re: COM1 Problems

    You can also output chr$(248) to get 'ø', so just use the character codes.

    248 ø
    244 ô
    242 ò
    241 ñ

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