473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Reading data from serial port and saving it in text file

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
8 12179
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
Killer42
8,435 Expert 8TB
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
579 512MB
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
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
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
Killer42
8,435 Expert 8TB
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
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
Killer42
8,435 Expert 8TB
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

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

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
7
by: Chris | last post by:
Here's the situation: I work at a scientific institution and I have a portable electronic device which is used to take measurements. The device produces a very small amount of numerical data,...
16
by: bloggsfred00 | last post by:
I need to read incoming bytes on a COM port but I do not want to have the script hang if there is nothing to read. Is there any way to have PHP interrogate a COM port buffer to see if there is...
0
by: pranavkrish | last post by:
I am using serialPort control to read data from a barcode scanner , Can you please suggest me the most reliable methof to do this. Now I generate an even based on the data received in the serial port...
6
by: John Wright | last post by:
I am trying to read the data from a device on a serial port. I connect just fine and can receive data fine in text mode but not in binary mode. In text mode the data from the device comes in...
2
by: crampio | last post by:
Hello everyone, I generally look at Google and other websites before I post a question, but trust me I still cannot find and answer to this problem. I'm using VB.net. My problem being is that I...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
1
by: ghjk | last post by:
I'm trying to read sms from GSM modem using c#. my code is only working for at command. When I enter at+cmgr=1 it says"ERROR". but when i typr it hyperterminal. It is working.Could you please tell me...
0
by: =?Utf-8?B?Q2hhcmxpZQ==?= | last post by:
Hi, This apparently is a common problem and I've yet to read a solution that actually works for my specific situation. I have a Zebra RW220 printer that I connect to via Bluetooth connection...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.