May 4th, 2012, 06:38 AM
Join Date: May 2012
Time spent in forums: 6 m 31 sec
Reputation Power: 0
9-bit communication issue over serial port
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:
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;
Command = 0x55;
Command = 0x57;
_serialPort.Write(Command, 0, 1);
//_serialPort.Write(Command, 1, 1);
_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);