如何实现pc机上的com口通信

kuaidi.ping-jia.net  作者:佚名   更新日期:2024-05-03
如何实现(电脑)PC机与单片机的串行通信

硬件部分有二种情况
1、电脑带有原生串口,就是主板上带有串行口,这个情况就太好办了。用“串口调试助手”测试一下就可以用VB写程序了。电脑和单片机用232芯片进行接口转换。
2、电脑没有原生串口,就是主板上没有串行口,这个情况有点麻烦。要用USB转串口的模块,装好驱动后会在“设备管理器”中出现一个串口,直接当原生串口用就行了,而且经过USB转出来的串口一般都TTL电平的,也就是可以直接接单片机不用232芯片的。不过这也要看你用的是哪种模块了,具体的要看模块说明书。
最后要注意的是波特率和单片机晶振的选用,不知道你用的是什么型号的单片机。

软件部分,可以用简单的VB编写上位机软件,这方面网上有很多的代码。单片机部分只要按照普通的串行写程序就行了,测试时可以用“串口调试助手”这款软件,调试好后再写VB程序。
上述是我们做项目和产品的经验,希望对你有所帮助。

51 单片机 C语言?

网络上例程 多得很呢

去百度文库 搜 单片机 100 例 吧
有 proteus 仿真图、C语言源码 等等

狂晕中....给你linux代码,你就问windows,给你windows串口控制方式,你就问linux,I 服了 You。
区别很大,两个不同的操作系统,在应用串口上就有所不同,但大体上,还是有共同点,1、设置串口,2、打开串口,3、读写数据,4、异常处理,5、关闭串口
这一系列的控制逻辑都大体相同,只是在代码实现上有所不一样,毕竟两种操作系统内核结构就不一样,linux的串口设置属性是通过头文件#include <termios.h>的struct termios结构实现,而windows的串口属性是commprop结构设定,其他的读写串口,都有相应的读写文件函数,两种系统都是把串口当作一个设备文件来读写,所以这里就不难理解,为什么使用文件函数来处理串口。linux下可以使用标准C库函数来控制读写串口,windows下可以使用WINDOWS API函数来做串口读写,具体请参看《windows API 大全》以及下文给出的参考地址,也是有详细的说明

在PC机上实现COM口通信并不困难,可你得说清楚是什么操作系统,不同的操作系统,控制串口的区别是很大。
在Windows系统上操作串口可以使用三种方式:MSCOMM控件,WINDOWS API,第三方控件

WINDOWS API使用起来相对麻烦些,主要是在设置串口属性、查询读取方面及异常处理方面。这里简要说下控制串口步骤:
1.打开串口:
使用createfile()打开串口,createfile()将返回串口的句柄。
handle createfile(
lpctstr lpfilename, // pointer to name of the file
dword dwdesiredaccess, // access (read-write) mode
dword dwsharemode, // share mode
lpsecurity_attributes lpsecurityattributes, // pointer to security attributes
dword dwcreationdistribution, // how to create
dword dwflagsandattributes, // file attributes
handle htemplatefile // handle to file with attributes to copy
);
lpfilename: 指明串口制备,例:com1,com2
dwdesiredaccess: 指明串口存取方式,例:generic_read|generic_write
dwsharemode: 指明串口共享方式
lpsecurityattributes: 指明串口的安全属性结构,null为缺省安全属性
dwcreateiondistribution: 必须为open_existin
dwflagandattributes: 对串口唯一有意义的是file_flag_overlapped
htemplatefile: 必须为null
2.关闭串口:

closehandle(hcommdev);

3.设置缓冲区长度:

bool setupcomm(
handle hfile, // handle of communications device
dword dwinqueue, // size of input buffer
dword dwoutqueue // size of output buffer
);

4.commprop结构:

可使用getcommproperties()取得commprop结构,commprop结构中记载了系统支持的各项设置。
typedef struct _commprop { // cmmp
word wpacketlength; // packet size, in bytes
word wpacketversion; // packet version
dword dwservicemask; // services implemented
dword dwreserved1; // reserved
dword dwmaxtxqueue; // max tx bufsize, in bytes
dword dwmaxrxqueue; // max rx bufsize, in bytes
dword dwmaxbaud; // max baud rate, in bps
dword dwprovsubtype; // specific provider type
dword dwprovcapabilities; // capabilities supported
dword dwsettableparams; // changeable parameters
dword dwsettablebaud; // allowable baud rates
word wsettabledata; // allowable byte sizes
word wsettablestopparity; // stop bits/parity allowed
dword dwcurrenttxqueue; // tx buffer size, in bytes
dword dwcurrentrxqueue; // rx buffer size, in bytes
dword dwprovspec1; // provider-specific data
dword dwprovspec2; // provider-specific data
wchar wcprovchar[1]; // provider-specific data
} commprop;
dwmaxbaud:
baud_075 75 bps
baud_110 110 bps
baud_134_5 134.5 bps
baud_150 150 bps
baud_300 300 bps
baud_600 600 bps
baud_1200 1200 bps
baud_1800 1800 bps
baud_2400 2400 bps
baud_4800 4800 bps
baud_7200 7200 bps
baud_9600 9600 bps
baud_14400 14400 bps
baud_19200 19200 bps
baud_38400 38400 bps
baud_56k 56k bps
baud_57600 57600 bps
baud_115200 115200 bps
baud_128k 128k bps
baud_user programmable baud rates available
dwprovsubtype:
pst_fax 传真设备
pst_lat lat协议
pst_modem 调制解调器设备
pst_network_bridge 未指定的网桥
pst_parallelport 并口
pst_rs232 rs-232口
pst_rs422 rs-422口
pst_rs423 rs-432口
pst_rs449 rs-449口
pst_scanner 扫描仪设备
pst_tcpip_telnet tcp/ip telnet协议
pst_unspecified 未指定
pst_x25 x.25标准
dwprovcapabilities
pcf_16bitmode 支持特殊的16位模式
pcf_dtrdsr 支持dtr(数据终端就绪)/dsr(数据设备就绪)
pcf_inttimeouts 支持区间超时
pcf_parity_check 支持奇偶校验
pcf_rlsd 支持rlsd(接收线信号检测)
pcf_rtscts 支持rts(请求发送)/cts(清除发送)
pcf_setxchar 支持可设置的xon/xoff
pcf_specialchars 支持特殊字符
pcf_totaltimeouts 支持总(占用时间)超时
pcf_xonxoff 支持xon/xoff流控制
标准rs-232和window支持除pcf_16bitmode和pcf_specialchar外的所有功能
dwsettableparams
sp_baud 可配置波特率
sp_databits 可配置数据位个数
sp_handshaking 可配置握手(流控制)
sp_parity 可配置奇偶校验模式
sp_parity_check 可配置奇偶校验允许/禁止
sp_rlsd 可配置rlsd(接收信号检测)
sp_stopbits 可配置停止位个数
标准rs-232和window支持以上所有功能
wsettabledata
databits_5 5个数据位
databits_6 6个数据位
databits_7 7个数据位
databits_8 8个数据位
databits_16 16个数据位
databits_16x 通过串行硬件线路的特殊宽度路径
windows 95支持16的所有设置

5.dcb结构:

typedef struct _dcb {// dcb
dword dcblength; // sizeof(dcb)
dword baudrate; // current baud rate
指定当前的波特率
dword fbinary: 1; // binary mode, no eof check
指定是否允许二进制模式,
windows 95中必须为true
dword fparity: 1; // enable parity checking
指定奇偶校验是否允许
dword foutxctsflow:1; // cts output flow control
指定cts是否用于检测发送控制。
当为true是cts为off,发送将被挂起。
dword foutxdsrflow:1; // dsr output flow control
指定cts是否用于检测发送控制。
当为true是cts为off,发送将被挂起。
dword fdtrcontrol:2; // dtr flow control type
dtr_control_disable值将dtr置为off, dtr_control_enable值将dtr置为on, dtr_control_handshake允许dtr"握手",dword fdsrsensitivity:1; // dsr sensitivity 当该值为true时dsr为off时接收的字节被忽略
dword ftxcontinueonxoff:1; // xoff continues tx
指定当接收缓冲区已满,并且驱动程序已经发
送出xoffchar字符时发送是否停止。
true时,在接收缓冲区接收到缓冲区已满的字节xofflim且驱动程序已经发送出xoffchar字符中止接收字节之后,发送继续进行。
false时,在接收缓冲区接收到代表缓冲区已空的字节xonchar且驱动程序已经发送出恢复发送的xonchar之后,发送继续进行。
dword foutx: 1; // xon/xoff out flow control
true时,接收到xoffchar之后便停止发送
接收到xonchar之后将重新开始
dword finx: 1; // xon/xoff in flow control
true时,接收缓冲区接收到代表缓冲区满的xofflim之后,xoffchar发送出去
接收缓冲区接收到代表缓冲区空的xonlim之后,xonchar发送出去
dword ferrorchar: 1; // enable error replacement
该值为true且fparity为true时,用errorchar 成员指定的字符代替奇偶校验错误的接收字符
dword fnull: 1; // enable null stripping
true时,接收时去掉空(0值)字节
dword frtscontrol:2; // rts flow control
rts_control_disable时,rts置为off
rts_control_enable时, rts置为on
rts_control_handshake时,
当接收缓冲区小于半满时rts为on
当接收缓冲区超过四分之三满时rts为off
rts_control_toggle时,
当接收缓冲区仍有剩余字节时rts为on ,否则缺省为off
dword fabortonerror:1; // abort reads/writes on error
true时,有错误发生时中止读和写操作
dword fdummy2:17; // reserved
未使用
word wreserved; // not currently used
未使用,必须为0
word xonlim; // transmit xon threshold
指定在xon字符发送这前接收缓冲区中可允许的最小字节数
word xofflim; // transmit xoff threshold
指定在xoff字符发送这前接收缓冲区中可允许的最小字节数
byte bytesize; // number of bits/byte, 4-8
指定端口当前使用的数据位
byte parity; // 0-4=no,odd,even,mark,space
指定端口当前使用的奇偶校验方法,可能为:
evenparity,markparity,noparity,oddparity
byte stopbits; // 0,1,2 = 1, 1.5, 2
指定端口当前使用的停止位数,可能为:
onestopbit,one5stopbits,twostopbits
char xonchar; // tx and rx xon character
指定用于发送和接收字符xon的值
char xoffchar; // tx and rx xoff character
指定用于发送和接收字符xoff值
char errorchar; // error replacement character
本字符用来代替接收到的奇偶校验发生错误时的值
char eofchar; // end of input character
当没有使用二进制模式时,本字符可用来指示数据的结束
char evtchar; // received event character
当接收到此字符时,会产生一个事件
word wreserved1; // reserved; do not use 未使用
} dcb;

6.改变端口设置

使用如下的两个方法
bool getcommstate(hcomm,&dcb);
bool setcommstate(hcomm,&dcb);

7.改变普通设置

buildcommdcb(szsettings,&dcb);
szsettings的格式:baud parity data stop
例: "baud=96 parity=n data=8 stop=1"
简写:"96;,n,8,1"
szsettings 的有效值
baud:
11 or 110 = 110 bps
15 or 150 = 150 bps
30 or 300 = 300 bps
60 or 600 = 600 bps
12 or 1200 = 1200 bps
24 or 2400 = 2400 bps
48 or 4800 = 4800 bps
96 or 9600 = 9600 bps
19 or 19200= 19200bps
parity:
n=none
e=even
o=odd
m=mark
s=space
data:
5,6,7,8
stopbit
1,1.5,2

8.commconfig结构:

typedef struct _comm_config {
dword dwsize;
word wversion;
word wreserved;
dcb dcb;
dword dwprovidersubtype;
dword dwprovideroffset;
dword dwprovidersize;
wchar wcproviderdata[1];
} commconfig, *lpcommconfig;
可方便的使用bool commconfigdialog(
lptstr lpszname,
hwnd hwnd,
lpcommconfig lpcc);
来设置串行口。

9.超时设置:

可通过commtimeouts结构设置超时,
typedef struct _commtimeouts {
dword readintervaltimeout;

原文参考《VC实现串口通信例程》 作者:阮帮秋
http://www.mp3sea.net/Visual-C/2007-3-22/VC-ShiXianChuanKouTongShenLiChengZuoZhe-RuanBangQiu-bbjm10723.htm

下次记得把问题一次提出来。即便是帮你找资料,也方便一些。

  • 电脑COM口怎么设置
    答:首先,打开控制面板,可以在“计算机/我的电脑”中点击上方的“打开控制面板”选项,或者直接从“开始”菜单中点击进入。进入控制面板后,点击“硬件与声音”选项,并在设备和打印机的下属选项中点击“设备管理器”。进入设备管...
  • 电脑com端口怎么设置
    答:电脑com端口设置方法如下:在电脑的打开界面上右击此电脑,在打开的界面上点击属性,进入到系统界面后点击设备管理器,在该选项的打开界面上点击端口,接着双击通信端口,在通信窗口的打开界面上就能设置端口的数据了。还可以打...
  • 求大神给电脑串口连电脑串口线接线图,谢谢!!!
    答:一、串行接口 串行接口(Serial port)又称“串口”,也称串行通信接口(通常指COM接口),是采用串行通信方式的扩展接口。常见的有一般电脑应用的RS-232(使用 25 针或 9 针连接器)和工业电脑应用的半双工RS-485与全双工...
  • 一台计算机上两个串口如何实现通信?
    答:通常插头在DCE端,插座在DTE端. 一些设备与PC机连接的RS-232-C接口,因为不使用对方的传送控制信号,只需三条接口线,即“发送数据”、“接收数据”和“信号地”。所以采用DB-9的9芯插头座,传输线采用屏蔽双绞线。
  • 如何实现pc机上的com口通信
    答:在PC机上实现COM口通信并不困难,可你得说清楚是什么操作系统,不同的操作系统,控制串口的区别是很大。在Windows系统上操作串口可以使用三种方式:MSCOMM控件,WINDOWS API,第三方控件WINDOWS API使用起来相对麻烦些,主要是在设置串口属性、查询...
  • 串口转网口如何通信?
    答:1. 配置串口服务器:按照说明书上的步骤,将串口服务器与计算机连接,并在配置软件中设置好串口参数,如波特率、数据位、校验位等。2. 配置网络参数:给串口服务器设定一个在本地局域网内唯一的IP地址,以便其它的计算机可以...
  • 怎么样设置电脑的COM口才能使电脑和三菱PLC通讯呢。
    答:很可能是你的电脑的COM端口号设置的不对,一般PLC默认的是用COM1口,而你的电脑上的COM口可能是COM2,COM3或别的号。可以右单击我的电脑--属性--硬件配置--选择你的COM口,右击COM设置,设置为COM1就可以了。
  • plc怎样与电脑通信?
    答:具体方法如下:1、首先需要传输端口,当PLC 与电脑连接通讯线之后,右击“我的电脑”,选择“管理”。2、在弹出的“计算机管理”窗口中,选择“设备管理器”。3、查看通讯线所使用的com端口为多少,并记住这个端口,本实例...
  • PC机串口通信的工作原理
    答:Serial Bus或者USB混淆)。大多数计算机包含两个基于RS232的串口。串口同时也是仪器仪表设备通用的通信协议;很多GPIB兼容的设备也带有RS-232口。同时,串口通信协议也可以用于获取远程采集设备的数据。串口通信的概念非常简单,...
  • 用电脑串口如何跟485口通信?
    答:2、设置完成后,然后再windows开始点击右键选择打开设备管理器。3、在设备管理器的端口选项中看到已经设置好的的串口2和串口4,并且两个已经设置好的串口已经相连说明两个串口之间可以进行通信了。4、在windows中找到proteus仿真...