串口通讯

发布于 2024-07-22 21:13:43 字数 377 浏览 0 评论 0 原文

我想将数据十六进制格式(0x01)或(0xff)发送到连接rs232电缆的端口。 我使用的是VC++ 6.0中的MSCOMM控件。 这是发送十六进制数据的正确方法吗? 这是我的代码。

CString output;
UCHAR data1,data2;
data1=0x01;
output.Format("%x",data1);
m_mscom.SetOutput(COleVariant(output));
data2=0xff;
output.Format("%x",data2);
m_mscom.SetOutput(COleVariant(output));

如果不正确,请告诉我如何将十六进制格式的数据发送到端口。

提前致谢

I want to send data hex format (0x01) or (0xff) to the port where rs232 cable is connected. I am using the MSCOMM control in VC++ 6.0. Is that the correct way to send the hex data. This is the code I have.

CString output;
UCHAR data1,data2;
data1=0x01;
output.Format("%x",data1);
m_mscom.SetOutput(COleVariant(output));
data2=0xff;
output.Format("%x",data2);
m_mscom.SetOutput(COleVariant(output));

If it is not correct, please tell me how to send hex format data to the port.

Thanks in Advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟雨凡馨 2024-07-29 21:13:43

如果您的数据只是要发送的十六进制值(而不是十六进制格式的字符串),您可能需要“%c”而不是“%x”。 给定数据值(例如)0x65,“%x”将在串行端口上产生两个字节:“6”(ascii 值 54)和“5”(ascii 值 53)。 “%c”将在串行端口上生成一个字节:“e”(ascii 值 100 或 0x65)。

至于用 C 语言在串行端口上发送数据,请查看 CSerialCSerialPort:它们可能会为您简化一些事情(请注意,我没有使用过它们,我倾向于使用 pyserial 模块或在 wxWidgets 中使用 ctb 库)。

编辑:另一个非常好的(我以前用过它,但在我写原始帖子时找不到链接)是 CSerialFileWFC 库的一部分。 正如我所说,我现在倾向于使用 wxWidgets,但如果您使用 Microsoft 基础类,CSerialFile 确实使串行访问变得非常容易。

If your data is simply a hex value that you want to send out (as opposed to a hex formatted string), you probably want "%c" rather than "%x". Given a data value of (e.g.) 0x65, "%x" will produce two bytes on the serial port: '6' (ascii value 54) and '5' (ascii value 53). "%c" will produce a single byte on the serial port: 'e' (ascii value 100 or 0x65).

As far as sending data on a serial port in C, have a look at CSerial or CSerialPort: they may simplify things for you a bit (note that I've not used them, I tend to do serial port apps in python with the pyserial module or in wxWidgets with the ctb library).

Edit: The other one that's quite good (I have used it before, but couldn't find the link when I wrote the original post) is CSerialFile, part of the WFC libraries. As I said, I tend to use wxWidgets now, but if you're using the Microsoft foundation classes, CSerialFile does make serial access very easy.

远山浅 2024-07-29 21:13:43

我对 MSCOM 不熟悉,但它似乎不起作用。 Format 可能会将数据重新格式化为 ASCII 字符串表示形式。

或者,您可以在 Windows 中将任何串行端口用作“文件”。 查看用于打开文件的 Windows api,您会发现可以使用“COM1:”之类的文件名来将某些设备作为文件进行寻址。

然后,您可以像文件一样读取/写入它。

I'm no familiar with MSCOM but it seems like it won't work. Format may re-format the data to an ASCII string representation instead.

Alternatively, you can just use any serial port as a 'file' in Windows. Look at the windows api for opening files and you will see that you can address certain devices as files by using a filename like 'COM1:' instead.

Then, you can read/write from it like a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文