与串口通讯 [英] communicate with serial port

查看:74
本文介绍了与串口通讯的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!
如何使用Visual C ++通过串行端口将数据传输到微控制器.微控制器正在运行C程序(代码Warrier).我的问题是我无法将数据发送到微控制器板.我尝试了很多事情,但无济于事.
编译程序时没有错误.而且,它什么也不会转移任何东西到电路板上.

提前谢谢.

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.

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());

推荐答案

看看此MSDN文章:
Take a look at this MSDN article: http://support.microsoft.com/default.aspx?scid=kb;EN-US;q115831[^]


"COM11"真的正确吗?也许应该更像是"COM1".

szLogon.StationID = Convert::ToByte("31");的含义是什么?就像这样写:szLogon.StationID = 31;

问候,

-MRB
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


我看不到您在哪里设置波特率,奇偶校验等.很可能需要设置这些参数.

我注意到您正在使用托管C ++.为什么不使用.NET
串行端口 [<而是使用href ="http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.aspx" target ="_ blank" title ="New Window"> ^ ]对象Win32 API?使用起来非常简单.

无论如何,如果您必须使用Win32 API,那么本文将为您指明正确的方向:
在Win32上创建串行通讯 [
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[^]


这篇关于与串口通讯的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆