|
-
Nov 5th, 2005, 06:35 PM
#1
Thread Starter
Lively Member
[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.
-
Nov 5th, 2005, 07:17 PM
#2
Thread Starter
Lively Member
-
Nov 5th, 2005, 07:24 PM
#3
Re: COM1 Problems
What are you trying to send data to? Are you sure the parameters are correct?
-
Nov 5th, 2005, 07:50 PM
#4
Thread Starter
Lively Member
Re: COM1 Problems
to the serial port to a microcontroller
yeah i think they r right
-
Nov 6th, 2005, 01:25 AM
#5
Thread Starter
Lively Member
-
Nov 6th, 2005, 05:16 AM
#6
Thread Starter
Lively Member
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.
-
Nov 7th, 2005, 03:33 AM
#7
Re: COM1 Problems
MSComm1.Output = "11110010"
Do you mean that this is 1 Byte or 8 bytes?
-
Nov 7th, 2005, 04:17 AM
#8
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.
-
Nov 7th, 2005, 02:16 PM
#9
Thread Starter
Lively Member
Re: COM1 Problems
i m trying to send it as one byte, bit by bite
-
Nov 7th, 2005, 02:20 PM
#10
Re: COM1 Problems
 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.
-
Nov 7th, 2005, 02:29 PM
#11
Thread Starter
Lively Member
Re: COM1 Problems
so each 1 or 0 sent is a byte?
so how can i send a byte like 11110100?
mscomm.output="????"
-
Nov 7th, 2005, 02:44 PM
#12
Re: COM1 Problems
Use this function like this:
mscomm.output = BinToByte("11110100")
VB Code:
Private Function BinToByte(ByVal Bin As String) As String
Dim B As Byte, K As Long
For K = 1 To 8
If Mid$(Bin, K, 1) = "1" Then
B = B Or 2 ^ (K - 1)
End If
Next K
BinToByte = Chr(B)
End Function
-
Nov 7th, 2005, 02:49 PM
#13
Re: COM1 Problems
 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...
-
Nov 7th, 2005, 03:00 PM
#14
Thread Starter
Lively Member
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
-
Nov 7th, 2005, 03:01 PM
#15
Re: COM1 Problems
 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...
-
Nov 7th, 2005, 03:05 PM
#16
Thread Starter
Lively Member
-
Nov 7th, 2005, 03:10 PM
#17
Thread Starter
Lively Member
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
-
Nov 7th, 2005, 03:35 PM
#18
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.
-
Nov 7th, 2005, 03:41 PM
#19
Thread Starter
Lively Member
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?
-
Nov 7th, 2005, 04:46 PM
#20
Re: COM1 Problems
First of all there is a small error in Michael's code
VB Code:
'This line
' B = B Or 2 ^ (K - 1)
'Should be
B = B Or 2 ^ (8 - K)
Try this to send all four bytes at once
VB Code:
Option Explicit
Private Sub Command1_Click()
Dim sdata As Variant
sdata = Array("11111000", "11110100", "11110010", "11110001")
MSComm1.Output = BinToByte(sdata)
End Sub
Private Function BinToByte(ByVal Bin As Variant) As Variant
Dim B As Byte, K As Long, j As Long
Dim s As String
s = ""
For j = LBound(Bin) To UBound(Bin)
B = 0
For K = 1 To 8
If Mid$(Bin(j), K, 1) = "1" Then
B = B Or 2 ^ (8 - K)
End If
Next K
s = s & Chr(B)
Next j
BinToByte = s
End Function
msdn says that the output should be a variant.
-
Nov 7th, 2005, 05:03 PM
#21
Re: COM1 Problems
 Originally Posted by moeur
First of all there is a small error in Michael's code
VB Code:
'This line
' B = B Or 2 ^ (K - 1)
'Should be
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:
Private Function BinToByte([b]ParamArray Bin() As Variant[/b]) As Variant
Dim B As Byte, K As Long, j As Long
Dim s As String
s = ""
For j = LBound(Bin) To UBound(Bin)
B = 0
For K = 1 To 8
If Mid$(Bin(j), K, 1) = "1" Then
B = B Or 2 ^ (8 - K)
End If
Next K
s = s & Chr(B)
Next j
BinToByte = s
End Function
Private Sub Form_Load()
Debug.Print BinToByte("11111000", "11110100", "11110010", "11110001")
End Sub
-
Nov 7th, 2005, 05:29 PM
#22
Thread Starter
Lively Member
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
-
Nov 7th, 2005, 05:48 PM
#23
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.
-
Nov 7th, 2005, 07:33 PM
#24
Re: COM1 Problems
As long as you are only using those four codes, just use this:
VB Code:
Select Case KeyCode
Case 37
MSComm1.Output = "ò"
Text1.Text = 11110010
Case 38
MSComm1.Output = "ñ"
Text1.Text = 11110001
Text2.Text = MSComm1.OutBufferCount
Case 39
MSComm1.Output = "ø"
Text1.Text = 11111000
Text2.Text = MSComm1.OutBufferCount
Case 40
MSComm1.Output = "ô"
Text1.Text = 11110100
Text2.Text = MSComm1.OutBufferCount
End Select
Debug.Print BinToByte("11111000", "11110100", "11110010", "11110001")
øôòñ
-
Nov 7th, 2005, 07:39 PM
#25
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.
-
Nov 8th, 2005, 03:53 AM
#26
Thread Starter
Lively Member
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
-
Nov 8th, 2005, 04:03 AM
#27
Thread Starter
Lively Member
Re: COM1 Problems
but VB isnt accepting the characters! it is putting a question mark instead. any ideas plz
-
Nov 8th, 2005, 04:10 AM
#28
Re: COM1 Problems
You can also output chr$(248) to get 'ø', so just use the character codes.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|