Click here to Skip to main content
15,898,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, was wondering if somebody could give me a hand...

I'm relativly new to c# and I'm having difficulty writing subsequent characters to a com port.
I basically need to send a user-input character followed by a short pause before the char 'x'.
I can send either the input character...OR a char defined in the program but only the first instruction is executed and I'm not sure why. I've tried a few methods but none seem to do the trick...

Here's snippets of my code, I'd really appriciate the help.

Ideally I'd like the program to wait for a char to be entered into buff[0]; send this character to com port; send 'x' char to com port and then wait for the next character to be entered into buff[0]

attempt one...
C#
char[] buff = new char[1];
char[] xbuff = new char[1];

buff[0] = e.KeyChar;
xbuff[0] = 'x';

serialPort1.Write(buff, 0, 1);
System.Threading.Thread.Sleep(500);
serialPort1.Write(xbuff, 0, 1);
System.Array.Clear(buff, 0, 1);


attempt two...

C#
string lettera = ((char)97).ToString();
string letterb = ((char)98).ToString();

serialPort1.Write(lettera);
System.Threading.Thread.Sleep(500);
serialPort1.Write(letterb);


For some reason on both attempts only the first char will be sent. I've been pulling my hair out for the last 2 days and would really be grateful for any ideas

M.
Posted
Updated 5-Jan-11 0:50am
v2

It's possible that the problem comes because you may send data in byte arrays instead of char.

Anyway, this can help you: CLICK
Or this: CLICK
 
Share this answer
 
Comments
Espen Harlinn 5-Jan-11 7:35am    
5+ Good links
Take a look at this article:
Serial Communication using C# and Whidbey[^]

Regards
Espen Harlinn
 
Share this answer
 
Comments
adocas 7-Jan-11 2:37am    
Good link
adopted a completly method but got it sorted...cheers for the reply
 
Share this answer
 

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