将十六进制数据传递到串行设备的串行端口 [英] Passing Hex Data to Serial Port for a card dispenser Device
问题描述
美好的一天,
我一直在做这个项目很长一段时间。我也用过Php这样做却失败了。所以我切换到C#。
我一直想做的是将一个命令传递给我的设备,其中命令是42(42H,H代表十六进制)
我写了一段代码,但它没有做任何事情。我的设备根本没有配药。
我有一个工具来测试我的串口是否实际传递了由我提供给我的数据朋友。
通过使用该工具,它会分发卡,但如果我在C#Visual Studio上执行,则不会。
如果我按卡出来,则会显示[0x42]。
有人可以帮我解决这个或任何建议吗?
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
名称空间Card_Reader
{
公共部分类Form1:表格
{
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();
}
}
}
也许它应该是
'serialPort1.Write(new byte [] {0x42} ,0,1)'。
您知道设备是否需要包含42H的单字节或两个字符'4'和'2'吗?
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(); } } }
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’?
这篇关于将十六进制数据传递到串行设备的串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!