Problem saving data
Problem saving data
Problem saving data
Problem saving data
Problem saving data
Problem saving data Problem saving data Problem saving data Problem saving data Problem saving data Problem saving data Problem saving data Problem saving data
Problem saving data Problem saving data
Problem saving data
Go Back  Xtreme Visual Basic Talk > > > Problem saving data


Reply
 
Thread Tools Display Modes
  #1  
Old 05-21-2005, 10:42 AM
chrisjsmith chrisjsmith is offline
Newcomer
 
Join Date: Oct 2003
Location: Netherlands
Posts: 6
Unhappy Problem saving data


First I have to thank all those good folks who have answered other peoples questions and have helped me along the way. I’m only a beginner but really enjoy the programming, when it works.
Ok this is my problem. I’m transferring data to and from a CNC machine. The data is in ascii format. Through this forum I learnt all about mscomm and trial and error produced a usable programme. Uploading is no problem but receiving is the difficult part. If I check the incoming data for the machines particular EOF signature, chr(4) , and call another procedure to show and close up the new received file, all’s ok.
Trying to use the programme on another machine, same data format, but doesn’t have a distinctive EOF signature, it only catches the first couple of lines before closing up the new file. Obviously I’ve tried “InBufferCount” and “InBufferSize” to call close-up-the-new-file but to no avail. Here’s my code, first the one that works with chr(4) and second the one that fails. Please be gentle with my code as I only took this programming lark up a couple of years ago and even then I was well over half a century old.

‘This works

Private Sub Mscomm2_OnComm()

If MSComm2.CommEvent = comEvReceive Then

rBuffer = rBuffer & MSComm2.Input
TempNewFile.Write rBuffer
If InStr(rBuffer, chr(4)) Then Call ShowDataReceive
rBuffer = ""
End If

End Sub

Private Sub ShowDataReceive()

Animation1.AutoPlay = False
Animation1.Visible = False

LblReceivedData.Visible = True

Set TempNewFile = Nothing

If MSComm2.PortOpen Then MSComm2.PortOpen = False

If Check1.Value = vbChecked Then

On Error GoTo Errorhandler

Open DirReceiveFile For Input As #1
Do While Not EOF(1)
Line Input #1, ReceivedData
TxtResults.Text = TxtResults.Text + ReceivedData & vbCrLf
Loop
Close 1

End If
CmdSend.Visible = True

Exit Sub

Errorhandler:
MsgBox Str(Err.Number) & ": " & Err.Description, , "Error"

End Sub

‘This looses data after two lines

Private Sub Mscomm2_OnComm()

If MSComm2.CommEvent = comEvReceive Then
rBuffer = rBuffer & MSComm2.Input
TempNewFile.Write rBuffer
rBuffer = ""
End If

Call ShowDataReceive

End Sub
Reply With Quote
  #2  
Old 05-22-2005, 07:45 AM
SystemOfADown's Avatar
SystemOfADown SystemOfADown is offline
Contributor
 
Join Date: Aug 2003
Location: My Room
Posts: 607
Default

well nobody else is answering, so I'll try to help, but I don't think I can do much.
My only thoughts are the fact that the 1st part of the program waits for chr(4) before it Calls the ShowDataReceive function, basically it waits for what I'm assuming is an EOF character or something. The second part of the program, however, simply shows data after the sub has run. So, I'm assuming the problem may be that only parts of the data reach their destination when the OnComm function is fired, and so the ShowDataReceive sub is only showing part of the data. I hope this helps...
__________________
Programmers need love too.
Reply With Quote
  #3  
Old 05-22-2005, 12:07 PM
chrisjsmith chrisjsmith is offline
Newcomer
 
Join Date: Oct 2003
Location: Netherlands
Posts: 6
Default

Thanks for help SystemOfADown. I'll pursue your line of thinking and see where it leads.
Chris
Reply With Quote
  #4  
Old 05-22-2005, 03:54 PM
webbone's Avatar
webbone webbone is offline
Hydrogen Powered

Administrator
* Expert *
 
Join Date: Jul 2003
Location: Sacramento, CA
Posts: 6,090
Default

Does the second machine (the one that doesn't send Chr(4) to mark EOF) have either A) a character count or record/message length as part of the message? or B) some OTHER marker for EOF?

If there is no way to determine "end of packet" then how can you be sure that you have all the data? You could have some sort of timeout wherein if no more data arrives for N seconds then you assume end of message and process accordingly but that doesn't seem like something I would expect from a CNC or other machine tool.

SystemOfADown is pointing you correctly to the fact that you are ending your process before receiving all the data - in fact, your second code which doesn't work is doing exactly what you are telling it to which is to throw away the data immediately after receiving it.

Code:
Option Explicit Dim bMessageTimeout as Boolean 'in declarations section - flag value to indicate timer expired Dim sEOFMarker as String 'programmable EOF symbol(s) 'somewhere in your code at the place where you do whatever causes your device to send data back... bMessageTimeout = False 'clear the flag Timer1.Enabled = True 'start the timeout timer sEOFMarker = Chr$(4) 'for your original device 'sEOFMarker = "END" ' and example of using this to look for some other sort of EOF marker... 'now do the action which causes the device to send you a message back... Private Sub Mscomm2_OnComm() If MSComm2.CommEvent = comEvReceive Then rBuffer = rBuffer & MSComm2.Input TempNewFile.Write rBuffer Timer1.Enabled = False Timer1.Enabled = True ' reset the message timeout timer since data was received If (InStr(rBuffer, sEOFMarker)>0) Or bMessageTimeout Then Call ShowDataReceive rBuffer = "" End If End If 'This was missing in your original post - assuming you do have it! End Sub Private Sub Timer1_Timer() Timer1.Enabled = False bMessageTimeout = True End Sub

The above example could be used if you just have to rely on a timeout - add the timer to your project and set the interval to something large enough so you know you've gotten the entire message (but not so long you have to wait and wait and wait...) - probably a value of 2000 to 5000 should work (but again, I don't know your device).

Hopefully you can adapt the idea above to suit your needs. Let us know if you are still having trouble and post your revised code.
__________________
"With the appearance of the AddressOf operator, an entire industry has developed among authors illustrating how to do previously impossible tasks using Visual Basic. Another industry is rapidly developing among consultants helping users who have gotten into trouble attempting these tasks." -Dan Appleman
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Problem saving data
Problem saving data
Problem saving data Problem saving data
Problem saving data
Problem saving data
Problem saving data Problem saving data Problem saving data Problem saving data Problem saving data Problem saving data Problem saving data
Problem saving data
Problem saving data
 
Problem saving data
Problem saving data
 
-->