|
-
Aug 7th, 2012, 03:42 PM
#1
Thread Starter
Hyperactive Member
Comms Problem
Hi All
I am sending 13 characters to my vb6 prog via comms which works most of the time but then for some reason the 13th character becomes the first character and it does this all the time until i reload my vb6 program example:-
1907122220870 is how it should be but ends up like 0190712222087 help would be very greatful
Code:
Private Sub Form_Load()
Dim textfile As String
Open "C:\users\administrator\dirnames.txt" For Input As #1
Do While Not EOF(1)
Input #1, textfile
FolderCreator.Combo1.AddItem (textfile)
Loop
Close #1
' Show Date on main screen.
'Label3.Caption = DateValue(Now)
' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 13
' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 13
' 9600 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "9600,N,8,1"
' Disable DTR
MSComm1.DTREnable = False
' Open COM1
MSComm1.CommPort = 1
MSComm1.PortOpen = True
MSComm1.InBufferCount = 0
End Sub
Private Sub MSComm1_OnComm()
Dim sData As String ' Holds our incoming data
Dim lHighByte As Long ' Holds HighByte value
Dim lLowByte As Long ' Holds LowByte value
Dim lWord As Long ' Holds the Word result
' If comEvReceive Event then get data and display
If MSComm1.CommEvent = comEvReceive Then
' Shell ("subst Z: /D")
'Shell ("net use /Delete Z:")
sData = MSComm1.Input ' Get data (5 bytes)
' lHighByte = Asc(Mid$(sData, 1, 1)) ' get 1st byte
' lLowByte = Asc(Mid$(sData, 2, 1)) ' Get 2nd byte
' Combine bytes into a word
' lWord = (lHighByte * &H100) Or lLowByte
' Convert value to a string and display
' lblRCTime.Caption = sData
' MkDir sData
' Dim folder As String, filename As String, path As String
Label8.Caption = sData
'MSComm1.Output = sData
Dim wshThisShell As WshShell
Dim lngRet As Long
Dim strShellCommand As String
Dim strBatchPath As String
'label8.caption = ""
Set wshThisShell = New WshShell
strBatchPath = "c:\deletevert.bat"
'the path for the batch file you're using
strShellCommand = """" & strBatchPath & """"
'the ridiculous number of quotation marks is necessary
'when there is a space in one or more of the folder names
lngRet = wshThisShell.Run(strShellCommand, vbNormalFocus, vbTrue)
'set 3rd argument above to vbFalse for asynchronous
'execution of the batch file.
'label8.caption = ""
'label9.caption = ""
If Dir$("e:\" & sData, vbDirectory) = "" Then
Label9.Caption = "Directory does not exist."
MkDir "e:\" & sData
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(0)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(1)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(2)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(3)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(4)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(5)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(6)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(7)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(8)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(9)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(10)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(11)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(12)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(13)
MkDir "e:\" & sData & "\" & FolderCreator.Combo1.List(14)
'Shell ("subst F: " & "e:\" & sData)
Shell ("net use Z: " & "\\ENDOFLINE\STORAGE\" & sData & " /persistent:yes")
Shell ("net use Z: " & "\\ENDOFLINE\STORAGE\" & sData & " /persistent:yes")
MSComm1.Output = sData
Regards
Steve
Last edited by Hack; Aug 8th, 2012 at 10:56 AM.
Reason: Added Code Tags Last edited by sbarber007; Yesterday at 04:50 PM.
-
Aug 7th, 2012, 05:08 PM
#2
Re: Comms Problem
Why would you think you would get 5 bytes of data here?
Code:
sData = MSComm1.Input ' Get data (5 bytes)
You have your threshold set to 13 so the event will not fire until 13 bytes are present in the buffer.
Input would read all bytes in the buffer unless maybe if you have your sData string defined as a fixed length of 5 in which case you would run into problems due to the fact that 13 is not evenly divisible by 5
strShellCommand = """" & strBatchPath & """"
'the ridiculous number of quotation marks is necessary
You can optionally do it like this
Code:
strShellCommand = CHR$(34) & strBatchPath & CHR$(34)
As for your issue I can't tell what you are really trying to do
-
Aug 8th, 2012, 01:35 AM
#3
Re: Comms Problem
Also, the RTHreshold Property value is the minimum number of bytes that must be in the MSComm internal buffer before the OnComm event triggers. By the time it does trigger, there may be more than RTHreshold bytes in the buffer which you will then read.
I find it 'safer' to set RTHreshold to 1 and buffer the input until the end of the record is reached (in your case, until you have at least 13 bytes in the buffer)
Code:
Private Sub MSComm_OnComm()
'
' Assumes RTHreshold Property is set to a value > 0
' (Recommended value is 1)
'
Static strBuffer As String
Dim strData As String
Dim strRX As String
Dim boComplete As Boolean
Select Case MSComm.CommEvent
Case comEvReceive
strData = MSComm.Input
strBuffer = strBuffer & strData
Do
If Len(strBuffer) >= 13 Then
strRX = Mid$(strBuffer, 1, 13)
'
' rest of your code goes here - strRX contains the 13 characters sent
' After you've done all your processing add the following code
'
If Len(strBuffer) = 13 Then
strBuffer = ""
boComplete = True
Else
strBuffer = Mid$(strBuffer, 14)
End If
Else
boComplete = True
End If
Loop Until boComplete = True
End Select
End Sub
Last edited by Doogle; Aug 8th, 2012 at 01:39 AM.
-
Aug 8th, 2012, 07:57 AM
#4
Re: Comms Problem
Yep, That's the way I would do it too.
-
Aug 8th, 2012, 04:09 PM
#5
Thread Starter
Hyperactive Member
Re: Comms Problem
Hi Thanks For all your replays
@doogle
Basicly what my program does is creates folders and and then creates a virtual drive from infomation from the comms "engine numbers", I have the comms open all the time waiting for 13 numbers to be sent then it creates a folder with the 13 numbers, another software then saves images to the virutal drive then another engines comes in to station and then another 13 unquie number is sent then creates folders and so on for each engine.
Could you please check though this script below I have inserted with your script Thanks
I dont understand the strBuffer = Mid$(strBuffer, 14)
what is the 14 value, and what does boolean and bocomplete do.
Private Sub Form_Load()
Dim textfile As String
Open "C:\users\administrator\dirnames.txt" For Input As #1
Do While Not EOF(1)
Input #1, textfile
FolderCreator.Combo1.AddItem (textfile)
Loop
Close #1
' Show Date on main screen.
'Label3.Caption = DateValue(Now)
' Fire Rx Event Every Two Bytes
MSComm1.RThreshold = 1 ' Is this correct
' When Inputting Data, Input 2 Bytes at a time
MSComm1.InputLen = 13 ' Is this correct
' 9600 Baud, No Parity, 8 Data Bits, 1 Stop Bit
MSComm1.Settings = "9600,N,8,1"
' Disable DTR
MSComm1.DTREnable = False
' Open COM1
MSComm1.CommPort = 1
MSComm1.PortOpen = True
MSComm1.InBufferCount = 0 ' Do I need this
End Sub
Private Sub MSComm1_OnComm()
'
' Assumes RTHreshold Property is set to a value > 0
' (Recommended value is 1)
'
Static strBuffer As String
Dim strData As String
Dim strRX As String
Dim boComplete As Boolean
Select Case MSComm1.CommEvent
Case comEvReceive
strData = MSComm1.Input
strBuffer = strBuffer & strData
Do
If Len(strBuffer) >= 13 Then
strRX = Mid$(strBuffer, 1, 13)
'
' rest of your code goes here - strRX contains the 13 characters sent
' After you've done all your processing add the following code
' START OF MY SCRIPT
Label8.Caption = strData
'MSComm1.Output = strData
Dim wshThisShell As WshShell
Dim lngRet As Long
Dim strShellCommand As String
Dim strBatchPath As String
'label8.caption = ""
Set wshThisShell = New WshShell
strBatchPath = "c:\deletevert.bat"
'the path for the batch file you're using
strShellCommand = Chr$(34) & strBatchPath & Chr$(34)
'the ridiculous number of quotation marks is necessary
'when there is a space in one or more of the folder names
lngRet = wshThisShell.Run(strShellCommand, vbNormalFocus, vbTrue)
'set 3rd argument above to vbFalse for asynchronous
'execution of the batch file.
'label8.caption = ""
'label9.caption = ""
If Dir$("e:\" & strData, vbDirectory) = "" Then
Label9.Caption = "Directory does not exist."
MkDir "e:\" & strData
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(0)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(1)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(2)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(3)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(4)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(5)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(6)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(7)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(8)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(9)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(10)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(11)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(12)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(13)
MkDir "e:\" & strData & "\" & FolderCreator.Combo1.List(14)
Shell ("net use Z: " & "\\ENDOFLINE\STORAGE\" & strData & " /persistent:yes")
Shell ("net use Z: " & "\\ENDOFLINE\STORAGE\" & strData & " /persistent:yes")
MSComm1.Output = strData
Else
ViewRejects.ViewList.AddItem strData & " REJECTED ON " & Now
Label5.Caption = ViewRejects.ViewList.ListCount
Label9.Caption = "Directory exists."
Shell ("net use Z: " & "\\ENDOFLINE\STORAGE\" & strData & " /persistent:yes")
Shell ("net use Z: " & "\\ENDOFLINE\STORAGE\" & strData & " /persistent:yes")
MSComm1.Output = strData
Open "e:\REJECTS.txt" For Append As #1
' For I = 0 To Lis't1.ListCount - 1
Print #1, strData & " REJECTED ON " & Now
' Next
Close #1
Open "e:\" & strData & "\REJECTS.txt" For Append As #2
' For I = 0 To Lis't1.ListCount - 1
Print #2, strData & " REJECTED ON " & Now
' Next
Close #2
End If
End If
' THE REST OF YOUR SCRIPT got compile error below
If Len(strBuffer) = 13 Then
strBuffer = ""
boComplete = True
Else
strBuffer = Mid$(strBuffer, 14)
End If
Else ' I get Compile Error Else Without IF
boComplete = True
End If
Loop Until boComplete = True
End Select
End Sub
Thanks for your help
Regards
Steve
Last edited by sbarber007; Aug 8th, 2012 at 05:08 PM.
-
Aug 8th, 2012, 07:52 PM
#6
Re: Comms Problem
You added to many End IFs
Code:
' Next
Close #2
End If
End If ' this is the cause of your error
' THE REST OF YOUR SCRIPT got compile error below
If Len(strBuffer) = 13 Then
-
Aug 9th, 2012, 02:52 AM
#7
Re: Comms Problem
 Originally Posted by sbarber007
I dont understand the strBuffer = Mid$(strBuffer, 14)
what is the 14 value, and what does boolean and bocomplete do.
After you've processed the 13 bytes in the buffer you need to check whether there were any more in there. (you might have actually read more than 13 bytes) If there are then that statement moves the remaining data to the front of the buffer. ie Byte number 14 to the end of strBuffer
The Boolean Data Type has two possible values "True" and "False" (where "False" has a value of 0 and "True" is 'Not False' )
boComplete is used to signal when to exit the Do Loop; there are two conditions when it will exit (a) When the data has been processed and there's nothing left in the buffer and (b) When there is less than 13 bytes in the buffer.
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
|