Quantcast
Channel: CodeGuru Forums - Visual C++ Programming
Viewing all articles
Browse latest Browse all 3029

Data Received From Serial Port not

$
0
0
hi all i am received following string from Device and store in database N1Z1802200000000000001000000S009000570101640048500851202880033800265003140015000536000280001400048004420013100098002530 Problem is date 1.) complete data not show in Text box 2.) Data save in database in shape of multi record . Kindly advise me . ===code Start ======= 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 System.Data.OleDb; using MY251_Final.Screens.Templates; using System.Configuration; namespace M235_Final { public partial class TestingUtilityForm : TemplateForm { OleDbConnection connection = new OleDbConnection(); // String DataOut; String DataIN; public TestingUtilityForm() { InitializeComponent(); connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\Interfacing.accdb"; } private void TestingUtilityForm_Load(object sender, EventArgs e) { connection.Open(); connectionStatusLabel.Text = "Connection Establish"; connection.Close(); // Create Function to get Serial Port string[] ports = SerialPort.GetPortNames(); comPortsComboBox.Items.AddRange(ports); } private void openButton_Click(object sender, EventArgs e) { try { serialPort1.PortName = comPortsComboBox.Text; serialPort1.BaudRate = Convert.ToInt32(baudComboBox.Text); serialPort1.DataBits = Convert.ToInt32(dataBitsComboBox.Text); serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBitsComboBox.Text); serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), parityComboBox.Text); serialPort1.Open(); progressBar1.Value = 100; } catch (Exception Err) { MessageBox.Show(Err.Message,"Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void closeButton_Click(object sender, EventArgs e) { if (serialPort1.IsOpen) { serialPort1.Close(); progressBar1.Value = 0; } } private void sendDataButton_Click(object sender, EventArgs e) { } private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { DataIN = serialPort1.ReadExisting(); this.Invoke(new EventHandler(ShowData)); try { connection.Open(); OleDbCommand command = new OleDbCommand(); command.Connection = connection; command.CommandText = "insert into tblData(MYdata) values('"+ DataIN + "')"; command.ExecuteNonQuery(); DataIN = null; connection.Close(); } catch (Exception ex) { MessageBox.Show("error"+ex); } } private void ShowData(object sender, EventArgs e) { int dataINLength = DataIN.Length; dataInLengthLabel.Text = String.Format("{0:00}", DataIN.Length); INdataTextBox.Clear(); INdataTextBox.Text += DataIN; } private void portStatusLabel_Click(object sender, EventArgs e) { } private void clearButton_Click(object sender, EventArgs e) { INdataTextBox.Text = ""; dataInLengthLabel.Text = ""; } private void INdataTextBox_TextChanged(object sender, EventArgs e) { int DataOutLength = INdataTextBox.TextLength; dataInLengthLabel.Text = String.Format("{0:00}", DataOutLength); } private void connectionStatusLabel_Click(object sender, EventArgs e) { } private void showDataButton_Click(object sender, EventArgs e) { try { connection.Open(); OleDbCommand command = new OleDbCommand(); command.Connection = connection; string query = "select * from tblData"; command.CommandText = query; OleDbDataAdapter da = new OleDbDataAdapter(command); DataTable dt = new DataTable(); da.Fill(dt); dataGridView1.DataSource = dt; connection.Close(); } catch (Exception ex) { MessageBox.Show("error" + ex); } } } } =======End Code============== Attachment 35177 (http://forums.codeguru.com/attachment.php?attachmentid=35177)

Viewing all articles
Browse latest Browse all 3029

Latest Images

Trending Articles



Latest Images