3 Replies - 13689 Views - Last Post: 19 January 2010 - 06:33 AM Rate Topic: -----

#1 lord0m3ga78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-November 09

Writing to textbox from data received through serialport

Posted 19 January 2010 - 12:43 AM

I'm having problems in displaying the bytes sent from one port to another. Please help...

CODE THAT SENDS A 0X01 BYTE TO ANOTHER SERIAL PORT.

 public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
			//configuring the serial port
			serialPort1.PortName = "COM1";
			serialPort1.BaudRate = 115200;
			serialPort1.DataBits = 8;
			serialPort1.Parity = Parity.None;
			serialPort1.StopBits = StopBits.One;
			serialPort1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
		}

		private void Test_button_Click(object sender, EventArgs e)
		{
			//opening the serial port
			serialPort1.Open();

			serialPort1.Write(new byte[] { 0x01 },0,1);

			//close the port
			serialPort1.Close();
		}	 
	}



CODE FROM THE RECEIVING SERIAL PORT.

public Form1()
		{
			InitializeComponent();
			//configuring the serial port
			serialPort1.PortName = "COM2";
			serialPort1.BaudRate = 115200;
			serialPort1.DataBits = 8;
			serialPort1.Parity = Parity.None;
			serialPort1.StopBits = StopBits.One;
			serialPort1.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
			serialPort1.Open();
		}

		private delegate void SetTextDeleg(string text);

		void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
		{
			string data = serialPort1.ReadExisting();	
			// Invokes the delegate on the UI thread, and sends the data that was received to the invoked method.
			// ---- The "si_DataReceived" method will be executed on the UI thread which allows populating of the textbox.
			this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
		}

		private void si_DataReceived(string data) { textBox1.Text = data.Trim(); }
		}




the receiving port has a textbox in the form. The problem is when i click send, COM1(sending port) sends the 0x01 to COM2(receiving port), the data being displayed in the form is not 0x01 but "[]" (two brackets). when i change the " serialPort1.Write(new byte[] { 0x01 },0,1); " in the first code to " serialPort1.Write("MARTIN"); " MARTIN would be displayed in the textbox.. please help... thank you!!!!

This post has been edited by lord0m3ga78: 19 January 2010 - 12:44 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Writing to textbox from data received through serialport

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,605
  • Joined: 23-August 08

Re: Writing to textbox from data received through serialport

Posted 19 January 2010 - 05:54 AM

A TextBox displays text. The byte value 0x1 is a non-printable character, and therefore cannot be displayed.
Was This Post Helpful? 0
  • +
  • -

#3 lord0m3ga78  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 30-November 09

Re: Writing to textbox from data received through serialport

Posted 19 January 2010 - 06:21 AM

View PostJackOfAllTrades, on 19 Jan, 2010 - 04:54 AM, said:

A TextBox displays text. The byte value 0x1 is a non-printable character, and therefore cannot be displayed.


what can i do to display such as that, 0x01 or 01 only, to be displayed in the textbox? thank you for your reply
Was This Post Helpful? 0
  • +
  • -

#4 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Writing to textbox from data received through serialport

Posted 19 January 2010 - 06:33 AM

View Postlord0m3ga78, on 19 Jan, 2010 - 09:21 AM, said:

View PostJackOfAllTrades, on 19 Jan, 2010 - 04:54 AM, said:

A TextBox displays text. The byte value 0x1 is a non-printable character, and therefore cannot be displayed.


what can i do to display such as that, 0x01 or 01 only, to be displayed in the textbox? thank you for your reply


you need to convert it from a hex code to a string.

string test = "0x6D";

int value = Convert.ToInt32(test, 16);
string stringValue = Char.ConvertFromUtf32(value);

Console.WriteLine("The hex code {0} is converted to {1}", test, stringValue);
Console.Read();



This code was taken from the MSDN...

http://msdn.microsof...y/bb311038.aspx
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1