| | |
Sending a string/byte to the usb port
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2007
Posts: 6
Reputation:
Solved Threads: 0
The following bit of code is to send a string of data to the usb port. How can I modify the code so I only send one byte of data to the usb port instead of a string?
private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
{
this.lb_message.Items.Add("My device was found");
//setting string form for sending data
string text = "";
for (int i = 0; i < this.usb.SpecifiedDevice.OutputReportLength - 1; i++)
{
text += "000 ";
}
this.tb_send.Text = text;
}
private void btn_send_Click(object sender, EventArgs e)
{
try
{
string text = this.tb_send.Text + " ";
text.Trim();
string[] arrText = text.Split(' ');
byte[] data = new byte[arrText.Length];
for (int i = 0; i < arrText.Length; i++)
{
if (arrText[i] != "")
{
int value = Int32.Parse(arrText[i], System.Globalization.NumberStyles.Number);
data[i] = (byte)Convert.ToByte(value);
}
}
if (this.usb.SpecifiedDevice != null)
{
this.usb.SpecifiedDevice.SendData(data);
}
else
{
MessageBox.Show("Sorry but your device is not present. Plug it in!! ");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void usb_OnSpecifiedDeviceArrived(object sender, EventArgs e)
{
this.lb_message.Items.Add("My device was found");
//setting string form for sending data
string text = "";
for (int i = 0; i < this.usb.SpecifiedDevice.OutputReportLength - 1; i++)
{
text += "000 ";
}
this.tb_send.Text = text;
}
private void btn_send_Click(object sender, EventArgs e)
{
try
{
string text = this.tb_send.Text + " ";
text.Trim();
string[] arrText = text.Split(' ');
byte[] data = new byte[arrText.Length];
for (int i = 0; i < arrText.Length; i++)
{
if (arrText[i] != "")
{
int value = Int32.Parse(arrText[i], System.Globalization.NumberStyles.Number);
data[i] = (byte)Convert.ToByte(value);
}
}
if (this.usb.SpecifiedDevice != null)
{
this.usb.SpecifiedDevice.SendData(data);
}
else
{
MessageBox.Show("Sorry but your device is not present. Plug it in!! ");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
![]() |
Similar Threads
- problem with USB port (C#)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
- Printer does not work w/USB port (Windows 95 / 98 / Me)
- USB port stops responding (Storage)
- My DSL connects to USB port, powerbook is not recognizing (OS X)
- Reading from USB port using php? (PHP)
- USB port not responding on multi-accounts (Windows NT / 2000 / XP)
- Problems Printing from MSDos w/USB Port (Windows NT / 2000 / XP)
- usb port (Windows 95 / 98 / Me)
Other Threads in the C# Forum
- Previous Thread: Alt-ctrl-del in vista
- Next Thread: dictionary
Views: 7159 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gcd gdi+ http httpwebrequest image index input java list listbox listener login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml





