• (6 Pages)
  • +
  • « First
  • 3
  • 4
  • 5
  • 6

Serial Port Communication in C#

#61 Guest_Hal*


Reputation:

Posted 22 August 2010 - 09:07 PM

Hi,
I am new to serial port programming. I have RFID reader & sensor transmit data through serial port. when RFID reader reads rfid hanger & sensor reads an object simultaneously then sensor fails to transmit data. Below is my C# code:

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            StationDATA szCANData;
            
            //String szData ="";
            int nBytesToRead = serialPort1.BytesToRead;
            byte[] szBuff = new byte[nBytesToRead];
            //string szCOM= serialPort1.ReadExisting();
            //szData += serialPort1.ReadExisting();
            //ProcessBuffer(szData);
            serialPort1.Read(szBuff, 0, nBytesToRead);
            
            szCANData.StationID = szBuff[0];
            szCANData.StationCMD = szBuff[1];
            szCANData.StationMSG0 = szBuff[2];
            szCANData.StationMSG1 = szBuff[3];
            szCANData.StationMSG2 = szBuff[4];
            szCANData.StationMSG3 = szBuff[5];
            szCANData.StationMSG4 = szBuff[6];
            szCANData.StationMSG5 = szBuff[7];
            szCANData.StationMSG6 = szBuff[8];
            szCANData.StationMSG7 = szBuff[9];
            szCANData.EOD = szBuff[10];
}


PLease advice. Many thanks
Was This Post Helpful? 0

#62 Guest_JasonDiplomat*


Reputation:

Posted 06 October 2010 - 09:22 AM

BTW for those people trying to open COM10 or above, I would expect you have to do the "odd naming thing" mentioned in the MSDN description of CreateFile:

\\.\COM10 etc
Was This Post Helpful? 0

#63 Guest_Jim Beam*


Reputation:

Posted 14 October 2010 - 08:47 AM

Great code! However, there seems to be a problem with writing to the ports. Anything that I send to the port is simply echoed back to the console. I know my hardware works because I can manage it just fine with HyperTerminal but this code just echoes. In HyperTerminal I use 9600 baud, 8-N-1 with no flow control and I thought I made the proper adjustments in the code but it does nothing but echo back. Help.
Was This Post Helpful? 0

#64 Guest_Lucifer*


Reputation:

Posted 18 October 2010 - 03:19 AM

Hi guys!
I fond a problem whit not getting any data back from the modem (or whatever you are communicating with).

Quite a few complaints in this thread arise from that there is no data coming back from the serial port.
Hence sending seems OK, reciving is not working.

Except from the fix of setting DTR high after port open (suggested earlier) I also found that some modems are actually not returning any data until you send the \r sign (carriage return). Just sending a text string does not make the modem react.

To make the sample code work you should therefore change the actual send line into this:

comPort.Write(msg + "\r");


Also see all previous fixes in this thread.
Was This Post Helpful? 0

#65 Guest_carlos*


Reputation:

Posted 03 November 2010 - 10:28 AM

Definitely follow lucifer's suggestions. Ultimately, this change to the code fixed the problem with the no response data coming back from the device under test.


View PostLucifer, on 18 October 2010 - 02:19 AM, said:

Hi guys!
I fond a problem whit not getting any data back from the modem (or whatever you are communicating with).

Quite a few complaints in this thread arise from that there is no data coming back from the serial port.
Hence sending seems OK, reciving is not working.

Except from the fix of setting DTR high after port open (suggested earlier) I also found that some modems are actually not returning any data until you send the \r sign (carriage return). Just sending a text string does not make the modem react.

To make the sample code work you should therefore change the actual send line into this:

comPort.Write(msg + "\r");


Also see all previous fixes in this thread.

Was This Post Helpful? 0

#66 Guest_JohnCampbell*


Reputation:

Posted 08 November 2010 - 03:40 PM

Error!

El puerto COM1 no existe
Was This Post Helpful? 0

#67 Guest_Scott*


Reputation:

Posted 18 November 2010 - 08:35 AM

View PostLucifer, on 18 October 2010 - 02:19 AM, said:

Hi guys!
I fond a problem whit not getting any data back from the modem (or whatever you are communicating with).

Quite a few complaints in this thread arise from that there is no data coming back from the serial port.
Hence sending seems OK, reciving is not working.

Except from the fix of setting DTR high after port open (suggested earlier) I also found that some modems are actually not returning any data until you send the \r sign (carriage return). Just sending a text string does not make the modem react.

To make the sample code work you should therefore change the actual send line into this:

comPort.Write(msg + "\r");


Also see all previous fixes in this thread.


Good catch, sir! It is worth noting, however, that depending on the device involved, this too may require some tweaking. The floor scale I am doing com with required

comPort.Write(msg + "\r\n");


before responding correctly to serial commands.

Some of the other fixes mentioned in this forum(closePort method) aren't quite working for me verbatim either (VS 2008, win7-32bit) in the demo implementation, but work fine in my application. Kudos to you Psychocoder.
Was This Post Helpful? 0

#68 RNN14  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-November 10

Posted 25 November 2010 - 05:00 AM

I've been able to put some of the code from this tutorial into very good use, thanks for sharing!
Was This Post Helpful? 0
  • +
  • -

#69 Guest_Derin*


Reputation:

Posted 12 December 2010 - 04:11 PM

Refer http://www.derinpdavis.webs.com/
Was This Post Helpful? 0

#70 Guest_Xavier*


Reputation:

Posted 27 January 2011 - 10:13 AM

Quick question... howcome you don't open the port if you try to send Hex data but you do if you want to send a string?

#region WriteData
public void WriteData(string msg)
{
	switch (CurrentTransmissionType)
	{
		case TransmissionType.Text:
			//first make sure the port is open
			//if its not open then open it
			if (!(comPort.IsOpen == true)) comPort.Open();
			//send the message to the port
			comPort.Write(msg);
			//display the message
			DisplayData(MessageType.Outgoing, msg + "\n");
			break;					
		case TransmissionType.Hex:
			try
			{
				//convert the message to byte array
				byte[] newMsg = HexToByte(msg);
				//send the message to the port
				comPort.Write(newMsg,0,newMsg.Length);
				//convert back to hex and display
				DisplayData(MessageType.Outgoing, ByteToHex(newMsg) + "\n");
			}
			catch (FormatException ex)
			{
				//display error message
				DisplayData(MessageType.Error, ex.Message);
			}
			finally
			{
				_displaywindow.SelectAll();
			}
			break;				 
		default:
			//first make sure the port is open
			//if its not open then open it
			if (!(comPort.IsOpen == true)) comPort.Open();
			//send the message to the port
			comPort.Write(msg);
			//display the message
			DisplayData(MessageType.Outgoing, msg + "\n");
			break;	
			break;
	}
}
#endregion


Was This Post Helpful? 0

#71 rishikesh.kumar@hotmail.com  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-August 08

Posted 08 March 2011 - 01:37 AM

Hi Sir,

In this article how to save data in text file from the text communication mode

Thanks for reply..

Regards,
Rishikesh
Was This Post Helpful? 0
  • +
  • -

#72 llu02  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-April 11

Posted 01 April 2011 - 10:15 AM

This is a nice tutorial. I have tested the code attached. There is one issue I cannot figure out. Method DisplayData append outgoing and incoming data of the serial port in the richtextbox. However, when I use this software, I found the content in the richtextbox is automatically reflashed and only the most recent incoming or outgoing data will be display in the richtextbox. I was wondering if it should be showing all the data instead. If not, hot to change the code to keep all the data are shown in this textbox without being refleshed.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#73 suvarnars  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-September 09

Posted 16 June 2011 - 04:18 AM

I've been trying to use this code (as is - from zip file) to communication with an embedded target. The target is connected with to the computer using a usb<->serial adapter. I'm trying to read the data from the target to the computer. However, even though opening the port seems to be successful, i never receive any data.

What could the problem be?
Was This Post Helpful? 0
  • +
  • -

#74 euekimpoy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-November 11

Posted 23 November 2011 - 06:28 AM

View Postth4k1dd, on 20 March 2008 - 06:46 AM, said:

Hello, I was so excited to find this post and can't wait to play. I did run into one problem I am having with the code. I went ahead and threw it into C# and compiled. The program detects my one and only COM port, COM12 and adds it to the port drop down box like it should.

However, when I hit the Open Port button, it throws up an error in the Rich Text Box which says that COM1 does not exist. Of course that was not a mistype. I select COM12 and yet it tries to connect to COM1.

I took a look at the code and followed the _portName variable but was unable to see why it would cut off the 2 in 12. It would seem it should not as the variable is setup as a string.

Quick Info:
Windows Vista Business
Microsoft .Net Visual Studio 2008 Express
Arduino USB Microcontroller Card
Port Settings (should be): COM12, 9600, None, 1, 8
Programs Receiving COM12: Arduino Software, Putty, & Realterm

Am I missing something? You help is greatly appreciated.

One thing I noticed...I went into Device Manager and changed the USB Virtual COM port to use COM1 and for some reason the program works fine. Also works great if changed to COM2 in Device Manager.

Any ideas?

* Needs to read up on variable tracing *



the PORT1 was hardcoded on public CommunicationManager() ,try to change it
Was This Post Helpful? 0
  • +
  • -

#75 lusher00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 14-August 12

Posted 14 August 2012 - 07:13 PM

I can send data just fine but I can't receive. All I see is '?'s. I read this means .NET believes its not valid ASCII data but I can see it is in RealTerm. I have a dozen or more FTDI devices laying around and they all work in a loopback configuration but this FT2232 on a development board just can't be read. Lil help?
Was This Post Helpful? 0
  • +
  • -

  • (6 Pages)
  • +
  • « First
  • 3
  • 4
  • 5
  • 6