Bacnet

Hello,

I am newer to Arduino; however, I have started creating a BacNet device. I have been able to define my UDP port to 47808 and I am able to have the Arduino respond to a who-is command. I am trying to create a device object with the correct properties associated with it. My problem is I need the data that is coming into my arduino to convert from BIN to HEX and stored in an array. At this time I can write if else statements that compare the array to know HEX code. If i ask to see the present value of a property within an object I'll send that HEX to the arduino, the arduino will see the HEX and interpert it and respond accordingly. Does anyone have information on how this works? I have been able to get my device to respond to who is, but I need to get it so I have a device object so I can install the device into a BAS system.

My problem is I need the data that is coming into my arduino to convert from BIN to HEX and stored in an array.

All digital data are binary. There are various representations of binary data, such as BIN, HEX and decimal, that are used to make the data readable to humans. Please rephrase the question.

I am doing something very incorrect and I am not sure what it is... My array that I created called byte My Array works really well. The hex code is sent out correctly etc... I have two problems. I cannot serial print the incoming packet to see how arduino is interperting it. I know what hex code I am sending to the arduino but I am not sure how the arduino is seeing it... Please set me strait because I am doing something competly incorrect.

#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h.> //load Ethernet librart
#include <EthernetUdp.h> // UDP library

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; //Defines MAC address
IPAddress ip(192,168,0,100);
unsigned int localPort = 47808; // UDP Port
EthernetUDP Udp; // An EthernetUDP instance to let us send and receive packets over UDP

//packet info
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
String datReq; //string for our data
int packetSize; // size of the packet
byte myArray[] = {0x81,0x0A,0x00,0x17,0x01,0x20,0xFF,0xFF,0x00,0xFF,0x10,0x00,0xC4,0x02,0x00,0x30,0x39,0x21,0x32,0x91,0x03,0x21,0x02};

void setup()

{
// start the Ethernet and UDP:
Ethernet.begin(mac, ip); // start ethernet
Udp.begin(localPort); //start UDP
Serial.begin(9600); //start serial connection
delay(1500); //delay

}

void loop() {
packetSize=Udp.parsePacket(); //reads packet size only not the info

if(packetSize>0) {
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); //read the incoming packet from client and write it to packetBuffer char
//String datReq(packetBuffer); //convert char array packetBuffer into a string called dataReq

if(packetBuffer == "C0A800FFBAC0810A000C0120FFFF00FF1008") //Who-is
Serial.println("Yes");
Serial.println(packetBuffer);
Udp.beginPacket(Udp.remoteIP(),Udp.remotePort()); //starts sending a packet to the IP and Port one was recieved from
Udp.write(myArray,23); //I-am hex code
Udp.endPacket(); //Ends the packet
delay(30);
}
else
{
// Serial.println("no");
}

}

Use strcmp() to compare character arrays, not ==.
Improper or missing curly braces.
Use code tags to post code.

Hmm.. instead of storing it as a char I tried a byte.. Originally I would sent it a hex code and it saved it in the buffer as 220.. I sent the same hex code two more times and it saved it as different numbers.. Let me as you this. If I send a hex value from a piece of software to the arduino over UDP and I want to compare the HEX code the hex code with another hex code what would be the best data type to use? What would be the best way of doing this? Thank you for all the help :slight_smile: I am a noobie at the arduinos :slight_smile:

You really need to learn that values stored in a computer are BINARY. You are in way over your head unless you spend some time reading and learning the following very basic and important points:

0xABCD is a HEXADECIMAL representation of a binary value. It is equal to 43981 in DECIMAL representation.

"ABCD" is a constant BINARY character array (often called a C string), consisting of 5 bytes that represent the ASCII characters A,B,C,D, terminated by a zero byte. It is the same a defining

char packet[]={'A','B','C','D',0};

You do not send "hex codes". You send binary values, possibly stored in a character array.

You can compare an array of zero-terminated binary values with another zero-terminated binary array, possible defined by their equivalent HEX representation, using the function strcmp(). Any good C/C++ reference will have examples of how to use strcmp().

time to hit the books :slight_smile: I do understand that all the communication is binary; however, I am trying to take that incoming binary and display it in a way I can read it and compare it. My background is in HVAC mechanical design and programming G code for CNCs. I haven't messed around with programming a ton so thank you for pointing me in the right direction.

Have you had any success with your project? I would also be interested to make a BACnet/IP device with Arduino.

BR,
Samppa