Help with SerialPort

  • Thread starter Thread starter Benedictum
  • Start date Start date
B

Benedictum

I have the following method that initializes the port. I am having problems
assigning values to the Serial port properties. Can someone help ? Here is
the snippet with my comments:

private void btnOpenPort_Click(object sender, EventArgs e)

{

if (ServoPort.IsOpen)

{

ServoPort.Close();

btnOpenPort.Text = "Open Port";

}

else

{

// SerialPort parameters

ServoPort.PortName = tbxSPortName.Text; // This is OK

ServoPort.BaudRate = int.Parse(tbxSBaud.Text); // This is OK

ServoPort.Parity = int.Parse(tbxSParity.Text); // ERROR!

ServoPort.DataBits = int.Parse(tbxSDatabits.Text); // This is OK

ServoPort.StopBits = int.Parse(tbxSStopbit.Text); //ERROR!

btnOpenPort.Text = "Close Port";

}

}

Any other ideas are welcome.
 
parity and stopbits are enums not ints. be sure you have the correct values.
also be sure that ServoPort is a static (shared across page requests). also
you should implement locking so two page requests do not access the port at
the same time.

if you are trying to control more than one server serial port, then keep a
collection of port managers and use the name as a key.

-- bruce (sqlwork.com)
 
I have the following method that initializes the port. I am having problems
assigning values to the Serial port properties. Can someone help ? Here is
the snippet with my comments:

private void btnOpenPort_Click(object sender, EventArgs e)

{

if (ServoPort.IsOpen)

{

ServoPort.Close();

btnOpenPort.Text = "Open Port";

}

else

{

// SerialPort parameters

ServoPort.PortName = tbxSPortName.Text; // This is OK

ServoPort.BaudRate = int.Parse(tbxSBaud.Text); // This is OK

ServoPort.Parity = int.Parse(tbxSParity.Text); // ERROR!

ServoPort.DataBits = int.Parse(tbxSDatabits.Text); // This is OK

ServoPort.StopBits = int.Parse(tbxSStopbit.Text); //ERROR!

btnOpenPort.Text = "Close Port";

}
}

Any other ideas are welcome.

Here is what I used in vb. I am not sure if it will help

Function SendSampleData(ByVal copies As String)
'Instantiate the communications port with some basic settings
Dim count As String = 0
Dim port As SerialPort
port = New SerialPort("COM1", 9600, Parity.None, 8,
StopBits.One)

' Open the port for communications
port.Open()
Do Until count = copies
' Write a string
port.Write("EZ{PRINT,STOP350:@20,35:ZP08A|003")
count = count + 1
Loop

' Close the port
port.Close()
Return 0
 
Back
Top