i am develop application for getting weight from weigh Bridge machine using C#.Net.i am trying lot of ways but,doesn't read correct data format weight from weigh bridge machine.i am getting ouput like ?x???????x?x?x??x???x??x???x???x? continuously get from serial port.i want to get weight from weigh bridge machine my code is listed below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
namespace SerialPortTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String a = "";
private void button1_Click(object sender, EventArgs e)
{
serialPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
if (serialPort1.IsOpen == false)
{
serialPort1.Open();
}
timer1.Start();
button1.Enabled = false;
button2.Enabled = true;
}
private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
a = a + serialPort1.ReadExisting();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (a.Length != 0)
{
textBox1.AppendText(a);
a = "";
}
}
private void button2_Click(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
{
serialPort1.Close();
button2.Enabled = false;
button1.Enabled = true;
}
}
private void Form1_Load(object sender, EventArgs e)
{
if (serialPort1.IsOpen == true)
{
button1.Enabled = false;
button2.Enabled = true;
}
else
{
button1.Enabled = true;
button2.Enabled = false;
}
}
}
}
my code is append text from serial port data to textbox but,it's shows only like ?xxx?xxxx?xxxx?
can any one help me how to get weight from serial port using c#
Thanks For Reading My Post!
You are using ReadExisting(), that method tries to convert the bytes received by the port into a string. You'll get a question mark if that conversion fails. The default Encoding is ASCII, a byte value between 128 and 255 is not an ASCII character and thus produces a ?
Several possible reasons, roughly in order of likelihood:
Using the wrong baud rate, in particular guessing too high.
The device might be sending binary data, not strings. Which requires using Read() instead of ReadExisting and decoding the binary data.
Electrical noise picked up by a long cable that isn't shielded well enough. Easy to eliminate as a possible reason by disconnecting the cable at the bridge end. If that stops the data then it isn't likely to be noise.
Be sure to thoroughly read the manual. Contact the vendor of the device if you don't have one or can't make sense of it.
This code will be reading weightbridge continuously in background. Be sure to connect the pc with serial port. Also in design page Form1.cs[Design] you need to add Serial port from the toolbox. This code works for me, I hope it works for you too...
public partial class Form1 : Form
{
//Initialize the port and background Worker
private SerialPort port;
private BackgroundWorker backgroundWorker_Indicator;
public Form1()
{
backgroundWorker_Indicator = new BackgroundWorker();
backgroundWorker_Indicator.WorkerSupportsCancellation = true;
backgroundWorker_Indicator.DoWork += new DoWorkEventHandler(Indicator_DoWork);
//set the port according to your requirement.
port = new SerialPort("COMM2", 2400, Parity.None, 8, StopBits.One);
port.DataReceived += new SerialDataReceivedEventHandler(this.Indicator_DataReceived);
}
//button which starts the method. You can also put the method in Form1_Load()
private void SerialPortButton(object sender, EventArgs e)
{
StartStopIndicator();
}
private void StartStopIndicator()
{
try
{
port.Open();
backgroundWorker_Indicator.RunWorkerAsync();
}catch (Exception ea)
{
MessageBox.Show("13 "+ea.Message);
}
}
// Not a button. Just a methood.
private void Indicator_DoWork(object sender, DoWorkEventArgs e)
{
}
private void Indicator_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string str = StripNonNumeric(port.ReadLine());
UpdateWeightOnUI(str);
}
catch (Exception eb)
{
MessageBox.Show("12"+eb.Message);
}
}
private void UpdateWeightOnUI(string Weight)
{
try
{
// A label named weightLabel from the toolbox. This will keep updating on weight change automatically
if (weightLabel.InvokeRequired)
{
this.Invoke((Delegate)new Form1.SetTextCallBack(this.UpdateWeightOnUI), (object)Weight);
}
else
{
weightLabel.Text = Weight;
}
}
catch (Exception ec)
{
MessageBox.Show("11"+ec.Message);
}
}
// This method will remove all other things except the integers
private string StripNonNumeric(string original)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (char c in original)
{
if (char.IsDigit(c))
stringBuilder.Append(c);
}
return stringBuilder.ToString();
}
private delegate void SetTextCallBack(string text);
Related
I am tasked with creating a Windows form application for the factory i work for.
We will be weighing multiple bags, so every time the button is pressed it must send the weight that is currently on the scale. In other words just capture the current weight and wait for me to press the button again to capture the next bag that will be loaded on the scale. Currently it continuously receives data from the scale once the button is clicked and the result only flashes in my textbox. I want to change so it only receives one weight then stops. Can you please show me how i can accomplish this.
Thank you in advance
Below is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using Microsoft.Office.Interop.Excel;
namespace ScaleV5
{
public partial class Form1 : Form
{
private SerialPort _serialPort; //<-- declares a SerialPort Variable to be used throughout the form
private const int BaudRate = 9600; //<-- BaudRate Constant. 9600 seems to be the scale-units default value
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] portNames = SerialPort.GetPortNames(); //<-- Reads all available comPorts
foreach (var portName in portNames)
{
comboBox1.Items.Add(portName); //<-- Adds Ports to combobox
}
comboBox1.SelectedIndex = 0; //<-- Selects first entry (convenience purposes)
}
private void button1_Click(object sender, EventArgs e)
{
//<-- This block ensures that no exceptions happen
if (_serialPort != null && _serialPort.IsOpen)
_serialPort.Close();
if (_serialPort != null)
_serialPort.Dispose();
//<-- End of Block
_serialPort = new SerialPort(comboBox1.Text, BaudRate, Parity.None, 8, StopBits.One); //<-- Creates new SerialPort using the name selected in the combobox
_serialPort.DataReceived += SerialPortOnDataReceived; //<-- this event happens everytime when new data is received by the ComPort
_serialPort.Open(); //<-- make the comport listen
textBox1.Multiline = true;
textBox1.Text += $"Listening on " + _serialPort.PortName + $"\r\n";
}
private delegate void Closure();
private void SerialPortOnDataReceived(object sender, SerialDataReceivedEventArgs serialDataReceivedEventArgs)
{
if (InvokeRequired) //<-- Makes sure the function is invoked to work properly in the UI-Thread
BeginInvoke(new Closure(() => { SerialPortOnDataReceived(sender, serialDataReceivedEventArgs); })); //<-- Function invokes itself
else
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
string str = System.Text.Encoding.UTF8.GetString(data);
textBox1.Text = str.ToString();
_serialPort.WriteLine("w");
}
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
_serialPort.Close();
this.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
}
}
put the entire port instantiation and closing/opening code into the SelectedIndexChanged event of the combobox. When the user chooses a port you simply open it and leave it open. Opening and closing ports takes time. So you should keep it to a minimum.
When pressing the button you would only probe the scale via the port for the values. (If you really have 2 modes of operation choose the single measurement)
private void button1_Click(object sender, EventArgs e)
{
// probe device for value
_serialPort.WriteLine("w");
Thread.Sleep(100);
string response = _serialPort.ReadExisting();
// here parse the resonse to extract you value
// stop the device
_serialPort.WriteLine("w");
textBox1.Multiline = true; // <- this code belongs into the constructor
textBox1.Text += extractedStringValue;
}
disclaimer: this code runs synchronously and will block the UI. It depends strongly on your device how long it need to responde to your first request of measurements. To avoid the blocking of the UI you would need to work with async/await.
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);
}
I have an application in Visual studio, reading value from serial port and drawing it on a chart. Everything goes perfecly fine, but when I click a close button on the application (or serial port disconnect button), an error occurs:"IOException() was unhandled", and the program highlights the serial1.Readline() command. How can I handle the exception?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Windows.Forms.DataVisualization.Charting;
namespace usart3
{
public partial class OknoGlowne : Form
{
public OknoGlowne()
{
string[] mojePorty = SerialPort.GetPortNames();
InitializeComponent();
foreach (string port in mojePorty)
{
cmbPorty.Items.Add(port);
}
cmbBaud.Items.Add(2400);
cmbBaud.Items.Add(9600);
cmbBaud.Items.Add(19200);
btnRozlacz.Enabled = false;
}
private volatile string rxString;
//private byte[] rxByte;
private Object thisLock = new Object();
Boolean i = false;
private void btnPolacz_Click(object sender, EventArgs e)
{
i = true;
serialPort1.PortName = cmbPorty.Text;
serialPort1.BaudRate = Convert.ToInt32(cmbBaud.Text);
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.DataBits = 8;
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.Open();
btnPolacz.Enabled = false;
btnRozlacz.Enabled = true;
}
int rt = 0;
private void btnRozlacz_Click(object sender, EventArgs e)
{
serialPort1.Close();
btnPolacz.Enabled = true;
btnRozlacz.Enabled = false;
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
rt++;
rxString = serialPort1.ReadLine();
this.Invoke(new EventHandler(displayText));
}
private void displayText(object o, EventArgs e)
{
richTextBox1.AppendText(rxString);
this.chart1.Series["Temperatura"].Points.AddXY(rt, rxString);
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Form2 nowy = new Form2();
nowy.Show();
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void Start2_Click(object sender, EventArgs e)
{
Form2 nowy = new Form2();
nowy.Show();
}
private void button1_Click_1(object sender, EventArgs e)
{
Form2 nowy = new Form2();
nowy.Show();
}
}
}
I had similar issue.
The IOExcpection is kind of confusing because accordingly to the documentation ReadLine() is not supposed to throw this type of exception. The problem is that ReadLine is in a state of waiting for an EOL character when we close the port. Closing the port stops the thread holding ReadLine in an unsupported state.
For me the solution was to follow this port closure sequence:
Unsubscribe data_reciver method from the port.
Send a port.WriteLine("get serial number") command. The goal is to trig an EOL character in the input_buffer. This will release ReadLine.
Wait until EOL is recieved.
Close the port.
Surround your call with a try/catch block. In the catch you should code in way that you can handle the error by doing something else.
try {
rxString = serialPort1.ReadLine();
} catch (IOException e) {
// or do something else
}
EDIT:
try {
rt++;
rxString = serialPort1.ReadLine();
this.Invoke(new EventHandler(displayText));
} catch (IOException e) {
MessageBox.Show("Connection is lost");
}
I am new to C# and Visual Studio. I have built a GUI to choose the COM port from a MenuStrip in Visual Studio 2013. What I want to know is how can I connect this with serial port communication.
Should I use a another class for serial communication? Or can I do it in the same class? How can it be programmed?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace exo_new
{
public partial class rehab : Form
{
public rehab()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void conndToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void cOM1ToolStripMenuItem_Click(object sender, EventArgs e)
{
cOM1ToolStripMenuItem.Checked = true;
cOM2ToolStripMenuItem.Checked = false;
cOM3ToolStripMenuItem.Checked = false;
cOM4ToolStripMenuItem.Checked = false;
cOM5ToolStripMenuItem.Checked = false;
cOM6ToolStripMenuItem.Checked = false;
cOM7ToolStripMenuItem.Checked = false;
cOM8ToolStripMenuItem.Checked = false;
cOM9ToolStripMenuItem.Checked = false;
cOM10ToolStripMenuItem.Checked = false;
}
private void cOM2ToolStripMenuItem_Click(object sender, EventArgs e)
{
cOM2ToolStripMenuItem.Checked = true;
cOM1ToolStripMenuItem.Checked = false;
cOM3ToolStripMenuItem.Checked = false;
cOM4ToolStripMenuItem.Checked = false;
cOM5ToolStripMenuItem.Checked = false;
cOM6ToolStripMenuItem.Checked = false;
cOM7ToolStripMenuItem.Checked = false;
cOM8ToolStripMenuItem.Checked = false;
cOM9ToolStripMenuItem.Checked = false;
cOM10ToolStripMenuItem.Checked = false;
}
You can use the System.IO.SerialPort class to program the serial port from a .NET application. (The link is to the MSDN documentation for the class, which includes an example program.) If you want more help than that you'll need to provide some of your code and more of an explanation of what you want to do, and when. (For example, user clicks button X and you want to send message Y...)
UPDATE: Thanks for sharing your code so far. Here is how I would implement a simple solution based on what you've started with:
public partial class rehab : Form
{
private string portName = "COM1";
private const int baudRate = 9600;
public Form1()
{
InitializeComponent();
//TODO: Simplify your UI by dynamically creating the COM port names.
// Get the list of available ports on the computer via the following:
//var portNames = SerialPort.GetPortNames();
// Call this to initially mark 'COM1' as checked.
UpdatePortCheckmarks();
}
private void conndToolStripMenuItem_Click(object sender, EventArgs e)
{
var textToSend = this.textBox1.Text;
// Use a try-catch block to log any exceptions that occur.
try
{
// Use a using block to close and dispose of the serial port
// resource automatically. Also, note that the SerialPort
// constructor takes the port name and baud rate here.
// There are also overloads that let you pass the number of
// data bits, parity, and stop bits, if needed.
using (var serialPort = new SerialPort(portName, baudRate))
{
// Open the port before writing to it.
serialPort.Open();
// Send the content of the textbox (with a newline afterwards).
serialPort.WriteLine(textToSend);
}
}
catch (Exception ex)
{
// You could also use MessageBox.Show. Console.WriteLine will
// display errors in your debugger's output window.
Console.WriteLine("ERROR: " + ex.ToString());
}
}
private void cOM1ToolStripMenuItem_Click(object sender, EventArgs e)
{
portName = "COM1";
UpdatePortCheckmarks();
}
private void cOM2ToolStripMenuItem_Click(object sender, EventArgs e)
{
portName = "COM2";
UpdatePortCheckmarks();
}
// .. and so on for each additional port menu item (COM3 through COM10)
// This method lets you share the code for updating the checkmarks on
// the menu items, so your form code will be cleaner.
private void UpdatePortCheckmarks()
{
cOM1ToolStripMenuItem.Checked = portName == "COM1";
cOM2ToolStripMenuItem.Checked = portName == "COM2";
cOM3ToolStripMenuItem.Checked = portName == "COM3";
cOM4ToolStripMenuItem.Checked = portName == "COM4";
cOM5ToolStripMenuItem.Checked = portName == "COM5";
cOM6ToolStripMenuItem.Checked = portName == "COM6";
cOM7ToolStripMenuItem.Checked = portName == "COM7";
cOM8ToolStripMenuItem.Checked = portName == "COM8";
cOM9ToolStripMenuItem.Checked = portName == "COM9";
cOM10ToolStripMenuItem.Checked = portName == "COM10";
}
}
I've included a 'TODO' comment as a suggestion on how you can further improve your code, but that's optional (and should be a new question if you do have any questions about it).
I am trying to interface my C# Windows Forms application from my laptop to the Arduino Duemilanove. A Bluetooth module is connected to the Tx and Rx pins on the Arduino. My goal is to light up the on-board LED when I type in the letter 'a' and so far it has been unsuccessful. I am sure the Bluetooth is connected with my laptop, but it is not responding to the letter I am pressing.
C# code
public partial class Form1 : Form
{
private Guid service = BluetoothService.SerialPort;
private BluetoothClient bluetoothClient;
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyPress += new KeyPressEventHandler(Form1_KeyPress);
}
void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 'a')
{
Stream peerStream = bluetoothClient.GetStream();
Byte[] buffer = Encoding.ASCII.GetBytes("a");
peerStream.Write(buffer, 0, buffer.Length);
}
}
private void search_Click(object sender, EventArgs e)
{
BluetoothRadio.PrimaryRadio.Mode = RadioMode.Discoverable;
BluetoothRadio myRadio = BluetoothRadio.PrimaryRadio;
bluetoothClient = new BluetoothClient();
Cursor.Current = Cursors.WaitCursor;
BluetoothDeviceInfo[] bluetoothDeviceInfo = { };
bluetoothDeviceInfo = bluetoothClient.DiscoverDevices(10);
comboBox1.DataSource = bluetoothDeviceInfo;
comboBox1.DisplayMember = "DeviceName";
comboBox1.ValueMember = "DeviceAddress";
comboBox1.Focus();
Cursor.Current = Cursors.Default;
}
private void Connect_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedValue != null)
{
try
{
bluetoothClient.Connect(new BluetoothEndPoint((BluetoothAddress)comboBox1.SelectedValue, service));
MessageBox.Show("Connected");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Arduino Code
int incomingByte = 0; // For incoming serial data
void setup()
{
pinMode(13, OUTPUT); // On-board LED as output
Serial.begin(9600); // Opens serial port, sets data rate to 9600 bit/s.
}
void loop()
{
if (Serial.available() > 0)
{
// Read the incoming byte:
incomingByte = Serial.read();
if (incomingByte == 'a')
digitalWrite(13, HIGH);
}
}
Am I sending the ASCII code wrongly or what am I missing?
May be the same issue as I answered here.
I recently dabbled into this. The Arduino automatically resets when it
receives serial communication from most things other than the Arduino
IDE. This is why you can send from the IDE but not node.js.
I have an Uno and put a capacitor between Reset and Ground.Here's a
page with some good info on the subject. Good luck.
http://arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection