serail port readingshowing null error

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<span style="font-family:Times New Roman;font-size:small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small <font size=2 color="#0000ff <font size=2 color="#0000ff
HAI MY FRIENDS WISH ALL OF A HAPPY NEW YEAR<br/><br/>i have some piece of code for serail port reading in c#<br/><br/>it showing null error where i sacan barcode in serail port can anybody check this ?<br/>===========================<br/><br/><br/><br/>public
</font></font><font size=2 color="#0000ff

</font>
<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small partial<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small class<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Form1<span style="font-size:x-small : <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Form
<span style="font-size:x-small
{
<font size=2>

</font>
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small SerialPort<span style="font-size:x-small _serialPort;<font size=2>

</font>
<span style="color:#008000;font-size:x-small <span style="color:#008000;font-size:x-small // delegate is used to write to a UI control from a non-UI thread
<span style="font-size:x-small <font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small private<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small delegate<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small SetTextDeleg<span style="font-size:x-small (<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small string<span style="font-size:x-small text);<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small public<span style="font-size:x-small Form1()
{
InitializeComponent();
}
<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small private<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small btnStart_Click(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small object<span style="font-size:x-small sender, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small EventArgs<span style="font-size:x-small e)
{
<font size=2>

</font>
<span style="color:#008000;font-size:x-small <span style="color:#008000;font-size:x-small // Makes sure serial port is open before trying to write
<span style="font-size:x-small <font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small try
<span style="font-size:x-small
{
<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small if<span style="font-size:x-small (!_serialPort.IsOpen)
_serialPort.Open();
<font size=2>
_serialPort.Write(
</font>
<span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "SIrn"<span style="font-size:x-small );
}
<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small catch<span style="font-size:x-small (<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Exception<span style="font-size:x-small ex)
{
<font size=2>

</font>
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small MessageBox<span style="font-size:x-small .Show(<span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Error opening/writing to serial port :: "<span style="font-size:x-small + ex.Message, <span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "Error!"<span style="font-size:x-small );
}
}
<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small private<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small Form1_Load(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small object<span style="font-size:x-small sender, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small EventArgs<span style="font-size:x-small e)
{

<font size=2>
_serialPort =
</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small new<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small SerialPort<span style="font-size:x-small (<span style="color:#a31515;font-size:x-small <span style="color:#a31515;font-size:x-small "COM1"<span style="font-size:x-small , 19200, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Parity<span style="font-size:x-small .None, 8, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small StopBits<span style="font-size:x-small .One);<font size=2>
_serialPort.Handshake =
</font>
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Handshake<span style="font-size:x-small .None;<font size=2>
_serialPort.DataReceived +=
</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small new<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small SerialDataReceivedEventHandler<span style="font-size:x-small (sp_DataReceived);
_serialPort.ReadTimeout = 500;
_serialPort.WriteTimeout = 500;
_serialPort.Open();
}
<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small sp_DataReceived(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small object<span style="font-size:x-small sender, <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small SerialDataReceivedEventArgs<span style="font-size:x-small e)
{
<font size=2>

</font>
<span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small Thread<span style="font-size:x-small .Sleep(500);<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small string<span style="font-size:x-small data = _serialPort.ReadLine();<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small this<span style="font-size:x-small .BeginInvoke(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small new<span style="font-size:x-small <span style="color:#2b91af;font-size:x-small <span style="color:#2b91af;font-size:x-small SetTextDeleg<span style="font-size:x-small (si_DataReceived), <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small new<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small object<span style="font-size:x-small [] { data });
}
<font size=2>

</font>
<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small private<span style="font-size:x-small <span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small void<span style="font-size:x-small si_DataReceived(<span style="color:#0000ff;font-size:x-small <span style="color:#0000ff;font-size:x-small string<span style="font-size:x-small data)
{
textBox1.Text = data.Trim();
}
}<br/><br/>regards<br/><br/>hot n thought



View the full article
 
Back
Top