none
Sending string and hex command to serial port. RRS feed

  • Question

  • Hi everyone, 

    I'm having some trouble sending a string and hex command to a serial port using C#. I need to send a string to a display and then save that string with a hex command. The hex command to change the start up command is FE 40. Although, I am able to successfully print the string on the display, I don't know how to save it. When starting the display that string should show up on the start-up screen. Should I convert the string to hex before sending it with the hex command? I'm new to C# so any help would be appreciated. This is what I have so far. 

    // write to screen
            private void btnWrite_Click(object sender, EventArgs e)
            {
    
                string str = tbLine1.Text + tbLine2.Text + tbLine3.Text + tbLine4.Text;
                byte[] splash_str = Encoding.ASCII.GetBytes(str);
                byte[] change_screen = new byte[2] { 0xFE, 0x40 };
    
                try
                {
                    sp.Write(splash_str, 0, splash_str.Length);
                    //sp.Write(str);
                    sp.Write(change_screen, 0, change_screen.Length);
                }
                catch (Exception E) { MessageBox.Show(E.ToString(), "Error"); }
            }


    Monday, February 11, 2019 3:02 PM

All replies

  • I'm unclear as to where things aren't working for you. You keep saying "save" but I assume you don't mean save the string in your code but on the serial device. I'm not sure what the FE40 has to do with this. Is that the command to "save" on the device?

    You said the string shows up so does that mean that when you call Write with the string it is getting to the device properly? So the issue is that your FE40 isn't getting there? Do you know whether this device requires big endian or little? Do you maybe need to send the integral value FE40 and not the byte value?

    var command = 0xFE40;
    
    var change_screen = BitConverter.GetBytes(command);
    sp.Write(change_screen, 0, change_screen.Length);

    SerialPort has a Write method that takes a string, is there any reason you're manually doing the conversion?

    Extension methods may make this code easier to work with and can help isolate your code from the details. If the device is more complex then creating a separate type to wrap the port may be a better route as well. Here's an example of both approaches (not tested).

    //Extensions to make it easier to work with SerialPort
    public static class SerialPortExtensions
    {
        public static void Write ( this SerialPort source, int value )
        {
            var data = BitConverter.GetBytes(value);
    
            source.Write(data, 0, data.Length);
        }
    }
    
    //Custom class to work with a specific device
    public class MyDevice : IDisposable
    {
        //TODO: Expand this to allow you to configure your device such as parity, speed, etc
        public MyDevice ( string portName ) : this(new SerialPort(portName))
        { }
    
        public MyDevice ( SerialPort port )
        {
            _port = port;
        }
    
        public void Open ()
        {
            if (!_port.IsOpen)
                _port.Open();
        }
    
        //Put your custom device commands here
        public void SetMessage ( string message )
        {
            //This should work directly, if not then use your Encoding code you had
            _port.Write(message);
        }
    
        public void SetStartupMessage ( string message )
        {
            SetMessage(message);
    
            //Assuming you need to send an int and not 2 byte array
            _port.Write(0xFE40);
        }
    
        //IDisposable stuff so things get cleaned up
        public void Dispose ()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }                
    
        protected virtual void Dispose ( bool disposing )
        {
            if (disposing)
            {
                _port?.Dispose();
                _port = null;
            };
        }
    
        private SerialPort _port;
    }
    
    //Example usage
    class Program
    {
        static void Main ( string[] args )
        {
            using (var device = new MyDevice("COM1"))
            {
                device.SetStartupMessage("Hello");
            };
        }
    }
    


    Michael Taylor http://www.michaeltaylorp3.net

    Monday, February 11, 2019 3:46 PM
    Moderator
  • Yes, sorry. By save I mean saving the string in the serial device. And, FE40 is the command to change the display start-up screen. I'm programming a matrix orbital liquid crystal display. The string does print on the display, but when I restart the display the message is not saved. 

    I looked through the manual for the crystal display but found nothing about whether it's big or little endian. Thank-you for replying, I'm going to try and implement your solution. 

    Monday, February 11, 2019 4:12 PM
  • Hi espro88,

    Thank you for posting here.

    >>but when I restart the display the message is not saved.

    For your question, do you mean when you restart the app, the message does not been saved? 

    If yes, you could create a string in Resources to save it.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, February 12, 2019 5:50 AM
    Moderator