Click here to Skip to main content
15,886,732 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
What im trying to do, is get string data from my mk by pushing a button on it, but what im gettin on my c# program, its some random numbers(picture is attached).
http://imgur.com/a/79Pbm

What I have tried:

For example i want to receive "5", but i receive "650683" instead of it. On the mk side everything is working, i checked it with Emulator, so the problem is on c# code. Thanks in further.
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 WindowsFormsApplication8 
{
    public partial class Form1 : Form
    {
         public Form1()
         {
              InitializeComponent();
          }

         private void button1_Click(object sender, EventArgs e)   // Here i send a byte to MK
         {
             var dataByte = new byte[] { 0x00 };
             serialPort1.Write(dataByte, 0, 1);
         }

         private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)  // choosing a right com port
        {
            serialPort1.PortName = textBox1.Text;
            serialPort1.BaudRate = Convert.ToInt32(textBox2.Text);

        }
        int rs;
        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) // Data Receive Handler
        {
            try
            {
                rs = serialPort1.ReadByte();
                this.Invoke(new EventHandler(type));
            }
            catch (System.TimeoutException) { }
        }
        void type(object s,EventArgs e)              // receive data
        {
            textBox4.Text += rs.ToString(); 
        }

        private void button3_Click(object sender, EventArgs e)   // OPen port
        {
            serialPort1.Open();
        }

        private void button4_Click(object sender, EventArgs e)  // Close port
        {
            serialPort1.Close();
        }
    }
}
Posted
Updated 30-Jan-17 21:18pm
Comments
[no name] 30-Jan-17 23:54pm    
How do you confirm that baudrate and other settings are correct?
1. I would check that you are getting data as you expect on the PC end using PuTTy http://www.chiark.greenend.org.uk/~sgtatham/putty/
2. When you get 1. working make sure you set the serial port to exactly the same settings in your c# code. The debugger will help.

1 solution

you could have a look on
Machine Controller[^]
It is about serial and Parallel Port in VB but could uses as DLL via C# or even converted to C#
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900