none
Passing Hex Data to Serial Port for a card dispenser Device RRS feed

  • Question

  • Hi and Good day to All,

    I have been doing this project for quite some time.I also used Php to do this but failed. So I switched to C#.

    What I've been trying to do is to Pass a certain command to my device in which the Command is 42 (42H and H stands for hex)

    I've written a code but it does not do anything. My device is not dispensing at all.

    I have a tool to test if my serial port is actually passing data which was given to me by a friend.

    By using the tool, it dispenses the card but If I do it on C# Visual Studio, it does not.

    If I press card out it shows [0x42].

    Can anyone help me on this or any advice?

    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;
    
    namespace Card_Reader
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                
                serialPort1.Write(Convert.ToString(0x42));
                textBox3.Text = (Convert.ToString(0x42));
                serialPort1.Close();
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                serialPort1.PortName = "Com2";
                serialPort1.BaudRate = Convert.ToInt32("9600");
    
            }
            int rs;
            private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                try {
                    rs = serialPort1.ReadByte();
                    this.Invoke(new EventHandler(type));
                }
                catch (System.TimeoutException) { }
            }
            void type(object s, EventArgs e) {
                textBox4.Text += rs.ToString();
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                serialPort1.Open();
            }
        }
    }
    


    Saturday, January 13, 2018 4:43 PM

All replies

  • Maybe it should be ‘serialPort1.Write( new byte[] { 0x42 }, 0, 1 )’.

    Do you know if the device expects a single byte containing 42H or two characters ‘4’ and ‘2’?

    Saturday, January 13, 2018 7:35 PM
  • Hello,

     You best bet is to visit the MFG website for interface and coding support. What

    we offer is limited because you have omitted important details about the device.

     

     Thanks :)

    Sunday, January 14, 2018 9:39 PM