Main Site Documentation

Help me to send real number to PC via RS232


#1

hello
I have actual data I want to send to PC via RS232
to read with Matlab
for example:
/ / Convert the string to bytes is:

byte [] buffer = Encoding.UTF8.GetBytes (counter_string);

but
is that there is a command in C sharp to converte real number to type bytes ?
thanks


#2

Hi Riad1986,

This is a proc i use to send strings:


        static void RS232_Write(string _line)
        {
            if (RS232_On == 1 && RS232_Err == 0)
            {
                try
                {
                    byte[] _output = new byte[_line.Length + 1];

                    //loop door alle tekens
                    for (int Counter = 0; Counter < _line.Length; ++Counter)
                    {
                        _output[Counter] = (byte)_line[Counter];
                    }

                    //toevoegen end marker
                    _output[_line.Length] = (byte)RS232_Endbyte;

                    //and write
                    RS232.Write(_output, 0, _output.Length);
                }
                catch
                {
                    Debug.Print("***** RS232 Write Error ");
                }
            }
        }


#3

hello,
and thank you for the help
after the syntax of the program output that are sent are of type Byte.
can I send data de type Real (float)?
I used the following the syntax program:
using System.IO.Ports;
using System.Text;
using System.Threading;

namespace change_this_to_your_namespace
{
public class Program
{
public static void Main()
{
SerialPort UART = new SerialPort(“COM1”, 115200);
int counter = 0;
UART.Open();
while (true)
{
// create a string
string counter_string = “Count: " + counter.ToString() +
”\r\n";
// convert the string to bytes
byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
// send the bytes on the serial port
UART.Write(buffer, 0, buffer.Length);
// increment the counter;
counter++;
//wait…
Thread.Sleep(100);
}
}
}
}


#4

hello,
and thank you for the help
after the syntax of the program output that are sent are of type Byte.
can I send data de type Real (float)?
I used the following the syntax program:
using System.IO.Ports;
using System.Text;
using System.Threading;

namespace change_this_to_your_namespace
{
public class Program
{
public static void Main()
{
SerialPort UART = new SerialPort(“COM1”, 115200);
int counter = 0;
UART.Open();
while (true)
{
// create a string
string counter_string = “Count: " + counter.ToString() +
”\r\n";
// convert the string to bytes
byte[] buffer = Encoding.UTF8.GetBytes(counter_string);
// send the bytes on the serial port
UART.Write(buffer, 0, buffer.Length);
// increment the counter;
counter++;
//wait…
Thread.Sleep(100);
}
}
}
}


#5

sorry


#6

@ riad1986 -

Not math wiz but I would use…

Microsoft sample for you to look at
http://msdn.microsoft.com/en-us/library/c309e6c9(v=vs.110).aspx

Sorry but link is bad…

Microsoft code


public void ConvertDoubleString(double doubleVal) {
			
			string	stringVal;     

			// A conversion from Double to string cannot overflow.       
			stringVal = System.Convert.ToString(doubleVal);
			System.Console.WriteLine("{0} as a string is: {1}",
				doubleVal, stringVal);

			try {
				doubleVal = System.Convert.ToDouble(stringVal);
				System.Console.WriteLine("{0} as a double is: {1}",
					stringVal, doubleVal);
			} 
			catch (System.OverflowException) {
				System.Console.WriteLine(
					"Conversion from string-to-double overflowed.");
			}
			catch (System.FormatException) {
				System.Console.WriteLine(
					"The string was not formatted as a double.");
			}
			catch (System.ArgumentException) {
				System.Console.WriteLine(
					"The string pointed to null.");
			}
		}



I think you really wanted this?

http://msdn.microsoft.com/en-us/library/ds4kkd55(v=vs.110).aspx

Paste link into explorer click link does not work


#7

@ riad1986 - To format code, select the code and then press the 101/010 above the message box.


#8

thanks


#9

If you are on the Primium hardware you can use this method:

https://www.ghielectronics.com/downloads/man/Library_Documentation_v4.2/Premium/html/ae8d338b-5528-88e0-129c-a8d18e47bb1c.htm


public static void InsertValueIntoArray(
	float f,
	byte[] buffer,
	int offset
)