i want to receive data from serial COM port and show received data in TextBox. As i'm beginner, i can't understand why it's not working, seems SerialDataReceivedEventArgs e not happening.
Have you verified using PUTTY or some other terminal type program (such as hyperterm) that you can see some interaction on the port.
You have no setup code, so the SerialPort object should be defaulting to COM1, 9600 baud, 8n1 (8 data bits, parity none, 1 stop bit) and Handshake = none.
Do you know if these are correct for your setup?
Sometimes you might have to set the DtrEnable to True, but I haven't done a lot of serial in .Net (a little in VB.Net) (mostly in VB6 years ago), and none in C#, so can't be definitive.
I almost always want to use a terminal program like PUTTY first to verify that the hardware and serial protocols are known to work, then setup the software to use the verified port and settings, and work on issues if any.
If you don't verify the hardware connection first (perhaps the cable is a cross-over, or needs to be a cross-over), you're just shooting in the dark when things don't work.
You can't fix a hardware connection problem in your software, so check that first, then work on your code.
I tried to use Rs232 terminals to send/receive commands, so i know that device works and communication cable connected correctly.
Device connected to the COM port has been developed by me some years ago, there is also LCD display, so i see that it receive correct commands and also it sends response to each received command. Also i have another programm made not by me, this works perfect with my device.
So i'm 100% sure that problem is in my code, but i don't understand what exactly is wrong.
I made serial port settings by clicking on serial port feature in VB, so settings (such as baudrate, COM number, RTS and DTR) must be correct. Do you think i must add port settings in my code?
I wouldn't think you would have to set the settings in code since you've preset them in the IDE.
Unfortunately, I don't have time to play with this at the moment.
Also, looking back at code with the little bit of serial I've done in .Net, it was transmit only. I didn't need or expect any data coming back from the device I was talking to.
Did a lot more two way comms in VB6, but that wouldn't be of any use here.
Doesn't sound like you're exactly a beginner, but perhaps just a beginner when it comes to C# and Serial comms, but I'm sorry I can't participate further at the moment.
Well, i have more then 10 years experience in hardware development, but i'm total beginner in application development, so here i have lot of things to learn...
Thanks Passel for your attention, hope i will get some suggestions from someone else here.
I'm not that experienced in C#, but I'm not sure how you can tell by looking at the code in form1.cs whether a handler has been "bound" to the event or not. I put the same code in a project and assigned event handlers for the two buttons and the serialPort1.DataReceived, and there is no indication of that in the form1.cs file.
But you can see from the attached clip from the IDE that the function serialPort1_DataReceived is associated with the DataReceived event. That association obviously must be detailed in other files in C#.
I'm not that experienced in C#, but I'm not sure how you can tell by looking at the code in form1.cs whether a handler has been "bound" to the event or not. I put the same code in a project and assigned event handlers for the two buttons and the serialPort1.DataReceived, and there is no indication of that in the form1.cs file.
But you can see from the attached clip from the IDE that the function serialPort1_DataReceived is associated with the DataReceived event. That association obviously must be detailed in other files in C#.
C# has no equivalent to WithEvents and Handles from VB. C# only has one way to attach a method to an event and it is equivalent to AddHandler from VB. The statements that attach the event handlers are in the designer code file, just as they are for VB if you select not to create a member variable for a control or component that you add in the designer. In the case you show, there would be a statement like this:
csharp Code:
this.serialPort1.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
In VB, if you set GenerateMember to False for the SerialPort component in the designer then you'll get this corresponding line in the designer code file:
Note that, while probably not many people do, you should be setting GenerateMember to False if you don't intend to access that component via a member variable in code.