Send correct ascii to serialport

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Send correct ascii to serialport

 
0
  #1
Sep 7th, 2009
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.
  1. private void SendData()
  2. {
  3. if (serialPort1.IsOpen)
  4. {
  5. try
  6. {
  7. serialPort1.WriteLine(pollPur);
  8. MessageBox.Show("Command Issued: " + pollPur);
  9.  
  10. }
  11. catch
  12. { }
  13. }
  14. }
  15.  
  16. private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
  17. {
  18.  
  19. {
  20. try
  21. {
  22. string PortData = serialPort1.ReadExisting();
  23.  
  24. }
  25. catch (TimeoutException)
  26. {
  27. return; // Data not ready yet
  28. }
  29. }
  30.  
  31. private void btnOpenPort_Click_1(object sender, EventArgs e)
  32. {
  33. // If the port is open, close it.
  34. if (serialPort1.IsOpen) serialPort1.Close();
  35. else
  36. {
  37. // Set the port's settings
  38. serialPort1.BaudRate = int.Parse(cmbBaudRate.Text);
  39. serialPort1.DataBits = int.Parse(cmbDataBits.Text);
  40. serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmbStopBits.Text);
  41. serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), cmbParity.Text);
  42. serialPort1.PortName = cmbPortName.Text;
  43. serialPort1.ReadTimeout = 500;
  44. serialPort1.WriteTimeout = 500;
  45.  
  46. // Open the port
  47. serialPort1.Open();
  48. SendData();
  49.  
  50. textBox10.Text = serialPort1.ReadLine();
  51.  
  52.  
  53.  
  54.  
  55. }
  56.  
  57. if (serialPort1.IsOpen)
  58. {
  59. btnOpenPort.BackColor = Color.LimeGreen;
  60. }
  61. else
  62. {
  63. btnOpenPort.BackColor = Color.Red;
  64. }
  65. if (serialPort1.IsOpen) toolStripMenuItem7.Text = "&Disconnect";
  66. else toolStripMenuItem7.Text = "&Connect";
  67.  
  68. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Send correct ascii to serialport

 
0
  #2
Sep 7th, 2009
So what is the problem here? You can't read the data received, or you cant figure out how to encode \004 properly?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Re: Send correct ascii to serialport

 
0
  #3
Sep 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Send correct ascii to serialport

 
0
  #4
Sep 7th, 2009
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:
  1. string s = ASCIIEncoding.ASCII.GetString(new byte[] { 4 });
Last edited by sknake; Sep 7th, 2009 at 9:47 am.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Re: Send correct ascii to serialport

 
0
  #5
Sep 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Re: Send correct ascii to serialport

 
0
  #6
Sep 7th, 2009
The data that is coming from the serial port is ascii so I need to decode it to text
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Send correct ascii to serialport

 
0
  #7
Sep 7th, 2009
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?

  1. string PortData = serialPort1.ReadExisting();

You probably want to pick another method to read data off the serial port -- not using strings
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Re: Send correct ascii to serialport

 
0
  #8
Sep 7th, 2009
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
Attached Files
File Type: txt captured data.txt (85 Bytes, 1 views)
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 40
Reputation: procomp65 is an unknown quantity at this point 
Solved Threads: 0
procomp65 procomp65 is offline Offline
Light Poster

Re: Send correct ascii to serialport

 
0
  #9
Sep 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,464
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 629
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Send correct ascii to serialport

 
0
  #10
Sep 7th, 2009
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?
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

Tags
communication

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1029 | Replies: 12
Thread Tools Search this Thread



Tag cloud for communication
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC