Problem in Reading from serial port
-
hello all i treid the code below but nothing happens.... //in button write serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.Open(); serialPort1.Write("A"); serialPort1.Close(); // in Button Read serialPort1.Open(); textBox1.Text = serialPort1.ReadLine();
TJS4u wrote:
i treid the code below but nothing happens....
That's incorrect, as something did happen. Either you didn't see it happen, or you didn't understand what happens. The serial port doesn't "remember" that you sent an "A", since there is no memory attached to the serial port. Did you get an exception, or did the serial-port just 'eat' the information you sent? Same problem with reading from the serial port. If there isn't anything attached and sending messages, then there won't be anything to read from the port. Your best option would be going out to buy a "null-modem cable", or something that can echo the signals from the serial port. I wish you lots of luck, as you are going to need it :(
-
hello Simon Stevens my requirement is to connect a microcontrolelr to the serial port . i tried this to see if data is in the buffer ... then it can't be read back Thanks
You need to read your micro controllers docs.
Simon
-
TJS4u wrote:
i treid the code below but nothing happens....
That's incorrect, as something did happen. Either you didn't see it happen, or you didn't understand what happens. The serial port doesn't "remember" that you sent an "A", since there is no memory attached to the serial port. Did you get an exception, or did the serial-port just 'eat' the information you sent? Same problem with reading from the serial port. If there isn't anything attached and sending messages, then there won't be anything to read from the port. Your best option would be going out to buy a "null-modem cable", or something that can echo the signals from the serial port. I wish you lots of luck, as you are going to need it :(
hello Eddy Vluggen i am new to C#.. so asking such questions... the code is executing and when it enters the line string line=................... the system hangs wht to do next How can i be sure that the port is enabled and data has been send to the port... OR if i connect a device will that respond to the code i have written i await u r suggestions Thanks
-
You need to read your micro controllers docs.
Simon
hello Simon Stevens first i need to download some data to the microcontroller from my application ... the microcontroller is connected to the Serial port. When the Microcontroller receives the data it executes its and it returns some data.. at this time i expect this data be in the port and need to read ir from the serial port i await for u r suggestions on this Thanks
-
hello Eddy Vluggen i am new to C#.. so asking such questions... the code is executing and when it enters the line string line=................... the system hangs wht to do next How can i be sure that the port is enabled and data has been send to the port... OR if i connect a device will that respond to the code i have written i await u r suggestions Thanks
TJS4u wrote:
the system hangs
No, it's not hanging, but waiting. Waiting for someone to send a carriage-return + linefeed to the serial port. In other words, the system is reading from the serial port, and will continue reading, until someone sends an "enter" to the serial port.
TJS4u wrote:
How can i be sure that the port is enabled and data has been send to the port...
Make yourself a cable that echo's everything.
TJS4u wrote:
if i connect a device will that respond to the code i have written
The device doesn't know about your code. Your code will have to speak to the device in a way that the device itself understands. Various devices use the serial port in different ways. Your micrologger will have some examples on this.
-
TJS4u wrote:
the system hangs
No, it's not hanging, but waiting. Waiting for someone to send a carriage-return + linefeed to the serial port. In other words, the system is reading from the serial port, and will continue reading, until someone sends an "enter" to the serial port.
TJS4u wrote:
How can i be sure that the port is enabled and data has been send to the port...
Make yourself a cable that echo's everything.
TJS4u wrote:
if i connect a device will that respond to the code i have written
The device doesn't know about your code. Your code will have to speak to the device in a way that the device itself understands. Various devices use the serial port in different ways. Your micrologger will have some examples on this.
hello Eddy Vluggen Thanks for u r reply still some things makes confusing/// i have written string line = serialPort1.ReadLine(); At this point will the variable line stores the value from the port??? if a device is connected and it has some thing to return Thanks
-
hello all i treid the code below but nothing happens.... //in button write serialPort1.PortName = "COM1"; serialPort1.BaudRate = 9600; serialPort1.DataBits = 8; serialPort1.Parity = Parity.None; serialPort1.StopBits = StopBits.One; serialPort1.Open(); serialPort1.Write("A"); serialPort1.Close(); // in Button Read serialPort1.Open(); textBox1.Text = serialPort1.ReadLine();
Hi, It is my impression this project is way beyond your current reach. Nevertheless here are some facts: 1. transmit and receive channels are (almost) completely independent: there is a wire in each direction, there is separate electronics, separate data buffers, and different methods to call. The only link exists inside the serial driver when software dataflow is used (XON/XOFF). 2. whatever gets sent through a serial port is gone in a couple of micro- or milliseconds, you can't read it back; you can (if things go well) find the data at the target hardware after that delay. Some suggestions: 1. if all is new to you (C#, serial ports, microcontroller) you have zero probability to get it working by just setting it all up and hope for the best. You MUST go step by step, debug each step completely before taking the next step. 2. The first step should be one of these (in increasing level of difficulty): 2a. use two PC's (I'll call them master and target) connect them through a serial cable WITH A NULL MODEM (which switches transmit and receive lines); run a terminal emulator (HyperTerminal, whatever) on the target PC, so you make it emulate your microcontroller by reading the emulator display and typing keys when appropriate. And run your own code on the master PC. Now you can first debug transmission: your program writing, the emulator showing what comes in; then you can tackle the receive side (with buffers, events, etc). 2b. you could try the same on a single PC (using two serial ports, connected with again a NULL MODEM); it is much less comfortable though. 2c. you don't even need real serial ports and a null modem cable, you can use a VIRTUAL NULL MODEM (Google for com0com). 3. If all this works, you could try to use the microcontroller. If that one needs a program download, you'd better won't need its serial port for that, since it has not been debugged yet, so it will not work initially. Good luck.
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused:
-
hello Eddy Vluggen Thanks for u r reply still some things makes confusing/// i have written string line = serialPort1.ReadLine(); At this point will the variable line stores the value from the port??? if a device is connected and it has some thing to return Thanks
TJS4u wrote:
string line = serialPort1.ReadLine(); At this point will the variable line stores the value from the port???
At that point the computer will *wait*, until a device sends an "enter". It will keep stuffing all data into the "line" variable, until it receives the enter-character. Does your logger send a return-character when it is done sending data?
-
TJS4u wrote:
string line = serialPort1.ReadLine(); At this point will the variable line stores the value from the port???
At that point the computer will *wait*, until a device sends an "enter". It will keep stuffing all data into the "line" variable, until it receives the enter-character. Does your logger send a return-character when it is done sending data?
-
hello Eddy the microcontoller will get the code which is downloaded to it thru the port and it will execute after execution the result is send to the application thru the serial port from the microcontroller Thanks
TJS4u wrote:
the microcontoller will get the code which is downloaded to it thru the port and it will execute
You're sending "code" to the controller? I'm afraid I don't understand what you're saying here, can you give an example of the codes that you wanna send?
TJS4u wrote:
after execution the result is send to the application thru the serial port from the microcontroller
..and the ReadLine() method will "hang" until the microcontroller sends this "enter"-character. I bet that the logger doesn't send it :) It works if you remove that line, doesn't it?