Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The output I am getting after I save it to the text file is:

?????8 00rL????	????
???r??000.j?r???r?0?0000?
?r??L?r?
?????????r????r??L?r?
&????&????
&I??Mr??
?000000000
	????I??
????&????
???r?????r???r??????0020
Lr??L?r?
?)???r??0000.90000.
&r??&?r?
$???r&????


I should be getting something like this:
0020.0 0019.9 0010.0 0000.0 0100.0 0100.0 0000.0 0010.9 0000.0 0002.1 0000.0 0002.1 0000.0 0002.1 00000000000000000000000000000000


This is my code:

public override void DoJob()
        {
            try
            {
                mySerialPort.Open();

                mySerialPort.Write("$01I\r");

                
                //Console.WriteLine("{0} ", CurrentDate);
               // Console.WriteLine("Starting Time: {0}\n", CurrentDate);

                        if (mySerialPort.BytesToRead > 0) //if there is data in the buffer
                        {
                           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();
                                }
                            }



                            //mySerialPort.Close();
                        }


                mySerialPort.Close();

            }
            catch (IOException ex)
            {

                Console.WriteLine(ex);
            }



        }


What I have tried:

I cant seem to find the error, I tried changing the format but no use
Posted
Updated 27-Jan-20 8:08am
v2
Comments
Richard MacCutchan 27-Jan-20 9:40am    
Use your debugger to inspect the actual data that is being returned from the port. It may be that the encoding does not match the .NET standard.

Chances are it's the serial port configuration, ort the data you are receiving isn't in the format you think it should be.
Start with the debugger and look at exactly what you are getting from the device, and check that against your manual

If the manual specifies it should be human readable data rather than binary, use Hyperterminal or similar to work directly to the device and make sure that you can get "real data" before you start with code.
 
Share this answer
 
Comments
Member 14589606 27-Jan-20 8:07am    
I tried with Putty, and I am receiving the right response:
0020.0 0020.2 0050.0 0029.1 0100.0 0100.0 0000.0 0010.9 0000.0 0001.0 0000.0 000

9600 baud rate setting, what could be the problem?
OriginalGriff 27-Jan-20 9:30am    
Check your configuration: Baud rate, BPC, parity, stop bits, ... then use teh debugger to find out exactly what you are receiving.
Quote:
?????8 00r L???? ????
???r??000.j ?r???r?0?0000?
?r??L?r?
?????????r????r??L?r?

This is typical of a wrong setting of serial port.
A serial port have settings and serial communication works only when the settings match on both sides.
Settings are Baud rate (speed), bits, stops ...

You can use the hyper terminal or putty to find the correct settings and debug communication.

[Update]
Quote:
now it is giving the following:
0020.0 0020.3 0050.0 0029.1 0100.0 0100.0 0000.0 0010.9 0000.0 0001.0 0000.0 0001.0 0000.0 0001.0 00000000000000000000000000000000
with some un-usual characters

The unusual chars can be part of the communication, you need to check the exact format of message.
 
Share this answer
 
v3
Comments
Member 14589606 27-Jan-20 8:17am    
now it is giving the following:
0020.0 0020.3 0050.0 0029.1 0100.0 0100.0 0000.0 0010.9 0000.0 0001.0 0000.0 0001.0 0000.0 0001.0 00000000000000000000000000000000

with some un-usual characters
The data you are showing is binary. You are getting this because you are using a BinaryWriter to write the file. Try changing to a TextWriter.
 
Share this answer
 

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