6 Replies - 4693 Views - Last Post: 11 June 2009 - 12:59 PM Rate Topic: -----

#1 m__n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 09

Sending a string of characters to com port

Posted 11 June 2009 - 12:12 AM

If anyone can help me , I'll be thankful really....

In my program I must send a string of characters to the com port
( in fact the blue tooth module is being connected to the usb port of pc , and the system will define a com port for it , so I'm supposed to use the code for sending data to the COM PORT... )
In my code (as you see) I've used this command line :
" serialPort.Write(str, 0, str.Length); "
I know this command is for sending a data in "Byte type"( not characters !!) So The problem is that :
I DON'T KNOW what is the appropriate command For sending a "string of CHARACTERS " to the com port ??!!

Here is my code :

private void btncod_Click(object sender, EventArgs e)
{
   char[] str = new char[5];
   str[0] = 12;
   str[1] = 24;
   str[2] = 102;
   str[3] = 215;
   str[4] = 2;
   string portName = "COM3";
   int buadRate = 9600;
   SerialPort serialPort = new SerialPort(portName, buadRate);
   serialPort.DtrEnable = true;
   serialPort.RtsEnable = true;
   serialPort.Handshake = Handshake.None;
   serialPort.Parity = Parity.None;
   serialPort.StopBits = StopBits.One;
   serialPort.Open();
   serialPort.Write(str, 0, str.Length);
   serialPort.Close();
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Sending a string of characters to com port

#2 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Sending a string of characters to com port

Posted 11 June 2009 - 04:35 AM

The .Write method of a serial port has 3 overloads.

Write(String)
Write(Byte[], Int32, Int32)
Write(Char[], Int32, Int32)

You can look further on SerialPort.Write Method @ MSDN.

Thus your code should work fine, what's the issue with your code? Compile error or not working?
Was This Post Helpful? 1
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Sending a string of characters to com port

Posted 11 June 2009 - 05:13 AM

here is a snippet on how to write and read from a SerialPort(COM port).

http://www.dreaminco...snippet2764.htm
Was This Post Helpful? 0
  • +
  • -

#4 m__n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 09

Re: Sending a string of characters to com port

Posted 11 June 2009 - 12:27 PM

View PostMageUK, on 11 Jun, 2009 - 03:35 AM, said:

The .Write method of a serial port has 3 overloads.

Write(String)
Write(Byte[], Int32, Int32)
Write(Char[], Int32, Int32)

You can look further on SerialPort.Write Method @ MSDN.

Thus your code should work fine, what's the issue with your code? Compile error or not working?


Thanks for your reply ;
the problem is that there is a compiling error which says that a number like ,for example, 12 or 215,... can't convert to char type !!
in my code i considered that integer numbers can be defined as characters because I knew that character type does contain numbers !
so how can we send a string of integers to com port via these three methods ??!
Was This Post Helpful? 0
  • +
  • -

#5 m__n  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-June 09

Re: Sending a string of characters to com port

Posted 11 June 2009 - 12:40 PM

View Posteclipsed4utoo, on 11 Jun, 2009 - 04:13 AM, said:

here is a snippet on how to write and read from a SerialPort(COM port).

http://www.dreaminco...snippet2764.htm



thanks for your reply;
But I'm not familier with "try" & "catch " , so I prefer to use the simple commands as " serialPort.Write..." !! but there is an error that it can't convert numbers(eg 123 ) to char type !!
any way ... thanks !
Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1003
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Sending a string of characters to com port

Posted 11 June 2009 - 12:48 PM

View Postm__n, on 11 Jun, 2009 - 11:27 AM, said:

Thanks for your reply ;
the problem is that there is a compiling error which says that a number like ,for example, 12 or 215,... can't convert to char type !!
in my code i considered that integer numbers can be defined as characters because I knew that character type does contain numbers !
so how can we send a string of integers to com port via these three methods ??!

Well you could just do .Write("1234567890"); if you wanted :)

With regards to your other post, try/catch are language constructs and you should really learn them, they'll help you more than anything ;)
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Sending a string of characters to com port

Posted 11 June 2009 - 12:59 PM

View Postm__n, on 11 Jun, 2009 - 02:40 PM, said:

View Posteclipsed4utoo, on 11 Jun, 2009 - 04:13 AM, said:

here is a snippet on how to write and read from a SerialPort(COM port).

http://www.dreaminco...snippet2764.htm



thanks for your reply;
But I'm not familier with "try" & "catch " , so I prefer to use the simple commands as " serialPort.Write..." !! but there is an error that it can't convert numbers(eg 123 ) to char type !!
any way ... thanks !


Try/Catch has nothing to do with writing to a serial port. I would suggest you learn how to use Try/Catch.

http://msdn.microsof...989(VS.71).aspx

as for the error. Maybe you need to learn what a Char is?

You aren't required to uses chars to write to a serial port, as noted by two other posts. You can write that number as a string to the serial port. No real need to try to use it as a char.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1