Main Site Documentation

Sending data to PC via RS232


#1

Hi,

I’m trying to send and received data between my FEZ Raptor and my PC but couldn’t get it going.

Here’s the code on my FEZ Raptor

private void SetupSerialPort()
{
	serialPortA.Initialize(baudRate: 9600, parity: Serial.SerialParity.None, stopBits: Serial.SerialStopBits.One, dataBits: 8, flowControl: Serial.HardwareFlowControl.NotRequired);
	serialPortA.serialPort.DataReceived += serialPort_DataReceived;
	serialPortA.serialPort.LineReceived += serialPort_LineReceived;
	serialPortA.serialPort.AutoReadLineEnabled = true;

	serialPortA.serialPort.Open();
	Debug.Print(serialPortA.serialPort.IsOpen.ToString());
	Debug.Print(serialPortA.serialPort.PortName);
	serialPortA.serialPort.WriteLine("adfadf");
}

void serialPort_LineReceived(Serial sender, string line)
{
	Debug.Print("WOW WOW WOW");
	Debug.Print(line);
}

void serialPort_DataReceived(Serial sender, SerialData data)
{
	Debug.Print("WOW WOW WOW");
	Debug.Print(data.ToString());
}

private void SendManually()
{
	// I'm sending this manually upon button press
	serialPortA.serialPort.WriteLine(_clickCounter.ToString());
}

and here’s the code running on my laptop

public bool SetupSerialPort(string serialPortName)
{
	var result = false;
	SerialPort = new SerialPort(serialPortName)
				 {
					 //BaudRate = 57600,
					 BaudRate = 9600,
					 DataBits = 8,
					 Parity = Parity.None,
					 StopBits = StopBits.One
				 };
	if (SerialPort.IsOpen)
	{
		SerialPort.Close();
	}

	SerialPort.Open();
	SerialPort.DataReceived += SerialPort_DataReceived;

	result = true;
	return result;
}

private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
	var result = _serialPort.ReadTo(((char)3).ToString(CultureInfo.InvariantCulture));
	ProcessData(result);
}

I’m using serial monitoring tool from HHD and the tool is registering the data sent out from my laptop but didn’t see any input from the FEZ Raptor. FEZ Raptor didn’t receive the data sent by my laptop too.

Any idea?

Thanks.


#2

Try registering for the serial events after the serial port is opened. There used to be a problem if the registration was done before the open. I don’t know if it was fixed.

also try using only the DataReceived event.


#3

Hi,
I think some more informations are needed:
Gadgeteer application ? RS232 Module used? Button module used? Contents of Program.Started method
Cheers
Roland


#4

Hi,

@ Mike, tried Open the serial port before wiring the event handler but still the same - not working.

@ RoSchmi, here’s the extra information requested.
RS232 Module - https://www.ghielectronics.com/catalog/product/355 (I got it in my OBD2 purchase)

The content of program started is already pasted in my first post.

I’m connecting the UART module to PC, i’m suspecting the voltage level being the culprit here. Anyone can help validate?

Saw in this doc - https://www.ghielectronics.com/docs/15/uart
[em]“When we need to interface a processor using UART to a computer’s serial port, we need to convert the UART TTL level to RS232 using some circuitry. For beginners or a quick hack, there are companies who provide ready circuits that convert RS232 to UART.”[/em]

Any help or confirmation that I can’t connect my RS232 module straight to PC? If yes, any adaptor module that i can use?


#5

Hi,
if you have a PC with regular Serial port (RS232) you can directly connect it to the Gadgeteer RS232 Module but you must use a Null-Modem cable (this is a cable where the TX and RX lines are crossed). The RS232 Module already makes the voltage conversion from TTL-Level to RS232 Level.
Sorry, I cannot see the ProgramStarted method in your post.


#6

For an understanding of why a null modem cable is needed, Google “rs232 DTE DCE”


#7

@ RoSchmi, you were right. I needed a Null Modem Cable.

All works well after I crossed the Tx and Rx data line with my custom wiring.

Thanks!


#8

Great, like those selfmade cables; make our lives a little bit more advanturous (esp. with higher voltages)