0

How can I send a hexadecimal like 0x2b or 0x42 to a serial port using either C# or Php.

42 is the HEX that I need to send by the way.

I used this for C#

using System.IO.Ports;

namespace Card_Reader
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        using (SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8))
        {
            byte[] bytesToSend = new byte[1] { 0x2A };

            port.Open();
            port.Write(bytesToSend, 0, 1);
        }

    }

but nothing happens to the device connected to my serial port.

Now I used this for Php

public function dispense_card() {
    $serial = new CI_Phpserial();

    // First we must specify the device. This works on both linux and windows (if
    // your linux serial device is /dev/ttyS0 for COM1, etc)
    $serial->deviceSet("COM2");

    // We can change the baud rate, parity, length, stop bits, flow control
    $serial->confBaudRate(9600);
    $serial->confParity("even");
    $serial->confCharacterLength(8);
    $serial->confStopBits(1);
    $serial->confFlowControl("none");

    // Then we need to open it
    $serial->deviceOpen();

    // To write into
    /*$serial->sendMessage(0x42);*/
    $str = pack("h*",66);
    $serial->sendMessage($str);
}

I'm getting close to hanging myself since I'm trying to do this since last year and until now I cannot accomplish it.

Newbie here when it comes to serial data sending.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • One obvious difference is that in your PHP code you are setting Even Parity - in your C# code you are setting Parity to None. If you need to send 0x42 why are you sending 0x2A? – PaulF Jan 16 '18 at 15:50
  • I tried Different Parity. But none Worked. I'm quite confused if the device will ready either hex or decimal that is why I send either 0x42 or 0x2A. Kindly assist thanks. – Device Developer Jan 16 '18 at 15:56
  • You've told us the C# code doesn't work. Does the PHP code work? If not, are you sure that the port settings are correct? Which device are you trying to talk to? Do you need to send hex as bytes or is it an ASCII protocol, perhaps? – J... Jan 16 '18 at 16:05
  • @J...Port Settings are correct Sir. I'm quite not sure about how the device accepts data if it is in the form of an ASCII or as bytes. Can you advise me Sir on that one? Thanks. How do I test both. – Device Developer Jan 16 '18 at 16:09
  • Hex 0x42 is also called letter "B". You probably should first check what you're actually trying to do, or check f. ex. http://www.asciitable.com/ -- and at this time of year, "since last year" means 2 weeks? – James Z Jan 16 '18 at 16:25
  • I even used this `byte[] bytesToSend = new byte[1] { 0x2A };` and also nothing happens – Device Developer Jan 16 '18 at 16:42
  • Can you connect the serial port to another PC & monitor if anything is sent? [Or connect to COM1 on the same PC or perform a loop back test connect the Rx & Tx pins & seeing if the byte appears in the read buffer]. NOTE that there is no difference between ASCII & byte - they are just the same way of looking at the same thing - both are 8 bit values - ASCII value "B" is just another way of talking about hex value 0x42. ASCII values just have a limited range, hex values can describe all 256 values from 0x00 to 0xFF. Some applications will show byte values as Octal or Binary values. – PaulF Jan 16 '18 at 17:20
  • RE your first comment 0x42 is not the same as 0x2A. 0x42 = 66 (decimal) as per comment in the PHP code. 0x2A = 42 (decimal). – PaulF Jan 16 '18 at 17:23
  • @DeviceDeveloper The manual for the device would tell you what the protocol should be. If you can tell us what the device is we may be able to help. Some protocols communicate hex values as ASCII characters (ie: the value 0x42 is not sent as one byte, but as two bytes for ASCII characters `4` and `2`. – J... Jan 16 '18 at 17:30
  • It shouldn't be a problem as you are using an ASCII value but if you do need values above 0x7F then you should set the encoding to a value that allow 8bit data e.g. insert port.Encoding = System.Text.Encoding.GetEncoding(1252); before opening the device. See https://stackoverflow.com/questions/7178655/serialport-encoding-how-do-i-get-8-bit-ascii – PaulF Jan 16 '18 at 17:40
  • I have checked your PHP code $str = pack("h*",66); results in $str containing the single ASCII character 'f'. So your PHP code is sending the hex value 0x66 despite the comment it is sending 0x42. NOTE that the lower case h value in the pack statement treats the following value as a hex string with the low nybble first - so pack("h*", 42) returns an ASCII value of '$' or 0x24 and pack("h*", 24) would return ASCII B or hex 0x42. See http://php.net/manual/en/function.pack.php – PaulF Jan 16 '18 at 17:55
  • Thanks Sir @PaulF, Ill try to adjust the code – Device Developer Jan 16 '18 at 18:26
  • This is the only thing I can Find 1. Baud Rate: 9600, E, 8, q 2. Byte 1: ID Number, Byte 2: Command 3. CVD Card Out: 42H (i checked h is for hex) 4. The device will answer (50H) if success 5. The device will answer (4BH) for not successfull – Device Developer Jan 16 '18 at 18:39
  • This is the only thing I can Find 1. Baud Rate: 9600, E, 8, q 2. Byte 1: ID Number, Byte 2: Command 3. CVD Card Out: 42H (i checked h is for hex) 4. The device will answer (50H ACK) if success 5. The device will answer (4BH NAK) for not successfull – Device Developer Jan 16 '18 at 18:41
  • We still don't know what device you are using. Unless you can provide that information we can't do any more to help. – J... Jan 17 '18 at 11:52
  • 1
    It's an ICT CVD(card vending device). – Device Developer Jan 17 '18 at 14:16
  • From your last comments I believe this is what you are using : http://www.jimark.cz/upload/files/produkty_soubory/ict-cvd-300-cvd-1000/CVD_Series_Installation_Guide(EN).pdf - looking at the pinout of the serial connector - it is not a standard 9pin RS-232 pinout - are you using an adapter? – PaulF Jan 17 '18 at 14:19
  • Yes Sir. USB to RS232 then RS232 to WEL-R7U06-2 – Device Developer Jan 17 '18 at 15:53
  • Are you plugging the 9pin connector of the WEL-R7U06-2 directly into the USB-RS232 connector? – PaulF Jan 17 '18 at 16:40
  • @PaulF Yes Sir it is – Device Developer Jan 17 '18 at 18:01
  • But When I used a tool from ICT it works. But my code does not – Device Developer Jan 17 '18 at 18:04
  • I'm looking for a decompiler that can actually decompile this tool they gave me because this tool is working. So I can actually see how did this tool works while my code does not. – Device Developer Jan 17 '18 at 18:23
  • If it works with the ICT tool then the documentation may be misleading & the page with the WEL-R7U06-2 picture and showing Pin connections is referring to the wires label 1-4 behind the 9 pin connector & that is wired correctly. Looking at the communications protocol - refer to page 25 in the document I gave you a link to - you should be sending 2 bytes - the first is the ID of the device (1-4 page 18 says 1 is the default), then the command you want. I suggest you try to monitor the output of the ICT tool - connect COM2 to COM1 & see what arrives there when you get the tool to send a command. – PaulF Jan 17 '18 at 18:39
  • Ill try that Sir. Thanks. – Device Developer Jan 18 '18 at 03:01

0 Answers0