Click here to Skip to main content
15,909,051 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi!
How can one transfer data through serial port to a microcontroller using visual C++. The microcontroller is running a C program (code warrier). My problem is that I can't send data to the microcontroller board. I tried a lot of things, but to no avail.
There is no error when compiling the program. Moreover, it doesn't anything transfer anything to the circuit board.

Thanks in advance.

C++
private: System::Void FireRL3_button_Click(System::Object^  sender, System::EventArgs^  e) {
			
			Station szLogon;
			szLogon.StationID = Convert::ToByte("31"); //Convert.ToByte(tb1.Text);
			szLogon.StationCMD =(Byte)OP3FIRE;
			//szLogon.StationCMD  = (Byte)_outp(RL3FIRE,0x00);
			szLogon.StationMSG0 = 1;
			szLogon.StationMSG1 = 2;
			szLogon.StationMSG2 = 3;
			szLogon.StationMSG3 = 4;
			szLogon.StationMSG4 = 5;
			szLogon.StationMSG5 = 6;
			szLogon.StationMSG6 = 7;
			szLogon.StationMSG6 = 8;
			szLogon.StationMSG7 = 0;
			szLogon.EOD = 0x13;
			//
			//serialPort1->Read(StructureToByteArray(szLogon.StationID), 0, Marshal::SizeOf(szLogon.StationID));
			//_outp(00,00);
			 //Sleep(100);
			//serialPort1->DiscardOutBuffer();
			
			HANDLE hfile=CreateFile(TEXT("COM11"),GENERIC_WRITE|GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
		
			char buff[7];
			DWORD read=0;
			DWORD write=0;
			buff[0] =50;
			WriteFile(hfile,buff,write,&write,NULL);
			ReadFile(hfile,buff,sizeof(buff),&read,NULL);
			CloseHandle(hfile);
			SetTextBox(szLogon.StationCMD.ToString());
			//_outp(RL3FIRE,0x00);			
			//serialPort1->Write(StructureToByteArray(_outp(RL3FIRE,0)), 0, Marshal::SizeOf(szLogon.StationID));
			//serialPort1->Write(RL3FIRE.ToString());
Posted
Updated 24-Apr-11 22:16pm
v2
Comments
Manfred Rudolf Bihy 25-Apr-11 4:17am    
Edit: Added pre tags. Minor spelling and grammar changes.
Richard MacCutchan 25-Apr-11 4:23am    
Try adding some error checking, run it through the debugger. As this code stands it could run from start to finish without doing anything correctly and you would never know.

 
Share this answer
 
Is "COM11" really correct? Maybe it should read more like "COM1".

What's the bit with szLogon.StationID = Convert::ToByte("31"); supposed to mean? Just write it like this: szLogon.StationID = 31;

Regards,

-MRB
 
Share this answer
 
Comments
doramickey 25-Apr-11 4:32am    
it convert to byte, so output will be 31. My port is "COM11", otherwise the port is closed.
Richard MacCutchan 25-Apr-11 5:23am    
What is wrong with: szLogon.StationID = 31?
Manfred Rudolf Bihy 25-Apr-11 10:56am    
I don't understand that either, that's why I asked OP about it. :)
Richard MacCutchan 25-Apr-11 12:41pm    
I don't understand my comment; I think my brain was connected elsewhere.
I don't see where you're setting the baud rate, parity etc. Most likely these parameters need to be set.

I noticed you're using managed C++. Why don't you use the .NET Serial Port[^] object instead of the Win32 API? It is so much easier to use.

Anyway, if you must you Win32 API, here's an article that will point you in the right direction:
Creating a Serial communication on Win32[^]
 
Share this answer
 
Comments
doramickey 25-Apr-11 22:21pm    
I set baud rate,stop bit,parity bit on top. I directly connect microcontroller board through serial port?
Rick Shaub 25-Apr-11 22:52pm    
I don't see where you're setting the baud, parity bit, etc. DO you mean in a different method? Did you look at the second link? That shows how you should use Win32 to do serial communications. Also, look at Hans Dietrich's link. It contains important information about using serial port numbers higher than "COM9".
doramickey 26-Apr-11 5:05am    
I use serial port "COM11". I set it as global
serialPort1->PortName ="COM11";
serialPort1->BaudRate=9600;
serialPort1->ReceivedBytesThreshold = 11;
serialPort1->WriteBufferSize = 4096;
serialPort1->ReadTimeout=2000;
serialPort1->WriteTimeout=500;
serialPort1->Open();
There is a sample of Serial Port communication on CodeProject: Creating a Serial communication on Win32. I guess it will answer many of your questions. You can also try to find MSDN sample code for VC++ 6.0 (rather old but compiles and runs in VC++ 2008) named COMM sample (TTY).
Sergey Chepurin.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900