views:

123

answers:

2

Hi,

I need to send a write method as a string in C# to a com port. It should be enclosed btween stx and etx.

e.g. serialPort1.write("02TY000D03") - where the 02 and 03 are stx and etx.

Could someone provide a quick example please as I don't think the code above is correct?

Many thanks

Darren.

+1  A: 

You need to use escape codes;

serialPort1.Write("\x02TY000D\x03");
SLaks
+1  A: 

Here is a sample I took from here:

// This is a new namespace in .NET 2.0
// that contains the SerialPort class 
using System.IO.Ports; 

private static void SendSampleData() { 
    // Instantiate the communications
    // port with some basic settings 
    SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); 

    // Open the port for 
    communications port.Open(); 

    // Write a string 
    port.Write("Hello World"); 

    // Write a set of 
    bytes port.Write(new byte[] {0x0A, 0xE2, 0xFF}, 0, 3); 

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

Therefore you may be better using Write insted of write, note the capital W. If 02 and 03 are byte values you will have to escape the values with \x, i.e. \x03;

Obalix