phpoc_man
Published © GPL3+

A Single Arduino Uno with 14 RS-232 Ports

A single Arduino Uno/Mega can carry up to 14 RS-232 boards. Each board is distinguished by ID, which is set via a DIP switch.

BeginnerShowcase (no instructions)44,282
A Single Arduino Uno with 14 RS-232 Ports

Things used in this project

Story

Read more

Code

MultipleRS232

Arduino
#include <Phpoc.h>
#include <PhpocExpansion.h>

ExpansionSerial rs232_01(1);
ExpansionSerial rs232_02(2);
ExpansionSerial rs232_03(3);
ExpansionSerial rs232_04(4);
ExpansionSerial rs232_05(5);
ExpansionSerial rs232_06(6);
ExpansionSerial rs232_07(7);
ExpansionSerial rs232_08(8);
ExpansionSerial rs232_09(9);
ExpansionSerial rs232_10(10);
ExpansionSerial rs232_11(11);
ExpansionSerial rs232_12(12);
ExpansionSerial rs232_13(13);
ExpansionSerial rs232_14(14);

#define BUFFER_SIZE 100

byte serial_buf[BUFFER_SIZE];  // read buffer
int buf_len = 0;

void setup() {
    Serial.begin(115200);
    while(!Serial)
        ;
    
    Sppc.begin(PF_LOG_SPI | PF_LOG_NET);
    Expansion.begin();

	rs232_01.begin(F("115200N81N"));
	rs232_02.begin(F("115200N81N"));
	rs232_03.begin(F("115200N81N"));
	rs232_04.begin(F("115200N81N"));
	rs232_05.begin(F("115200N81N"));
	rs232_06.begin(F("115200N81N"));
	rs232_07.begin(F("115200N81N"));
	rs232_08.begin(F("115200N81N"));
	rs232_09.begin(F("115200N81N"));
	rs232_10.begin(F("115200N81N"));
	rs232_11.begin(F("115200N81N"));
	rs232_12.begin(F("115200N81N"));
	rs232_13.begin(F("115200N81N"));
	rs232_14.begin(F("115200N81N"));

}

void loop() {
	char byte;
	String expansionIdString = "";

	// Read data from RS-232 and print to Serial Monitor
	if(rs232_01.available()) {
		byte = rs232_01.read();
		expansionIdString = "01";
	} else if(rs232_02.available()) {
		byte = rs232_02.read();
		expansionIdString = "02";
	} else if(rs232_03.available()) {
		byte = rs232_03.read();
		expansionIdString = "03";
	} else if(rs232_04.available()) {
		byte = rs232_04.read();
		expansionIdString = "04";
	} else if(rs232_05.available()) {
		byte = rs232_05.read();
		expansionIdString = "05";
	} else if(rs232_06.available()) {
		byte = rs232_06.read();
		expansionIdString = "06";
	} else if(rs232_07.available()) {
		byte = rs232_07.read();
		expansionIdString = "07";
	} else if(rs232_08.available()) {
		byte = rs232_08.read();
		expansionIdString = "08";
	} else if(rs232_09.available()) {
		byte = rs232_09.read();
		expansionIdString = "09";
	} else if(rs232_10.available()) {
		byte = rs232_10.read();
		expansionIdString = "10";
	} else if(rs232_11.available()) {
		byte = rs232_11.read();
		expansionIdString = "11";
	} else if(rs232_12.available()) {
		byte = rs232_12.read();
		expansionIdString = "12";
	} else if(rs232_13.available()) {
		byte = rs232_13.read();
		expansionIdString = "13";
	} else if(rs232_14.available()) {
		byte = rs232_14.read();
		expansionIdString = "14";
	}

	if(expansionIdString != "")
	{
		Serial.print(expansionIdString.c_str());
		Serial.print(':');
		Serial.println(byte);
	}

	// Read data frame from Serial Monitor and send to RS-232. Frame delimiter is newline character
	if(Serial.available()) {
		byte = Serial.read();
		serial_buf[buf_len] = byte;
		buf_len++;

		if(byte == 10) { // 10 is newline character - Frame delimiter
			// Fram format: expansion_id + : + data + Frame delimiter
			// For example: 03:hello 03\n 
			// expansion_id is 03, data is "hello 03"
			char digit_st = serial_buf[0];
			char digit_nd = serial_buf[1];
			
			expansionIdString = String(digit_st) + String(digit_nd);
			int expansion_id = expansionIdString.toInt();

			for(int i = 0; i < buf_len; i++) {
				char buf_byte = serial_buf[i];
				if(expansion_id == 1)
					rs232_01.print(buf_byte);
				else if(expansion_id == 2)
					rs232_02.print(buf_byte);
				else if(expansion_id == 3)
					rs232_03.print(buf_byte);
				else if(expansion_id == 4)
					rs232_04.print(buf_byte);
				else if(expansion_id == 5)
					rs232_05.print(buf_byte);
				else if(expansion_id == 6)
					rs232_06.print(buf_byte);
				else if(expansion_id == 7)
					rs232_07.print(buf_byte);
				else if(expansion_id == 8)
					rs232_08.print(buf_byte);
				else if(expansion_id == 9)
					rs232_09.print(buf_byte);
				else if(expansion_id == 10)
					rs232_10.print(buf_byte);
				else if(expansion_id == 11)
					rs232_11.print(buf_byte);
				else if(expansion_id == 12)
					rs232_12.print(buf_byte);
				else if(expansion_id == 13)
					rs232_13.print(buf_byte);
				else if(expansion_id == 14)
					rs232_14.print(buf_byte);
			}

			buf_len = 0;
		}
	}
}

Credits

phpoc_man

phpoc_man

62 projects • 408 followers

Comments