CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Oct 2010
    Posts
    44

    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

  2. #2
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    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

  3. #3
    Join Date
    Oct 2010
    Posts
    44

    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...

  4. #4
    Join Date
    Oct 2010
    Posts
    44

    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..?

  5. #5
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    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

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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
  •  





Click Here to Expand Forum to Full Width

Featured