如何通过串口发送字符串......? [英] How to send string through a serial port...?

查看:136
本文介绍了如何通过串口发送字符串......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我尝试发送的串口发送字符串

I want to send string through the serial port i tried to send

char lpBuffer[] = "100001111";

我可以发送它成功。但是当我在下面尝试时,我会得到一个疯狂的结果。我可以使用什么方法....?



我尝试了什么:



i could send it successfully. but when I try below I am getting a crazy result. What method I can use....?

What I have tried:

void setDataToPort(string strData)
{
	BOOL   Status;

	//char lpBuffer[] = "100001111";
	
	string mStr = "100001111";
	
	string *srData = &mStr;

	DWORD dNoOFBytestoWrite;         // No of bytes to write into the port
	DWORD dNoOfBytesWritten = 0;     // No of bytes written to the port
	dNoOFBytestoWrite = sizeof(*srData);
	
	

	Status = WriteFile(hComm,        // Handle to the Serial port
		(LPCVOID*)srData,     // Data to be written to the port
		dNoOFBytestoWrite,  //No of bytes to write
		&dNoOfBytesWritten, //Bytes written
		NULL);
	
// I am getting below results
/*

Hö†.100001111.ÌÌÌÌÌÌ........Hö†.100001111.ÌÌÌÌÌÌ........hý†.100001111.ÌÌÌÌÌÌ........hý†.100001111.ÌÌÌÌÌÌ........ý†.100001111.ÌÌÌÌÌÌ........ý†.100001111.ÌÌÌÌÌÌ.........õ†.100001111.ÌÌÌÌÌÌ........

*/



}

推荐答案

也许您对这篇文章感兴趣:写入C中串行端口的字节广告文件•n。快速,直观的想法和解决方案文档。 [ ^ ]
Maybe this article is of interest to you: Writing bytes to a serial port in C | ad hocumentation • n. fast, instinctive documentation of ideas and solutions.[^]


无需使用C ++字符串类。只需使用 char 缓冲区。这避免了宽字符串的问题(例如,带有MFC的 CString )。使用字符串类时使用相应的函数来访问字符(例如 string :: c_str )。



避免使用辅助变量和强制转换。这使得代码不可读并且容易出错(在你的情况下要写入的字符数)。



这应该可以完成这项工作:

There is no need to use C++ string classes. Just use char buffers instead. This avoids problems with wide char strings (e.g. CString with MFC). When using using string classes use the corresponding functions to access the characters (e.g. string::c_str).

Avoid using helper variables and casts. This makes the code unreadable and is prone to errors (the number of characters to be written in your case).

This should do the job:
char lpBuffer[] = "100001111";
DWORD dNoOFBytestoWrite;
// You have to pass the number of characters to be written.
// sizeof() of would only work with byte arrays of fixed size when
//  all array items are initialised.
DWORD dNoOfBytesWritten = strlen(lpBuffer);
Status = WriteFile(hComm, lpBuffer, dNoOFBytestoWrite, &dNoOfBytesWritten, NULL);



但是我想你仍然会得到类似于那个的结果显示在你的问题中。看起来您正在接收数据,并且在打印接收的数据之前没有附加NULL字节,因此打印功能无法确定字符串的结尾。一个可能的解决方案是发送NULL字节:


But I guess that you will still get a result similar to the one shown in your question. It looks like you are receiving the data and did not append a NULL byte before printing the received data out so that the print function can not determine the end of the string. A possible solution would be sending also the NULL byte:

DWORD dNoOfBytesWritten = strlen(lpBuffer) + 1;

但这在串行通信中并不常见使用可打印的字符。相反,更改您的接收函数,以便在没有更多数据时附加NULL字节。

But this is rather uncommon with serial communications using printable characters. Instead, change your receiving function to append a NULL byte when there are no more data.


这篇关于如何通过串口发送字符串......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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