Help with SerialPort

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

bruce barker

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

id10t error

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:mad:20,35:ZP08A|003")
count = count + 1
Loop

' Close the port
port.Close()
Return 0
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top