Putc function equivalent in Arduino

Hi,

I have a system composed of two Arduino Mega boards connected through i2C (it works). The communicate with each other using Serial.begin (9600); (and the required Wire.h functions).

Recently, I've attached a PIC device through serial connection to the slave device. I have the C code to get a given information from that device, however, I need to use it with Arduino.

The C code uses the function "putc" to send bytes to the PIC device. Is Serial.write() the Arduino equivalent? Which function should I use to get and print the information?

I've checked the Serial.available() when I send multiple Serial.write information but it is empty. Am I sending the information correctly?

I insert my code:

const int RX_RF = 7; // RX from the Micro RF
const int TX_RF = 6; // TX from the Micro RF
int delay_ = 700;
int x; int flag = 0; int y;
const byte MY_ADDRESS = 42;
const byte MASTER_ADDRESS = 25;
//char PIN[] = "pin";
int pin = 0;
uint8_t Buffer[15];
uint8_t BufferStart[4];
//int Buffer[15];
int phase = 0;
char ini;
int p = 1;
int tx = 0;
int inByte = 0;
char incomingByte = 0;
int address = 0;
byte value;



#include <Wire.h>

void setup() {

  Serial.begin (9600); 
  pinMode(RX_RF, OUTPUT); digitalWrite(RX_RF, HIGH);
  pinMode(TX_RF, INPUT_PULLUP);
}

void loop() {

 TxRx((uint8_t) 05); // <- HERE I TRIGGER THE SENDING FUNCTION
 
}


#define RXBUFFSIZE 0x3f
typedef struct 
{
  byte Len;
  byte Chars[RXBUFFSIZE];
} TMsgTxCom;
TMsgTxCom MsgTxCom;

typedef struct
{
  uint8_t ValRSSI;
  uint8_t ValAFC;
  byte ValIF;
} TCfgCC1020;
TCfgCC1020 CfgCC1020;
//-------------------------------------------------------------------------------------------------
typedef struct
{
  uint8_t Modello;
  uint8_t MayorRelease;
  uint8_t Release;
  uint8_t Indirizzo1;
  uint8_t Indirizzo2;
  uint8_t Indirizzo3;
  uint8_t IndirizzoRis1;
  uint8_t IndirizzoRis2;
  uint8_t IndirizzoRis3;
  uint8_t Banda;
  uint8_t CanaleFrequenza;
} TCfgRxVer;
TCfgRxVer CfgRxVer;


#define STX    2
#define ETX   3
#define DLE   16
uint8_t MsgRxCom[RXBUFFSIZE], carcom, statocom, cntcom, lencom, checkcom, TimerRxCom, dleflag, MsgComOk;
uint8_t MaskUsciteA, MaskUsciteB, MaskUsciteC, MaskUsciteD, SeqMaskUscite;
uint8_t flagTastoPremuto = 0;

uint8_t putData(uint8_t data)
{
  uint8_t c = 0;
  if ((data == STX) || (data == ETX) || (data == DLE))
  {
    Serial.write(DLE);
    c = DLE;
  }
  Serial.write(data);
  return c + data;
}
//-------------------------------------------------------------------------------------------------
void SendMessage(void)
{
  uint8_t i, c;
  Serial.write(ETX);
  Serial.write(STX);
  c = STX;
  c += putData(MsgTxCom.Len);  
  for (i = 0; i < MsgTxCom.Len; i++)
  {    
    c += putData(MsgTxCom.Chars[i]);    
  }
  Serial.write((uint8_t)(256 - c));
  Serial.write(ETX);
  Serial.write(ETX);
  if (Serial.available() > 0) {    
    Serial.print("Size bytes ");
    int inByte=Serial.available();
    Serial.println(inByte);
    

  }
 

}
//-------------------------------------------------------------------------------------------------
void TxRx(uint8_t c)
{
  MsgTxCom.Chars[0] = c;
  MsgTxCom.Len = 1;
  SendMessage();
}

Thanks

Your Post is rather mixed up.

I2C and Serial are two different things.

I would expect Serial.write() to do the same thing as putc() - but without seeing the code that has putc() it is difficult to know. What happened when you tried it?

Why would you expect Serial.write() to affect Serial.available() ? The first is for sending data, the second is for receiving data. Again, without seeing your program ...

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Thanks @Robin2.

Here the code with the original putc

//-------------------------------------------------------------------------------------------------
int8 putDato(int8 dato)
{
	int8 c=0;
	if((dato == STX) || (dato == ETX) || (dato == DLE))
	{
		putc(DLE);
		c=DLE;
	}
	putc(dato);
	return c + dato;
}
//-------------------------------------------------------------------------------------------------
void SendMessage(void)
{
	int8 i,c;
	putc(ETX);
	putc(STX);
	c=STX;
	c+=putDato(MsgTxCom.Len); 
	for(i=0;i<MsgTxCom.Len;i++)
	{
		 c+=putDato(MsgTxCom.Chars[i]);
	}	 
	putc((int8)(256-c));
	putc(ETX);
	putc(ETX);
}
//-------------------------------------------------------------------------------------------------
void TxRx(int8 c)
{
	MsgTxCom.Chars[0] = c;
	MsgTxCom.Len = 1;
	SendMessage();
}

I'm aware that i2C and Serial are two different things. I just was wondering if one implementation (or way of communicating) could be affecting the other. For sending data slave->master through i2C I use Wire.beginTransmission, Wire.write and Wire.endTransmission. I don't know if Serial.write() is the correct function. How can I indicate the digital pin to transmit the data? I have tried to send all those bytes using Serial.write but I don't receive anything back. Is Serial.available() the function to check incoming bytes or should I use a different one? I've tried many different options without exit.

REM14:
I don't know if Serial.write() is the correct function. How can I indicate the digital pin to transmit the data? I have tried to send all those bytes using Serial.write but I don't receive anything back. Is Serial.available() the function to check incoming bytes or should I use a different one? I've tried many different options without exit.

There is obviously a tonne of stuff in your head that you have not shared with us.

Tell us exactly what you want to achieve - for the moment, never mind how it might be done.

If it depends on things being connected to your Arduino please post a photo of a simple pencil drawing showing all the connections. See this Simple Image Guide

...R

@Robin2 I attached a simple scheme of what I have. On the left the Arduino Mega board where the digital PIN 7 transmits commands to the serial device (this device is a PIC18F8722 microcontroller) and the digital PIN 6 receives the data from the serial device.

Arduino should communicate with the serial device using "frames" of data. In C, the type of data is formatted as int8. As I find out, maybe I'm wrong, that its conversion in Arduino is uint8_t. One example of data frame is this one:

uint8_t sendByte[8];
sendByte[0] = (uint8_t)ETX;
    sendByte[1] = (uint8_t)STX;
    sendByte[2] = (uint8_t)1; //sendByte[2]= (uint8_t)1;
    sendByte[3] = (uint8_t)DLE;
    sendByte[4] = (uint8_t)5; //sendByte[4]= (uint8_t)5;
    sendByte[5] = (uint8_t)(256 - 5); //sendByte[5]= (uint8_t)(256-5);
    sendByte[6] = (uint8_t)ETX;
    sendByte[7] = (uint8_t)ETX;    
    Serial.write(sendByte, sizeof(sendByte));

The problem is that when I send this information to the serial device, it does not respond. It should respond some specific data. I've tried using the if(Serial.available()) without exit.

When I'm checking the messages through the Serial Monitor, I receive this information ⸮. My questions: 1) When I'm doing Serial.write with the Serial Monitor opened, this transmitted information goes to the "screen" or to the serial device? 2) Is uint8_t equivalent to int8 (in C)? Should I use Serial.read() to receive the information from the serial device?

Thank you

You have on pins 14 to 19 three hardware serial interfaces (labeled TX1/RX1 to TX3/RX3) but you're connecting to pins 6/7? How does that make sense to you?

REM14:
@Robin2 I attached a simple scheme of what I have

Please make your image visible in your Post. I already gave you a link to the instructions.

...R

Thank pylon. I didn't know that I had to use exactly those pins to communicate with Serial devices, that why that made sense to me then. I've changed the cables and the pin configuration. However, I'm getting the same result and no information is returned.

@Robin2, find the image as you described.

Any clue?

So you connected the third serial interface (computers count from 0). That means you have to use the Serial2 object instead of the Serial object => Serial2.write() or Serial2.print().

I can't tell from that Fritzing image what pins the wires are connected to. A simple pencil drawing is so much easier to read.

The Mega serial pins have (for example) TX3 and RX3 printed beside the pins that work with Serial3 etc

...R