Convert string to char textbox

I need to send the data to the serial port in Char I have tried

textbox1.Text[0]

Convert.Tochar(textbox1.Text);

so basically serialPort1.WriteLine(textBox1.Text); must be sent in char

private void button1_Click(object sender, EventArgs e)
{
    textBox2.Text = textBox1.Text;
    serialPort1.WriteLine(textBox1.Text);
    textBox1.Text = "";
}

Upvotes: 1

Views: 962

Answers (2)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112324

The SerialPort.WriteLine method in the System.IO.Ports namespace is declared as

public void WriteLine (string str);

So I'm not sure why you think you need to pass it a char. Maybe it helps to set the encoding

serialPort.Encoding = Encoding.ASCII;

Upvotes: 3

ChaosPandion
ChaosPandion

Reputation: 78262

Try something like this:

serialPort1.Write(new [] { textBox1.Text == "1" ? '1' : '0' }, 0, 1);

Upvotes: 0

Related Questions