By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,886 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,886 IT Pros & Developers. It's quick & easy.

Reading data from serial port and saving it in text file

P: 5
This code is meant to take an input from the serial port and then save the input from the serial port as the name and as the data to a text file but all i am getting is an empty text file or a text file with only "," in it. It would be much appreciated if anyone could help with this problem. Thanks


Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.     MSComm1.PortOpen = False ' Close the comm port
  3. End Sub
  4.  
  5. Private Sub Timer1_Timer()
  6.     If MSComm1.PortOpen = False Then ' If comm port is not open
  7.        MSComm1.PortOpen = True ' Open it
  8.     End If
  9.  
  10.     If MSComm1.InBufferCount > 0 Then ' If theres data in comm buffer
  11.         inst = inst + MSComm1.Input ' Get the data
  12.         Text1.Text = inst ' Show its value
  13.         Filename.Text = Text1.Text
  14.         Text2.Text = Text1.Text
  15.     End If
  16.     If Text1.DataChanged < True Then
  17.         Open "C:\test\" + Filename.Text + ".txt" For Output As #1 'location of the file.
  18.             Write #1, Text1.Text, Text2.Text 'The things you want to save
  19.         Close 1
  20.     End If
  21.  
  22. End Sub
Mar 26 '07 #1
Share this Question
Share on Google+
8 Replies


P: 5
Sorry didn't add this to first post but anyway new to this type of thing.

I was working on making it save to a specific folder in many small files for me to sort through the data (small amounts of information currently for it) without a save popup or dialog to save. The timer is also set to 1000mils didn't know if that had any bearing on the saves or not. Thanks again for any input anyone can offer.
Mar 26 '07 #2

Expert 5K+
P: 8,434
Two very important questions...
  1. What version of VB is this? (From the looks of it, I'd guess VB6)
  2. Are you getting the wrong data, or just writing the files incorrectly?
If you don't know the answer to the second question, then you need to investigate and work it out. After all, there are two distinct operations going on - you need to know which one is misbehaving before you can fix it.
Mar 27 '07 #3

vijaydiwakar
100+
P: 579
This code is meant to take an input from the serial port and then save the input from the serial port as the name and as the data to a text file but all i am getting is an empty text file or a text file with only "," in it. It would be much appreciated if anyone could help with this problem. Thanks


Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Unload(Cancel As Integer)
  2.     MSComm1.PortOpen = False ' Close the comm port
  3. End Sub
  4.  
  5. Private Sub Timer1_Timer()
  6.     If MSComm1.PortOpen = False Then ' If comm port is not open
  7.        MSComm1.PortOpen = True ' Open it
  8.     End If
  9.  
  10.     If MSComm1.InBufferCount > 0 Then ' If theres data in comm buffer
  11.         inst = inst + MSComm1.Input ' Get the data
  12.         Text1.Text = inst ' Show its value
  13.         Filename.Text = Text1.Text
  14.         Text2.Text = Text1.Text
  15.     End If
  16.     If Text1.DataChanged < True Then
  17.         Open "C:\test\" + Filename.Text + ".txt" For Output As #1 'location of the file.
  18.             Write #1, Text1.Text, Text2.Text 'The things you want to save
  19.         Close 1
  20.     End If
  21.  
  22. End Sub
what's the interval of ur timmer?
try to increase it or u may use onComm event insted of timmer..
cut and paste the code of text1.datachanged into inbuffercount>0
try it
Good Luck
Mar 27 '07 #4

P: 5
Two very important questions...
  1. What version of VB is this? (From the looks of it, I'd guess VB6)
  2. Are you getting the wrong data, or just writing the files incorrectly?
Yeah the VB version is 6 and its not wrong data really just when it does get information on that port it does save a text file only its names ".txt" and it only contains "" or "," at most and does not contain any of the data
Mar 27 '07 #5

P: 5
what's the interval of ur timmer?
try to increase it or u may use onComm event insted of timmer..
cut and paste the code of text1.datachanged into inbuffercount>0
try it
Good Luck
Thanks vijaydiwakari will try what you said. Also tried moving the code fo text1.datachanged up under inbuffercount but it gave me that same response. Keep those replies comming any informations that leads me towards a semi working program is better than what i got so far :)
Mar 27 '07 #6

Expert 5K+
P: 8,434
Yeah the VB version is 6 and its not wrong data really just when it does get information on that port it does save a text file only its names ".txt" and it only contains "" or "," at most and does not contain any of the data
Sounds as though vijaydiwakar is probably on the right track. You're probably checking the port too often, before any data arrives, or something. The OnComm event does sound like a better option.

Let us know how it goes.

Also, consider simply writing stuff to the immediate window rather than actually creating the files. It's much quicker and simpler to debug that way.
Mar 27 '07 #7

P: 5
Thanks for the help so far i have this and it works...now lemme add some stuff and see what i can screw up next *grins*

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2. Open "z:\" + Test1 + ".txt" For Append As #1
  3. Write #1, Text1
  4. Close #1
  5. End Sub
  6. Private Sub Form_Load()
  7. If MSComm1.PortOpen = False Then ' If comm port is not open
  8.     MSComm1.PortOpen = True ' Open it
  9. End If
  10. End Sub
  11. Private Sub MSComm1_OnComm()
  12. If MSComm1.InBufferCount > 0 Then
  13.         inst = MSComm1.Input
  14.       Text1 = inst
  15.  
  16. End If
  17. End Sub
  18. Private Sub Text1_Change()
  19. Call Command1_Click
  20. End Sub
Mar 29 '07 #8

Expert 5K+
P: 8,434
Thanks for the help so far i have this and it works...now lemme add some stuff and see what i can screw up next *grins*
That's the spirit! :D

Let us know how it goes. And vijaydiwakar, thanks for your help.
Mar 30 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.