Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hey guys,
Feel a little cheeky because I got an answer but the finishing line is in sight and I just have one little hurdle to get over.

I have this code (supplied by mark...legend) to put an 'x' character between chars entered on a string (don't ask why!).

C#
StringBuilder sb = new StringBuilder();
foreach (Char c in textBox1.Text)
{
    sb.Append(c);
    if (!Char.IsWhiteSpace(c))
    {
        sb.Append("x");
        serialPort1.Write(sb.ToString());
    }
}
textBox2.Text = sb.ToString();



All is left to do is to send these characters to a com port individually. So far I can send a single char from a keypress or stored variable, but i need some kind of loop to send the above string (sb) one by one.
I've tried so many methods, and can list them, but I don't seem to be having any success.

M
Posted
Updated 12-Jan-11 9:28am
v5

1 solution

This is one way:

C#
string x = sb.ToString();
foreach (char ch in x)
{
    seriealPort1.Write(ch);
}


Or, to use your sample:

[EDIT]
C#
StringBuilder sb = new StringBuilder();
foreach (Char c in textBox1.Text)
{
    sb.Append(c);
    serialPort1.Write(c.ToString());

    if (!Char.IsWhiteSpace(c))
    {
        sb.Append("x");
        serialPort1.Write("x");
    }
}
textBox2.Text = sb.ToString();

[/EDIT]
 
Share this answer
 
v3
Comments
martyn lucas 12-Jan-11 15:42pm    
Hi John, thanks for your reply. both of the write port lines come up with "cannot convert char to string" and "invalid argument" errors, which i've really been struggling with when trying to send a string. Would you have any suggestions of how i could get round this?
JOAT-MON 12-Jan-11 15:56pm    
@Martyn - try to use casting or the character's .ToString() method.
martyn lucas 12-Jan-11 15:59pm    
yeah sorry, that really wasn't a problem, just me being a bit dizzy and not reading the code. The program compiles but i still can't seem to see any output on the com port, The text is displayed on the other text box so the fucntion is working...i'm really stumped
JOAT-MON 12-Jan-11 16:42pm    
@Martyn - assuming that John's code snippet is being used relatively unmodified in your code, it looks like the serialPort1 only writes out an 'x' if it is a character in your string. Try the following: serialPort1.Write(c.ToString() + "x");

Sorry. Forgot you were limited to sending one character at a time. I will edit John's code snippet above.
martyn lucas 13-Jan-11 16:35pm    
Hey guys, sorry i'm late but just had so much other work on...i really am at my wits end with this problem...its the same thing, the stringbuilder works brilliantly and displays it in the textbox, but noyhing is written to the com port. Could I try and use the stringbuilder to also include a space between characters, use the split function to put it into an array and then send it that way?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900