Barcode Scanner Only Reading One Character - c#

I am working on a small winform application that reads input from a barcode scanner on a virtual com port and writes the data back to a text box on my winform. I am new to C# so have been struggling through. My current code is below and adapted from here
namespace Barcode_Scanner
{
public partial class Form1 : Form
{
SerialPort sp;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
comboBox1.DataSource = ports;
Application.DoEvents();
}
private void btn_getComData_Click(object sender, EventArgs e)
{
try
{
if (!sp.IsOpen)
sp.Open();
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
}
catch (Exception ex)
{
MessageBox.Show("There was a problem with the Serial Port: " + ex.Message, "Error!");
}
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
sp = (SerialPort)sender;
string data = sp.ReadExisting();
txt_comData.Text = data;
Application.DoEvents();
}
private void button1_Click(object sender, EventArgs e)
{
// Makes sure serial port is open before trying to write
string portname = comboBox1.SelectedItem.ToString();
sp = new SerialPort(portname, 9600, Parity.None, 8, StopBits.One);
sp.Handshake = Handshake.None;
sp.Open();
}
private void button2_Click(object sender, EventArgs e)
{
sp.Close();
}
}
}
The full string I am trying to scan is "3894038" but I am only able to get the textbox to display one character at a time in the text box. I suspect it has something to do with my .ReadExisting command, but I am a bit perplexed on how to proceed. Is there something wrong with my code?
Thanks for the help in advance.
Marshall

There are quite a few issues with your code. The issue you described is caused by the fact that you are assigning the value of ReadExisting to the textbox rather than appending it. I've fixed that and several other issues below.
Notes:
Use AppendText instead of assigning to add new data to the end of the text box's text
There is virtually never a good reason to call Application.DoEvents
You open the serial port in an inconsistent way in two different places
You already have sp in defined at the class level; there's no need to hide it with a cast of the event sender.
Fixed code:
namespace Barcode_Scanner
{
public partial class Form1 : Form
{
SerialPort sp;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] ports = SerialPort.GetPortNames();
comboBox1.DataSource = ports;
}
private void btn_getComData_Click(object sender, EventArgs e)
{
try
{
if (!sp.IsOpen)
{
button1_Click(null, EventArgs.Empty);
}
}
catch (Exception ex)
{
MessageBox.Show("There was a problem with the Serial Port: " + ex.Message, "Error!");
}
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string data = sp.ReadExisting();
txt_comData.Appendtext(data);
}
private void button1_Click(object sender, EventArgs e)
{
// Makes sure serial port is open before trying to write
string portname = comboBox1.SelectedItem.ToString();
sp = new SerialPort(portname, 9600, Parity.None, 8, StopBits.One);
sp.Handshake = Handshake.None;
sp.Open();
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
}
private void button2_Click(object sender, EventArgs e)
{
sp.Close();
}
}
}

Related

Scoreboard data transfer Serial Port c#

There's some outdated software that's used to control the scoreboard at my local athletics track and I've been tasked with creating a new advanced one. However, I cannot seem to get the scoreboard to do what I ask it to do.
I've installed the original software to the my laptop and it works fine, however, when I run my test software that sends data to the board through the serial port, it isn't doing what I want.
I have the "Scoreboard Data Protocol" supplied by the manufacturer and I've been following this. I will supply the code to my test program that I've been using to see if I can get it to work and I will also supply the Data Protocol.
In the text box, I type 010D0201SPAIN and 003C630 and send it to the board and this doesn't work.
public partial class Form1 : Form
{
private SerialPort m_port;
public Form1()
{
InitializeComponent();
m_list.Items.AddRange(SerialPort.GetPortNames()); // Adds ports to combobox
m_port = new SerialPort();
m_port.BaudRate = 9600;
m_port.DataBits = 8;
m_port.Parity = Parity.Even;
m_port.StopBits = StopBits.One;
//m_port.Handshake = Handshake.None;
m_port.Encoding = new ASCIIEncoding();
m_port.RtsEnable = true;
m_port.DtrEnable = true;
m_port.ReceivedBytesThreshold = 1;
m_port.DataReceived += DataReceivedEvent;
}
private void button1_Click(object sender, EventArgs e)
{
m_port.Close();
m_port.PortName = (string)m_list.SelectedItem;
try
{
m_port.Open();
m_sendbutton.Enabled = true;
button2.Enabled = true;
}catch(UnauthorizedAccessException ex)
{
MessageBox.Show(ex.Message);
}
}
private void m_sendbutton_Click(object sender, EventArgs e)
{
m_port.Write(m_textbox.Text);
}
private void DataReceivedEvent(object sender, SerialDataReceivedEventArgs args)
{
Invoke(new EventHandler(DoUpdate));
}
private void DoUpdate(object s, EventArgs e)
{
label1.Text += m_port.ReadLine();
}
private void button2_Click(object sender, EventArgs e)
{
byte[] r_bytes = Encoding.ASCII.GetBytes(m_textbox.Text);
m_port.Write(r_bytes, 0, r_bytes.Length);
}
}
}
Scoreboard Data Protocol
Code: https://hastebin.com/epirobuduv.cs
Here's how to add STX and ETX around your message, in a byte array.
private void button2_Click(object sender, EventArgs e)
{
var msg = Encoding.ASCII.GetBytes(m_textbox.Text).ToList();
msg.Insert(0, 0x02); // STX at the start
msg.Add(0x03); // ETX at the end
m_port.Write(msg.ToArray(), 0, msg.Count);
}

Issue in Serial Communication

I have written a code in c# for receiving data through serial communication and display it in a text box. I am able to do that. But I am receiving data in following format, e.g.
1234 2345 3456 5667
or
every byte in next line..
But I want that every incoming byte should overwrite the previous byte and in the text box we should see just 1 byte and then another byte overwriting the previous one.
Please suggest how to do that?
private void SerialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = SerialPort.ReadExisting();
this.Invoke(new EventHandler(DisplayText));
}
private void DisplayText(object sender, EventArgs e)
{
txtIncomingData.AppendText(RxString);
}
You are appending every received data to your textbox! that is an expected behavior. You should rather change your textBox.Text value every time you want to display a new received data.
Change your DisplayText method from :
private void DisplayText(object sender, EventArgs e)
{
txtIncomingData.AppendText(RxString);
}
to
private void DisplayText(object sender, EventArgs e)
{
txtIncomingData.Text = RxString;
}
P.S: I am assuming that txtIncomingData is a textBox control, correct me if I'm wrong
The solution below works, but it gives blank bytes in between data.
SerialPort serialPort;
private delegate void SetTextDeleg(string text);
private void Form1_Load(object sender, EventArgs e)
{
serialPort= new SerialPort("COM6", 4800, Parity.None, 8, StopBits.One);
serialPort.Handshake = Handshake.None;
serialPort.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
serialPort.ReadTimeout = 500;
serialPort.WriteTimeout = 500;
serialPort.Open();
}
private void btnStart_Click(object sender, EventArgs e)
{
try
{
if(!serialPort.IsOpen)
serialPort.Open();
}
catch (Exception ex)
{
MessageBox.Show("Could not open Serial Port");
}
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(500);
string data = serialPort.ReadLine();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
}
private void si_DataReceived(string data)
{
txtIncomingData.Text = data.Trim();
}

Error reading serial port with C #

I have a system that makes a loop on all serial ports and send a command 'at' for them.
These ports return 'ok' in Putty, but in the C# the MessageBox is empty.
Why is this happening?
Putty: http://i.imgur.com/AZY7aRY.png /
C#: http://i.imgur.com/wgGyT2x.png
public partial class Form1 : Form
{
List<SerialPort> serialPort = new List<SerialPort>();
private delegate void SetTextDeleg(string text);
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var portNames = SerialPort.GetPortNames();
foreach (var port in portNames)
{
SerialPort sp;
sp = new SerialPort(port, 9600, Parity.None, 8, StopBits.One);
sp.Handshake = Handshake.None;
sp.DataReceived += new SerialDataReceivedEventHandler(sp_DataReceived);
sp.ReadTimeout = 500;
sp.WriteTimeout = 500;
serialPort.Add(sp);
listPorts.Items.Add(port);
}
}
private void listPorts_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
foreach (var sp in serialPort)
{
// Open port
try
{
if (!sp.IsOpen)
sp.Open();
sp.Write("at\r\n");
}
catch (Exception ex)
{
MessageBox.Show("Error opening/writing to serial port :: " + ex.Message, "Error!");
}
}
}
void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
Thread.Sleep(500);
SerialPort sp = (SerialPort)sender;
string data = sp.ReadLine();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { data });
}
private void si_DataReceived(string data)
{
String retorno = data.Trim();
MessageBox.Show(data);
}
}
Your modem response something like this: \n\rOK\n\r you should read all stream in the buffer or you should read 3 time the serial port buffer with ReadLine() method.

How to display the data read in DataReceived event handler of serialport

I have the following code which needs the data to be read from port and then display in a textbox. I am using DataReceived event handler for this purpose but donot know how to display this data in textbox. From various sources i learnt that Invoke method should be used for this but donot know how to use it. Suggestions please...
private void Form1_Load(object sender, EventArgs e)
{
//SerialPort mySerialPort = new SerialPort("COM3");
mySerialPort.PortName = "COM3";
mySerialPort.BaudRate = 9600;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
mySerialPort.Open();
}
private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string s= sp.ReadExisting();
// next i want to display the data in s in a textbox. textbox1.text=s gives a cross thread exception
}
private void button1_Click(object sender, EventArgs e)
{
mySerialPort.WriteLine("AT+CMGL=\"ALL\"");
}
The MSDN contains a good article with examples about using control methods and properties from other threads.
In short, what you need is a delegate method that sets the Text property of your text box with a given string. You then call that delegate from within your mySerialPort_DataReceived handler via the TextBox.Invoke() method. Something like this:
public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;
private void Form1_Load(object sender, EventArgs e)
{
//...
this.myDelegate = new AddDataDelegate(AddDataMethod);
}
public void AddDataMethod(String myString)
{
textbox1.AppendText(myString);
}
private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string s= sp.ReadExisting();
textbox1.Invoke(this.myDelegate, new Object[] {s});
}
Try this (works for me):
private delegate void UpdateUiTextDelegate(string text);
private void Receive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
if (mySerialPort.IsOpen)
{
RxString = mySerialPort.ReadLine();
Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), RxString);
}
}
private void DisplayText(string RxString)
{
myTextBox.Text = RxString;
}
I'm creating a GUI "Form" for USB COM Ports. This is how I send data to the window without getting a "Cross-Thread" error.
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
string inData = serialPort1.ReadLine(); // ReadLine includes the + "\n"
displayToWindow(inData);
}
private void displayToWindow(string inData)
{
BeginInvoke(new EventHandler(delegate
{
richTextBox1.AppendText(inData);
richTextBox1.ScrollToCaret();
}));
}

Incoming serial data loop is causing my program to freeze

I am working on code that connects to a serial port of a stepper motor. I send a command to the stepper motor via textBox2 and am attempting to read in the return data from the command to textBox3. I am able to make a connection, send the command, and receive the data. But after my GUI populates textBox3 with the returned serial data it freezes.
I believe that the code is getting stuck in the try loop but I don't know how to break out of it. Here is my code:
private void button3_Click(object sender, EventArgs e)
{
if (isConnectedMotor)
{
string command = textBox2.Text;
portMotor.Write(command + "\r\n");
portMotor.DiscardInBuffer();
while (true)
{
try
{
string return_data = portMotor.ReadLine();
textBox3.AppendText(return_data);
textBox3.AppendText(Environment.NewLine);
}
catch(TimeoutException)
{
break;
}
}
}
}
DataReceived Code:
private void connectToMotor()
{
isConnectedMotor = true;
string selectedPort = comboBox2.GetItemText(comboBox2.SelectedItem);
portMotor = new SerialPort(selectedPort, 9600, Parity.None, 8, StopBits.One);
portMotor.RtsEnable = true;
portMotor.DtrEnable = true;
portMotor.Open();
portMotor.DiscardInBuffer();
portMotor.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
button4.Text = "Disconnect";
enableControlsMotor();
}
private static void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
textBox3.AppendText(indata);
textBox3.AppendText(Environment.NewLine);
}
I am getting an error saying:
An object reference is required for the non-static field, method, or property 'Form1.textBox3'
Invoke code:
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
portMotor.DiscardInBuffer();
incoming_data = portMotor.ReadExisting();
this.Invoke(new EventHandler(displayText));
}
private void displayText(object o, EventArgs e)
{
textBox3.Text += incoming_data;
}
Instead of looping to read data, use a DataReceived event to get at the incoming bytes asynchronously.
See the documentation and example.
Also see this question for troubleshooting.
P.S. Here is a code sample to avoid locking up the UI.
private void ReceivedHandler(object sender, SerialDataReceivedEventArgs e) {
var incoming_data = portMotor.ReadExisting();
// this is executing on a separate thread - so throw it on the UI thread
Invoke(new Action(() => {
textBox3.Text += incoming_data;
}));
}

Categories