#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    1
    Rep Power
    0

    9-bit communication issue over serial port


    Hi All,

    I am new to .net. I have to write a code to communicate over a USB to RS485 converter with an Embedded device.
    It requires 9-bit data (8-bit data + 1-bit to indicate Address/Data) and Baud rate of 100 kbps.

    To get 9-bit data, we are using 8-bit with parity configuration. I am using parity.MARK to send 1 and parity.SPACE to send 0. I am able to send the 9-bit data correctly if the parity bit is set while Port open. But if I want to change the parity bit on-the-fly, then some extra bits are send along with 9-bit data. This is causing a lot of issues while communicating.

    Is it possible to change the parity bit after the port is opened?

    Find below the code snippet:
    Code:
            private void OpenSerialPorts()
            {
                ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from WIN32_PnPEntity");
    
                int iNumDevices = 0;
                foreach (ManagementObject Port in searcher.Get())
                {
                    string a = (string)Port.GetPropertyValue("Name");
    
                    if (a.IndexOf("USB") > -1 && a.Contains("COM"))
                    {
                        iNumDevices = iNumDevices + 1;
    
                        int iPos1 = a.IndexOf("(") + 1;
                        int iPos2 = a.IndexOf(")");
                        string strCOMPort = a.Substring(iPos1, (iPos2 - iPos1));
    
                        SerialPort _serialPort = new SerialPort(strCOMPort, 9600, Parity.Space, 8, StopBits.One);
    
                        _serialPort.Open();				// Opened the port with parity as Space
    
                        Byte[] Command = new Byte[2];
                        Command[0] = 0x55;
                        Command[1] = 0x57;
    
                        if (_serialPort.IsOpen)
                        {
                            _serialPort.Write(Command, 0, 1);		 
                            //_serialPort.Write(Command, 1, 1);
                        }
                        Thread.Sleep(200);
                        
                        _serialPort.Close();
                        _serialPort = null;
    
                        SerialPort _serialport1 = new SerialPort(strCOMPort, 9600, Parity.Mark, 8, StopBits.One);
                        _serialport1.Open();	// Opened the port with parity as Mark
    
                        _serialport1.Write(Command, 1, 1);
                        _serialport1.Close();
                    }
                }
            }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2007
    Location
    Charlotte
    Posts
    412
    Rep Power
    144
    Dude... I don't think it works that way.

    Serial ports are designed to transmit 8 bits at a time (serially). The PARITY bit (set to odd or even) is used as a type of error checking. It does not contain data. The port itself sets that, I believe.

    You'd have to write your own serial *driver* to do it differently. That is to manipulate the hardware at the hardware level. If you're using anything by the OS, it's not going to let you do that.

IMN logo majestic logo threadwatch logo seochat tools logo