1

I'm trying to read data by serial port from GTCO Calcomp Drawingboard VI, a graphic tablet (https://www.gtcocalcomp.com/drawingboard-vi-small-format-tech-specs/).

I tried to use a simple c# console application and it was possible to read some coordinates. Follows the code:

C# CONSOLE CODE

using System;
using System.IO.Ports;

class PortDataReceived
{
    public static void Main()
    {
        SerialPort mySerialPort = new SerialPort("COM3");

        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.RtsEnable = true;

        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        mySerialPort.Open();

        Console.WriteLine("Press any key to continue...");
        Console.WriteLine();
        Console.ReadKey();
        mySerialPort.Close();
    }

    private static void DataReceivedHandler(
                        object sender,
                        SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        Console.WriteLine("Data Received:");
        Console.Write(indata);
    }
}

The data is showed like this:

DATA SHOWED AT CONSOLE

Data Received:
#Data Received:
,Data Received:
4Data Received:
7Data Received:
8Data Received:
5Data Received:
8Data Received:
,Data Received:
0Data Received:
0Data Received:
0Data Received:
4Data Received:
5Data Received:
,Data Received:
0Data Received:
1Data Received:

It seems like coordinates from my graphic tablet and it is exactly what I need, but I want to implement a c# Windows Form application. I started to crate a simple layout just to connect my serial port and read data. The application looks like this: GUI Serial Application

The code of that is:

C# WINDOWS FORM APPLICATION

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;

namespace SerialDataAplication
{
    public partial class Form1 : Form
    {
        string data = "";

        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (serialPort1.IsOpen == true)  
                serialPort1.Close();            
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            string[] portasativas = SerialPort.GetPortNames();
            comboBox1.DataSource = portasativas;
        }
        
        private void buttonConnect_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen == false)
            {
                try
                {
                    serialPort1.PortName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
                    serialPort1.BaudRate = 9600;
                    serialPort1.DtrEnable = true;
                    serialPort1.DataBits = 8;
                    serialPort1.Parity = Parity.None;
                    serialPort1.StopBits = StopBits.One;
                    serialPort1.Handshake = Handshake.None;
                   
                    serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

                    serialPort1.Open();
                }
                catch
                {
                    return;
                }
                if (serialPort1.IsOpen)
                {
                    button_Connect.Text = "Disconnect";
                    button_Connect.BackColor = Color.LightGreen;
                    comboBox1.Enabled = false;
                }
            }
            else
            {
                try
                {
                    serialPort1.Close();
                    comboBox1.Enabled = true;
                    button_Connect.Text = "Connect";
                    button_Connect.BackColor = Color.Salmon;
                }
                catch
                {
                    return;
                }
            }
        }

        private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            data = sp.ReadExisting();
            this.Invoke(new EventHandler(writedata));
        }

        private void writedata(object sender, EventArgs e)
        {
            richTextBox1.Text += data.ToString() + "\n";
        }
    }
}

I tried to read data (just strings) from arduino and both, console and windows form app, worked well.

Unfortunately I am not getting success showing the data from my graphic tablet on "richtextbox".

1 Answers1

0

The code you posted works for me. However, it's not a good idea to use:

catch
{
    return;
}

because if a port isn't selected in the ComboBox and one clicks the "Connect" button, it won't work, and you won't know why.

Add a check to see if a port has been selected in the ComboBox:

if (comboBox1.SelectedIndex < 0)
{
    MessageBox.Show("Port not selected.", "Port Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

and then use the following:

catch (Exception ex)
{
    System.Diagnostics.Debug.WriteLine("Error - " + ex.Message);
    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    return;
}

Here's the full code:

private void buttonConnect_Click(object sender, EventArgs e)
{
    if (serialPort1.IsOpen == false)
    { 
        try
        {

            if (comboBox1.SelectedIndex < 0)
            {
                MessageBox.Show("Port not selected.", "Port Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            serialPort1.PortName = comboBox1.Items[comboBox1.SelectedIndex].ToString();
            serialPort1.BaudRate = 9600;
            serialPort1.DtrEnable = true;
            serialPort1.DataBits = 8;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;
            serialPort1.Handshake = Handshake.None;

            serialPort1.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

            serialPort1.Open();
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Error - " + ex.Message);
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }

        if (serialPort1.IsOpen)
        {
            button_Connect.Text = "Disconnect";
            button_Connect.BackColor = Color.LightGreen;
            comboBox1.Enabled = false;
        }
    }
    else
    {
        try
        {
            serialPort1.Close();
            comboBox1.Enabled = true;
            button_Connect.Text = "Connect";
            button_Connect.BackColor = Color.Salmon;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine("Error - " + ex.Message);
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
    }
}

You may also consider using richTextBox1.AppendText(data);

private void writedata(object sender, EventArgs e)
{
    //richTextBox1.Text += data.ToString() + "\n";
    richTextBox1.AppendText(data);
}

Update:

Since you've placed all of your code in Form1.cs, writedata is probably unnecessary. Try the following:

private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
    SerialPort sp = (SerialPort)sender;
    data = sp.ReadExisting();
    //this.Invoke(new EventHandler(writedata));

    if (richTextBox1.InvokeRequired)
    {
        richTextBox1.Invoke((MethodInvoker)delegate
        {
            richTextBox1.AppendText(data);
            //richTextBox1.Refresh();
        });
    }
    else
    {
        richTextBox1.AppendText(data);
        //richTextBox1.Refresh();
    }
}
Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • The following post may be useful as well: https://stackoverflow.com/questions/65957066/serial-to-usb-cable-from-a-scale-to-pc-some-values-are-just-question-marks/65971845#65971845 – Tu deschizi eu inchid May 05 '21 at 23:58
  • Did you get some data from serial port using the windows form code?! As I said, I tested the code reading a simple string from arduino and worked. The problem is that using my graphic tablet, it doesn't write anything at richtextbox. – Caio Bittencourt May 06 '21 at 16:56
  • I've updated the code for `DataReceivedHandler`. – Tu deschizi eu inchid May 06 '21 at 21:14
  • I tested the code using a barcode scanner. What do you mean by "graphic tablet"? Are you trying to use a touch screen? I don't have a touch screen computer, but I don't believe that a touch screen generates a mouse click event. See https://stackoverflow.com/questions/23940143/recognize-touch-as-mousedown-event – Tu deschizi eu inchid May 06 '21 at 21:25