sterretje:
Note in advance: I assume that you have a file that contains binary data, not the text 01000011111...
There is no function to read something from a PC. You will have to write (or find) and application. The below implements a serial reading for data from a PC. It reads 32 bytes from the serial port and stores it in RAM. For synchronisation, I've implemented a timeout (1 second, hard coded). It's basically Robin's example reworked.
// result codes for serial receive from PC
enum SERIALRESULT
{
COMPLETE, // complete
IN_PROGRESS, // waiting for more data
CRC_ERROR, // crc error
TIMEOUT_ERROR, // timeout error
};
#define BUFFERSIZE 32
uint8_t receivedBytesFromPC[BUFFERSIZE];
void setup()
{
Serial.begin(2000000);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
}
void loop()
{
SERIALRESULT sr = readSerial();
switch (sr)
{
case COMPLETE:
// crc check
// process the packet
for (uint8_t cnt = 0; cnt < BUFFERSIZE; cnt++)
{
if(receivedBytesFromPC[cnt] < 0x10)
{
Serial.print(0);
}
Serial.print(receivedBytesFromPC[cnt], HEX);
Serial.println();
}
// acknowledge to PC
Serial.print(sr);
break;
case IN_PROGRESS:
// nothing to do
break;
default:
// tell PC that something failed
Serial.print(sr);
break;
}
}
SERIALRESULT readSerial()
{
// index in array of received characters
static uint8_t index = 0;
static uint32_t startTime;
// while there is data
while (Serial.available() > 0)
{
// if it's first byte received
if (index == 0)
{
// set the time that this byte was received
startTime = millis();
}
// add to buffer
receivedBytesFromPC[index++] = Serial.read();
// if block received
if (index == BUFFERSIZE)
{
index = 0;
return COMPLETE;
}
}
// if receive in progress and timed out
if (index != 0 && (millis() - startTime > 1000))
{
index = 0;
return TIMEOUT_ERROR;
}
return IN_PROGRESS;
}
You can use to get ideas; tested on a Nano at 2MBit/second
2)
The principle is the same as for (1). Be aware that at 2MBit/s, sending 32 bytes to a PC takes 32 / 200,000 seconds. Don't try to send more than the software buffer size of the buffer in the Serial class (64 bytes) or the program will stall.
3)
Accessing an array element is shown in the above code.
4)
[Port manipulation](https://www.arduino.cc/en/Reference/PortManipulation); this is the fastest approach for toggling a pin. Note that it's for AVR chips (e.g. 328P on Uno, Nano).
5)
I think it's a single processor cycle (62.5 ns @16MHz) to set or clear a pin.
Im having a little bit of trouble understanding your code. As in what is it actually reading,
can port manipulation be used with this code
What pins am i limited to?
If i use the tx rx pins can i still use serial functions and port manipulation?