473,569 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Reading data from serial port and saving it in text file

5 New Member
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 12247
glaskan
5 New Member
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 Recognized Expert Expert
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 Contributor
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.datachang ed into inbuffercount>0
try it
Good Luck
Mar 27 '07 #4
glaskan
5 New Member
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
glaskan
5 New Member
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.datachang ed into inbuffercount>0
try it
Good Luck
Thanks vijaydiwakari will try what you said. Also tried moving the code fo text1.datachang ed 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 Recognized Expert Expert
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
glaskan
5 New Member
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 Recognized Expert Expert
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
9068
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 same port to change the direction of the robot. The trajectory frame is managed by an applet, and the project works good when the applet is called by...
7
12745
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, about 10 to 15 numbers per measurement. This operation is performed frequently and I would like to implement an automatic solution that moves this...
16
4152
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 any content before reading it? Any other method to achieve the result would also be welcome. Cheers,
0
2662
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 : private void serialPortS2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { ...
6
11889
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 like this: S~5BBBBBBBBBBBBBBB5BBBBBBBB5B31BB4BB2B5BB4BBBE S is the start of the line, ~ indicates a good read, B is a blank reading, and the...
2
3078
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 don't know how to redirect the serial port output to a selected file. I looked in the forum but no luck. Here is the sequence of events. User...
9
14360
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, "That's easy, there's a lot out there, Google it,", 1 is a discussion on it without examples and the other is who knows what. I did find some...
1
11523
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 what's wrong. I'm posting my code here. using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using...
0
3752
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 mapped to a serial port. The application I'm writing runs on a Windows Mobile 5 device, so there's no built in printer support and I have to use a...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.