By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,760 Members | 2,113 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,760 IT Pros & Developers. It's quick & easy.

Sending a byte over the serial port

P: n/a
I'm trying to send numbers over the serial port as bytes, not strings.
What I mean is if I want to send a 255, that should be 1 byte, not a 2
followed by a 5 and a 5. I'm trying to convert a borland c++ app.

This is what the c++ app does
int8B = 255;
WriteFile(serialPort,&int8B,1,&bytesWritten,NULL);
int8 = gainSlider->Position;
WriteFile(serialPort,&int8,1,&bytesWritten,NULL);
int8 = offsetSlider->Position;
WriteFile(serialPort,&int8,1,&bytesWritten,NULL);
int8 = exposureSlider->Position;
WriteFile(serialPort,&int8,1,&bytesWritten,NULL);
Here is what I'm doing that does not work
int x = 255;
sp.WriteLine(String.Format("{0}", x));
sp.WriteLine(String.Format("{0}", trackBar_Gain.Value));
sp.WriteLine(String.Format("{0}", trackBar_Offset.Value));
sp.WriteLine(String.Format("{0}", trackBar_Exposure.Value));

How do I send the 255? I can't do sp.WriteLine(255) since it wants a
string.
any help will be greatly appreciated.
Ringo

Feb 6 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a

"Ringo" <ri*********@gmail.comwrote in message
news:11**********************@a75g2000cwd.googlegr oups.com...
I'm trying to send numbers over the serial port as bytes, not strings.
What I mean is if I want to send a 255, that should be 1 byte, not a 2
followed by a 5 and a 5. I'm trying to convert a borland c++ app.

This is what the c++ app does
int8B = 255;
WriteFile(serialPort,&int8B,1,&bytesWritten,NULL);
int8 = gainSlider->Position;
WriteFile(serialPort,&int8,1,&bytesWritten,NULL);
int8 = offsetSlider->Position;
WriteFile(serialPort,&int8,1,&bytesWritten,NULL);
int8 = exposureSlider->Position;
WriteFile(serialPort,&int8,1,&bytesWritten,NULL);
Here is what I'm doing that does not work
int x = 255;
sp.WriteLine(String.Format("{0}", x));
sp.WriteLine(String.Format("{0}", trackBar_Gain.Value));
sp.WriteLine(String.Format("{0}", trackBar_Offset.Value));
sp.WriteLine(String.Format("{0}", trackBar_Exposure.Value));

How do I send the 255? I can't do sp.WriteLine(255) since it wants a
string.
any help will be greatly appreciated.
Isn't there a .Write method that accepts a byte array? That will avoid the
conversion to ASCII text.
Ringo

Feb 7 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.