I’m trying to make my Mega 2560 send and receive data from a PLC. PLC’s code I know well, but not C so much. I have a bunch of bits in the plc that I need to write to. It needs to be formatted so that each nibble of data represents 4 bits but gets send over in 2 byte chunks with the high byte first then the low byte. The code below (written to send 6 bytes) works until I turn on that 4th bit and then it craps out because it sends an (0f) rather than a 3430 . I have been staring at this for 2 days, and searching for the solution. I’m not saying its not here, if it is I just don’t understand it. Any insight would be MUCH appreciated, I would rather figure it out on my own unfortunately time has run out.
The actual data format to send is 02|42|32303030|3031|3030|0D = (The pipes are not needed just to make it easier to see)
This would send it STX|A|2000|01|00|CR (A = Write Command |2000 = address to read| 01 # of Bytes| 00 = Value to write| Carriage return)
I attached 2 pics of the format as well.
/*
MATRIX KEYPAD CONVERTER
***************************************************************************************************
Note:
The Inputs and Outputs are configured for a Custom Keypad with 40 LED's in a 5x8 matrix
SET GOT to microcomputer driver 9600, N, 8, 1
Must Jump Pins 4-6, 7-8 on the GOT serial side.
Must use TTL to 232 Converter on Pins 2 & 3
****************************************************************************************************/
//general constants and variables
//variables for Writing Bits M0 - M47
String sendData = "";
String readData = "";
const char STX = 2;
char command[2] = "B"; // Write Virtual M Bits
char command2[2] = "A"; //Read Virtual M Bits
String stNo = "00";
String address = "2000"; //M0 of Virtual Device (Buttons)
String raddress = "2100"; // M100 of Virtual Device (LED's)
String count = "06"; // How many Bytes
byte n[] = {
0,0,0,0,0,0,0,0,0,0,0,0};
char inChar;
char response[100] = "";
boolean m[50]; //Set up Bits for Keys
boolean LED[50]; //Set up Bits for LED's
int i = 0;
/***********************************************************************************************************************************************************************************
* The setup routine runs on power cycle or when you press reset:
*
***********************************************************************************************************************************************************************************/
void setup()
{
// initialize serial communication at 9600 bits per second:
Serial1.begin(9600);
Serial.begin(9600);
// Just set values for Testing
m[0] = 1;
m[1] = 1;
m[2] = 1;
m[3] = 0; //Ugh turn this bit on and get a Format Error
}
/*********************************************************************************************************************************************************************************
* The loop routine runs over and over again forever:
*
**********************************************************************************************************************************************************************************/
void loop()
{
for (i=0; i < 4; ++i){
bitWrite(n[0],i, m[i]); //Move bits into Bytes
bitWrite(n[1],i, m[i+4]);
bitWrite(n[2],i, m[i+8]);
bitWrite(n[3],i, m[i+12]);
bitWrite(n[4],i, m[i+16]);
bitWrite(n[5],i, m[i+20]);
bitWrite(n[6],i, m[i+24]);
bitWrite(n[7],i, m[i+28]);
bitWrite(n[8],i, m[i+32]);
bitWrite(n[9],i, m[i+36]);
bitWrite(n[10],i, m[i+40]);
bitWrite(n[11],i, m[i+44]);
}
SendKeysCommand(); //Write Keys to GOT
delay(1000);
ReadData();
while ((Serial.available() > 0) && i <99) //read response data. Currently ignored
{
inChar = (char)Serial.read();
response[i] = inChar;
i += 1;
}
CommTest();
}
/*******************************************************************************************************************************************************************************
* SUB ROUTINES
********************************************************************************************************************************************************************************/
void SendKeysCommand() //send Key State to GOT through Serial 1
{
sendData = (command) + (stNo) + (address) + (count);
Serial1.write (STX);
Serial1.print (sendData);
Serial1.print (n[1]); //Bytes must be swapped High Byte then Low Byte
Serial1.print (n[0]);
Serial1.print (n[3]);
Serial1.print (n[2]);
Serial1.print (n[5]);
Serial1.print (n[4]);
Serial1.print (n[7]);
Serial1.print (n[6]);
Serial1.print (n[9]);
Serial1.print (n[8]);
Serial1.print (n[11]);
Serial1.println (n[10]);
}
void ReadData() //Read Data from GOT through Serial 1
{
readData = (command2) + (stNo) + (raddress) + (count);
Serial1.write (STX);
Serial1.println (readData);
}
void CommTest() //Send Data out Serial port to monitor
{
Serial.print ("DATA SENT: ");
sendData = (command) + (stNo) + (address) + (count);
Serial.write (STX);
Serial.print (sendData);
Serial.print (n[1]); //Bytes must be swapped High Byte then Low Byte
Serial.print (n[0]);
Serial.print (n[3]);
Serial.print (n[2]);
Serial.print (n[5]);
Serial.print (n[4]);
Serial.print (n[7]);
Serial.print (n[6]);
Serial.print (n[9]);
Serial.print (n[8]);
Serial.print (n[11]);
Serial.println (n[10]);
Serial.print ("Received: ");
Serial.println (response);
}
The Actual Error
I get is on the Controller Side. It (PLC) tells me that the message is not properly formatted. The Mega happily goes along sending.