Click here to Skip to main content
15,880,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I saved the output to a text file and it has some unsual characters.
This is what I am using:
string b = mySerialPort.ReadExisting(); //read a byte




                         //Console.Write(b);
                         string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
                         string image1 = System.IO.Path.Combine(desktop, "Temperature_LOG.txt");

                         using (FileStream fs = new FileStream(image1, FileMode.Append, FileAccess.Write))
                         {
                             using (BinaryWriter bw = new BinaryWriter(fs))
                             {
                                 bw.Write(b);


                                 //bw.Close();
                             }
                         }



0040.0
0020.1 0050.0 0000.0 0100.0 0100.0 0000.0 0010.5 0000.0 0009.0 0000.0 0009.0 0000.0 00
09.0 00000000000000000000000000000000
0040.0 0020.1 0050.0 0000.0 0100.0 0100.0 0000.0 0010.5 0000.0 0009.0 0000.0 0009.0 0000.0 0009.0 00000000000000000000000000000000
0040.0 0020.1 0050.0 0000.0 0100.0 0100.0 0000.0 0010.4 0000.0 0009.0 0000.0 0009.0 0000.0 0009.0 00000000000000000000000000000000
0040.0 0020.1 0050.0 0000.0 0100.0 0100.0 0000.0 0010.4 0000.0 0009.0 0000.0 0009.0 0000.0 0009.0 00000000000000000000000000000000

What I have tried:

I am saving it to a text file but it gives out the above mentioned input
Posted
Updated 5-Dec-19 2:41am
Comments
Richard Deeming 5-Dec-19 8:50am    
We have no idea what data your serial port is receiving, nor why the output data you've shown is "incorrect".

At a guess, you're reading and saving the data as a string, but it's not meant to be a string.
MadMyche 5-Dec-19 12:58pm    
I would check some of those "incorrect" characters and see what the actual ASCII value of them is (hint: read the individual bytes/chars). From there you can determine if those individual characters are even meant to be printed; they may be "non-printable" by design, some examples would be ASCII control characters which are used as delineators

1 solution

This is because you are reading in bytes and not converting to string before you save to file. A good example can be found here, c# - How to save incoming data from serial port to a text file - Stack Overflow[^]
 
Share this answer
 
Comments
Richard Deeming 5-Dec-19 8:48am    
SerialPort.ReadExisting[^] already converts the data to a string.

It's more likely that the data needs to be read and saved as raw bytes instead.

But without knowing what the input data is, or why the output is "incorrect", we're just guessing. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900