Hi

I am battling for days now to send and receive the correct characters to hardware from my code. It works fine in tera term.

I have to send ASCII 4 " alt 004" in tera Term to poll the datalogger to return the data stream saved in memory. The hardware works fine and I get the correct data, but I can't seem to get the harware polled from my software. If I do a manual trigger on the hardware it sends the data, but it shows in some binary format. I have tried several ways to decode an encode, but it just seems to slip out of my reach.
Help will be greatly appreciated.

private void SendData()
        {  
         if (serialPort1.IsOpen)
              {  
         try 
              {  
                 serialPort1.WriteLine(pollPur);  
                 MessageBox.Show("Command Issued: " + pollPur);
             
             } 
                catch 
               { }  
         }  
     }  

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
         {           
            
             {
                 try
                 {
                     string PortData = serialPort1.ReadExisting();

                 }
                 catch (TimeoutException)
                 {
                     return; // Data not ready yet
                 }
             }

 private void btnOpenPort_Click_1(object sender, EventArgs e)
        {
            // If the port is open, close it.
            if (serialPort1.IsOpen) serialPort1.Close();
            else
            {
                // Set the port's settings
                serialPort1.BaudRate = int.Parse(cmbBaudRate.Text);
                serialPort1.DataBits = int.Parse(cmbDataBits.Text);
                serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbStopBits.Text);
                serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text);
                serialPort1.PortName = cmbPortName.Text;
                serialPort1.ReadTimeout = 500;
                serialPort1.WriteTimeout = 500;
               
                // Open the port
                serialPort1.Open();                
                SendData();
                
               textBox10.Text = serialPort1.ReadLine();

                     
                                

            }

            if (serialPort1.IsOpen) 
            {
                btnOpenPort.BackColor = Color.LimeGreen;
            }
             else 
            {             
            btnOpenPort.BackColor = Color.Red;
            }
            if (serialPort1.IsOpen) toolStripMenuItem7.Text = "&Disconnect";
            else toolStripMenuItem7.Text = "&Connect";
            
        }

So what is the problem here? You can't read the data received, or you cant figure out how to encode \004 properly?

I first have to send the ascii 4. I send it as "string pollPur = "4";
so yes I dont think I decode it right. Will try quickly "/004"

tried it, sofware hangs and I get a timeout.
yes I also need to decode the data that I receive in a readable format.

This is how it should look, if received correctly
01 1 2 1 0340 15 15 04 07 09 20 09 0004 0010 0000 0362 0362 0600 0000 0600 0000 0418 0362 0418 0362 0418 0362 0418 0362 0418 0362 0418 0203 0413 0500

Write the byte[] array of data received to a text file and upload it, please specify the encoding as well.

Here is how you would get the ascii encoded character:

string s = ASCIIEncoding.ASCII.GetString(new byte[] { 4 });

I have used your code to poll the hardware, but still do not get any response from the microcontroller.

Can you please elaborate on write the byte [] array of data to a text file, maybe provide sample code.
Sorry I am not very familiar with C# yet. This is my first project and I think it's probably to advanced for a first try

The data that is coming from the serial port is ascii so I need to decode it to text

At this point:

1. Can you send data across the serial port?
2. Does the device receive the data you send across the serial port?
3. Does the device respond to command sent?
4. Are you receiving the data from the response?
5. How are you receiving the response? In an event, trying to read the buffer?
6. Can you write the byte[] buffer of the response to a text file and upload it?

string PortData = serialPort1.ReadExisting();

You probably want to pick another method to read data off the serial port -- not using strings

1. I am not sure, I definately connect, because I receive data when I manually trigger the hardware. I think that I am not sending the correct ascii 4 char. I have tried several terminal software and coul only get tera term and hyper terminal to work with \004

2. yes but only manually triggered on the hardware

3.Doesn't seem to respond to the command sent, only in tera term

4. no, but yes in tera term

5. only ReadLine() seems to work when I manually ReadExisting() does nothing

6. Manually trigged data that I receive in the attached file

when I send the ascii \004, the character It shows in the messagebox is a 90 degree rotated NOT SIGN "\u00AC"
That I cannot find in the ascii table

Sometimes manufacturers pick control characters that are not found to be sure they don't have a conflict with any existing structures -- you said you wanted to send a character 4, i assumed 4 was the byte's value?, and you sent it -- but it is not correct?

I am developing the software for the manufacturer and he needs a \004 to trigger an event in the microcontroller.
If I press alt 004 in tera term and hyperterminal it works. I need to emulate that same sequence or character or byte to beable to communicate with the device. If you enter a "Alt 004" at the cursor on these 2 terminal programs, what is sent to the serialport?
BTW I am using VS2010 beta on Vista, could that also be a factor?

Thanks I changed ASCIIEncoding to UTF8Encoding now it works perfectly. I used a serial port monitor to capture the returned data. Exactly as it should be. Problem now, I can capture the data on the port monitor, but where is it going in my code. I need to read it into an array and then acess it to draw a graph. Any Ideas on how and where I should code it?

Hello,

I am just wondering if you are still having problems with the serial port. If so, I developed a serial port programming language in C# and I believe it solves nearly all of the problems that everybody encounters with.

Would you please take a look and try it ?

For example; I think you want to send <EOT> control character to the serial port. You can do it easily with this application.

state Init
    send("<EOT>");
  end state

Project is freely available on sourceforge and if you have any questions, please feel free to ask.

Project homepage
Download Link

Thank you,

Orhan

albay,

Please do not resurrect threads that are years old. By doing so you run the risk of confusing current posters.Have a look at forum rules.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.