I want to add the devices connected to the arduino in my project to my interface with c#, can you help me?
Welcome to the forum
What sort of interface is there between the Arduino and C#, which is(presumably running on a PC ?
I want to print the devices connected with arduino to the screen using visual studio and c#
Sorry, but that tells us nothing about the device interface or even if the interface exists, only that you have GUI on the PC
If you want to pass data between the PC and the Arduino for control and/or display then consider using a serial interface
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;
using System.Xml;
using System.Threading;
using System.Text.RegularExpressions;
using Microsoft.Win32;
namespace Staj_Arayüz_Projesi
{
public partial class Form1 : Form
{
String[] serialports = SerialPort.GetPortNames();
string com = null;
string VID = "0403";
string PID = "6001";
List<string> ComPortNames()
{
String pattern = String.Format("^VID_{0}.PID_{1}", VID, PID);
Regex _rx = new Regex(pattern, RegexOptions.IgnoreCase);
List<string> comports = new List<string>();
RegistryKey rk1 = Registry.LocalMachine;
RegistryKey rk2 = rk1.OpenSubKey("SYSTEM\\CurrentControlSet\\Enum");
foreach (String s3 in rk2.GetSubKeyNames())
{
RegistryKey rk3 = rk2.OpenSubKey(s3);
foreach (String s in rk3.GetSubKeyNames())
{
if (_rx.Match(s).Success)
{
RegistryKey rk4 = rk3.OpenSubKey(s);
foreach (String s2 in rk4.GetSubKeyNames())
{
RegistryKey rk5 = rk4.OpenSubKey(s2);
RegistryKey rk6 = rk5.OpenSubKey("Device Parameters");
comports.Add((string)rk6.GetValue("PortName"));
}
}
}
}
return comports;
}
private void SetColor (Color color)
{
vScrollBar1.Value=color.R;
vScrollBar2.Value=color.G;
vScrollBar3.Value=color.B;
serialPort1.Write(new[] { color.R, color.G, color.B }, 0, 3);
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
arduino_connection();
button5.BackColor = Color.Red;
if (serialPort1.IsOpen)
{
button5.BackColor = Color.Green;
}
}
private void arduino_connection()
{
List<string> names = ComPortNames();
if (names.Count > 0)
{
foreach (String s in SerialPort.GetPortNames())
{
if (names.Contains(s))
{
com = s;
}
}
}
if (com != null)
{
try
{
if (!serialPort1.IsOpen && com != "")
{
/* Seri Port Ayarları */
serialPort1.PortName = com;
serialPort1.BaudRate = 9600;
serialPort1.Parity = Parity.None;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Arduino");
}
}
else
MessageBox.Show("Arduino bulunamadı!", "Arduino");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
serialPort1.Close();
}
catch (Exception hata)
{
MessageBox.Show(hata.Message);
}
}
private void RDM_Read_button_Click(object sender, EventArgs e)
{
}
private void RDM_Write_button_Click(object sender, EventArgs e)
{
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar1.Value;
label11.Text = value.ToString();
SetColor(Color.FromArgb(vScrollBar1.Value, vScrollBar2.Value, vScrollBar3.Value));
}
private void vScrollBar2_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar2.Value;
label13.Text = value.ToString();
SetColor(Color.FromArgb(vScrollBar1.Value, vScrollBar2.Value, vScrollBar3.Value));
}
private void vScrollBar3_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar3.Value;
label15.Text = value.ToString();
SetColor(Color.FromArgb(vScrollBar1.Value, vScrollBar2.Value, vScrollBar3.Value));
}
private void vScrollBar4_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar4.Value;
label17.Text = value.ToString();
SetColor(Color.White);
}
private void vScrollBar5_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar5.Value;
label19.Text = value.ToString();
SetColor(Color.Yellow);
}
private void vScrollBar6_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar6.Value;
label21.Text = value.ToString();
SetColor(Color.Black);
}
private void vScrollBar7_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar7.Value;
label23.Text = value.ToString();
SetColor(Color.Purple);
}
private void vScrollBar8_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar8.Value;
label25.Text = value.ToString();
SetColor(Color.DeepPink);
}
private void vScrollBar9_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar9.Value;
label27.Text = value.ToString();
SetColor(Color.Brown);
}
private void vScrollBar10_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar10.Value;
label29.Text = value.ToString();
SetColor(Color.Orange);
}
private void vScrollBar11_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar11.Value;
label31.Text = value.ToString();
SetColor(Color.Cyan);
}
private void vScrollBar12_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar12.Value;
label33.Text = value.ToString();
SetColor(Color.Magenta);
}
private void vScrollBar13_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar13.Value;
label35.Text = value.ToString();
}
private void vScrollBar14_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar14.Value;
label37.Text = value.ToString();
}
private void vScrollBar15_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar15.Value;
label39.Text = value.ToString();
}
private void vScrollBar16_Scroll(object sender, ScrollEventArgs e)
{
int value = vScrollBar16.Value;
label41.Text = value.ToString();
}
private void DMX_Write_button_Click(object sender, EventArgs e)
{
}
private byte[] msg_to_dmx_master = new byte[4];
private void writeFirstSerial(byte label, int len, byte[] msg_data)
{
if (serialPort1.IsOpen)
{
msg_to_dmx_master[0] = 0x7E;
msg_to_dmx_master[1] = label;
msg_to_dmx_master[2] = (byte)((len >> 0) & 0xFF);
msg_to_dmx_master[3] = (byte)((len >> 8) & 0xFF);
serialPort1.Write(msg_to_dmx_master, 0, 4);
if (len > 0) serialPort1.Write(msg_data, 0, len);
msg_to_dmx_master[0] = 0xE7;
serialPort1.Write(msg_to_dmx_master, 0, 1);
}
}
private void discovery_button_Click(object sender, EventArgs e)
{
String uid_str = info_textBox.Text;
ulong uid = ulong.Parse(uid_str, System.Globalization.NumberStyles.HexNumber);
rdm_send_device_info_cmd(uid);
Thread.Sleep(15);
}
private byte[] rdm_packet_req = new byte[513];
private byte[] widget_unique_id = new byte[4];
private void rdm_send_device_info_cmd(ulong uid)
{
rdm_packet_req[0] = 0xCC;
rdm_packet_req[1] = 0x01;
rdm_packet_req[2] = 24;
rdm_packet_req[3] = (byte)((uid >> 40) & 0xFF);
rdm_packet_req[4] = (byte)((uid >> 32) & 0xFF);
rdm_packet_req[5] = (byte)((uid >> 24) & 0xFF);
rdm_packet_req[6] = (byte)((uid >> 16) & 0xFF);
rdm_packet_req[7] = (byte)((uid >> 8) & 0xFF);
rdm_packet_req[8] = (byte)((uid >> 0) & 0xFF);
ushort crc = 0;
for (int i = 0; i < 9; i++)
{
crc += rdm_packet_req[i];
}
writeFirstSerial(0x07, 26, rdm_packet_req);
}
private void identify_button_Click(object sender, EventArgs e)
{
}
}
}
The code block is like this, I'm trying to communicate between the pc and the device connected to the arduino.
Forget about doing anything complicated until you can pass a simple "HelloWorld" text string between the systems
Which Arduino board are you using ?
arduino leonardo
It's been a long time since I wrote hello word, can you take care of my problem?
void setup()
{
Serial1.begin(115200); //use pins 0 and 1
}
void loop()
{
Serial1.println("Hello World");
delay(1000);
}
For example, what you wrote has nothing to do with my problem. I don't think your knowledge level is enough anymore, but thank you for your help.
[image]
Perhaps if you explained the problem properly it might help
It seems that your knowledge level of how to post an image is not enough
okey
Are you going to give more details or must we continue to guess what you are doing and what you want to do and be accused of not having a sufficient "knowledge level" ?
Perhaps because you are asking a C# question in an Arduino forum? See post #1.
Does that pick up the Leonardo as a feasible port?
Below something that I would consider
If you want to query from the PC, you can send something like
<id,q,param>
- id; the device that is connected to the leonardo that you want to communicate with
- q; command to query the device identified by id
- param; the field that you want to query from the device, can be a numeric value or a text like temperature. humidity
At the Arduino side, you can parse the command; for guidance, you can look at Robin's Serial Input Basics - updated (example 3).
The reply from the Leonardo can be
<id,r,c,value>
- id; the device that is connected to the leonardo that the reply came from
- r; rply from the device identified by id
- c; reply code; 0 = OK, other value = error
- value; value of the requested field; not transmitted or empty on error
If the devices send data on their own to the Leonardo, you can e.g. use a capital 'R' instead of 'r'.
To set a parameter, you can use something like
<id,s,param=value>
- id; the device that is connected to the leonardo that you want to communicate with
- q; command to query the device identified by id
- param; parameter that you want to set on the device, can be a numeric value or a text like temperature. humidity
- value; the value to set the parameter to
The reply could be something like
<id,r,c>
- id; the device that is connected to the leonardo that the reply came from
- r; rply from the device identified by id
- c; reply code; 0 = OK, other value = error
Note on id:
You can use id 0 for communication with the Leonardo itself if needed, your devices would be 1, 2
Did you know that there is no way for an Arduino to know what devices are attached to it?
You might write some code on an Arduino that passes strings to C# with a list of names of devices you hope is attached to it. Or if you are clever write some code that scans the I2C bus to see what addresses are used.
But you can't know the part number of those devices unless your Arduino code knows what devices might use those addresses. Even then there might be other devices using the same addresses and the Arduino would never know.
The same goes for anything attached to the SPI bus. Devices might be chained so there is nothing you can do to exercise the SPI bus to see all the devices that might be capable of being addressed with it.
In short I think you are expecting too much the way of bureaucracy from an Arduino system.
Are you communicating with a FTDI device or several FTDI devices, I think you can only have one serial port open in your PC app. I did not see the DataReceived event handler in your code, maybe I missed it but I think it would be very helpful. If you need help with the DataReceived event handler I can do that. I would need to know what the received data packet looks like, ASCII text or binary data for example.
That would be a step back from the DOS days
You can only have one app at a time using a com port but you can have multiple open at the same time inthe same app.
