-
February 28th, 2011, 06:51 PM
#1
Send Hex data to Serial Port :-)
Hello C# Guro,
Im am new the C# and previously my world is in C++ and C, I have created a lot of program in C++ and now want to start the new phase of my programming career.
Is it possible to send a data in Serial Port using C#
Lets say i want the sex the 0x5A or 0x2B in the serial port..
How can i do that in C #?
Thanks.
Mico
-
February 28th, 2011, 07:18 PM
#2
Re: Send Hex data to Serial Port :-)
You want to send sex to the serial port?
Working with serial ports in C# is a breeze...
Code:
using (SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8))
{
byte[] bytesToSend = new byte[2] { 0x5A, 0x2B };
port.Open();
port.Write(bytesToSend, 0, 2);
}
It doesn't get much easier.
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
February 28th, 2011, 08:27 PM
#3
Re: Send Hex data to Serial Port :-)
Thank you very much for this one,.. I will try this and give a feedback once it works...
-
March 1st, 2011, 01:17 AM
#4
Re: Send Hex data to Serial Port :-)
Hello, I tried already the function but It didn't work
Actually what I want to do is to send a trigger character to A
barcode reader.. the the trigger for the barcode reader is 0x2B.
But this wont work...,
Is there any trick that might work on my problem..?
-
March 1st, 2011, 10:16 AM
#5
Re: Send Hex data to Serial Port :-)
Just adjust the byte[] so it only contains 1 hex value and then tell the SerialPort.Write method to send 1 character:
Code:
using (SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8))
{
byte[] bytesToSend = new byte[1] { 0x2B };
port.Open();
port.Write(bytesToSend, 0, 1);
}
R.I.P. 3.5" Floppy Drives
"I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein
-
March 1st, 2011, 08:20 PM
#6
Re: Send Hex data to Serial Port :-)
Yeah, using the serial port in C# is very convenient. From experience in debugging this sort of thing, you can test just the hardware by using HyperTerminal (usually comes Standard on Windows; Start > Programs > Accessories > Comunication) which will let you write text directly to the serial port. 0x2B is + in ASCII (link).
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|