7 Replies - 8191 Views - Last Post: 05 March 2013 - 10:48 PM Rate Topic: -----

#1 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,355
  • Joined: 14-January 10

Sending and Receiving Via A Serial Port?

Posted 01 May 2012 - 12:33 AM

Hi,

I don't have a question about my code I just need help understanding how I can complete a particular task. Currently, I have a Relay Controller that I have connected to my computer. When I send a command (i.e. REL1.GET) over the serial port the relay controller automatically returns either a 1 or a 0. Naturally if it returns a 1 then the relay is on and if it returns a 0 then the relay is off. My understanding is that you can't read from the serial port at the same time that you send something. So how are you supposed to read that value?

Thanks in advance for any help given.

Is This A Good Question/Topic? 0
  • +

Replies To: Sending and Receiving Via A Serial Port?

#2 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Sending and Receiving Via A Serial Port?

Posted 01 May 2012 - 02:50 AM

By its definition, serial communication is a synchronous communication: first you send, then you receive. You will receive data on DataReceived event which is basically a listener.
IF you are curious to go deeper, this synchronous communication is done at hardware level where some pins work together to achive this:
CTS - Clear to send (pin 8)
RTS - Request to send (pin 7)
DTR - Data terminal ready(pin 4)
DSR - Data set ready(pin 6)
These pins are linked togheter(4-6 and 7-8) and,depending on the state of the transmission, they are set to 0(false) or 1(true) - 0 = no electrical charge, 1 = electrical charge. I will not explain further because the names are obivious and the internet has many details on this subject.

Conclusion: you don't have to worry about the reading and writing, define an event handler for DataReceived event.
Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Sending and Receiving Via A Serial Port?

Posted 01 May 2012 - 03:07 AM

There is also Type in .net that communicates with the Serial Port. IO.Ports.SerialPort
Was This Post Helpful? 0
  • +
  • -

#4 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,355
  • Joined: 14-January 10

Re: Sending and Receiving Via A Serial Port?

Posted 01 May 2012 - 10:32 AM

@Ionut, Yes I already have a dataReceived event handler that receives data over the Serial Port.

@AdamSpeight2008, I've been using the SerialPort class for almost a year now. I have experience with communicating over a serial port. I just don't have any experience with receiving data as soon as I send a command.

The point I'm trying to make is that I know how to send and receive over a serial port, but I can't do this:

sp.write("REL1.TOGGLE");
data = sp.readLine();



Because the data will have already been sent from the controller to the serial port.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2240
  • View blog
  • Posts: 9,411
  • Joined: 29-May 08

Re: Sending and Receiving Via A Serial Port?

Posted 01 May 2012 - 11:29 AM

The data/response will be in the buffer.
Was This Post Helpful? 0
  • +
  • -

#6 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Sending and Receiving Via A Serial Port?

Posted 01 May 2012 - 01:46 PM

If you have to resume an action depending on the value received from serial port(and not just displaying something), implement an event that will be raised in the DataReceived event handler, after you parse the response.
Was This Post Helpful? 0
  • +
  • -

#7 thasnimhamza  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-December 09

Re: Sending and Receiving Via A Serial Port?

Posted 05 March 2013 - 10:17 PM

Hi,
I also Have same problem.I used dataReceived event handler.even that I can not read value as soon as write to serial port. If you solved that problem can you help me?
Was This Post Helpful? 0
  • +
  • -

#8 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,355
  • Joined: 14-January 10

Re: Sending and Receiving Via A Serial Port?

Posted 05 March 2013 - 10:48 PM

Please start a new thread and show us what code you have so far and someone will be happy to assist you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1