RS-232-C的C语言程序

RS-232-C的C语言程序,第1张

通过RS232总线与PC进行通信的测试程序(c语言)

/*这是一个使用单片机串行通信口通过RS232总线与PC进行通信的测试程序,无需扩展.可将DB9的第2和第3脚短路,进行自收自发测试.采用串行中断接收,速率:9600,通信方式2,晶振使用:osc=11.0592MHz。使用SW1-SW6键盘发送文本数字1-6,数码管显示接收的ASCII数据*/

void main(void)

{

SCON=0x50

TMOD=0x21

TH1=0xfd

TL1=0xfd

TH0=0xf4

TL0=0x48

ES=1ET0=1EA=1

TR1=1

TR0=1

while(1)

{

if(!key){ Delay(30000)

a=Key_down()

SBUF=table[a]

while(TI==0)

TI=0

}

}

}

uchar Key_down(void)

{ uchar x

if(!sw1)x=1

else if(!sw2)x=2

else if(!sw3)x=3

else if(!sw4)x=4

else if(!sw5)x=5

else if(!sw6)x=6

return(x)

}

void Int_T0(void) interrupt 1

{

TH0=0xf4

TL0=0x48

if(count==5)count=0

else count++

switch(count)

{

case 0:DIG_NUM=0x7f

LED_DISPLAY=buff_led1

break

case 1:DIG_NUM=0xbf

LED_DISPLAY=buff_led2

break

case 2:DIG_NUM=0xdf

LED_DISPLAY=buff_led3

break

case 3:DIG_NUM=0xef

LED_DISPLAY=buff_led4

break

case 4:DIG_NUM=0xf7

LED_DISPLAY=buff_led5

break

case 5:DIG_NUM=0xfb

LED_DISPLAY=buff_led6

break

default:break

}

}

void Int_Serial(void) interrupt 4

{

while(RI==0)

a=SBUFRI=0

buff_led1=buff_led2

buff_led2=buff_led3

buff_led3=buff_led4

buff_led4=buff_led5

buff_led5=buff_led6

buff_led6=a

}

void Delay(int t)

{while(--t)}

VB 6.0的测试代码如下: 可以做参考,用MSComm1控件

Dim i As Integer

Dim a As Integer

Dim t() As Byte

Private Sub Command1_Click()

If Command1.Caption = "打开串口" Then

'Shape1.Visible = True

Shape1.FillColor = &HFF&

Shape1.BackColor = &HFF&

Shape1.BorderColor = &HFF&

Command2.Enabled = True

Command3.Enabled = True

Command1.Caption = "关闭串口"

'MSComm1.CommPort = 1 '设定Com1口

MSComm1.CommPort = Combo1.Text '设定Com1口

If MSComm1.PortOpen = False Then

'MSComm1.Settings = "4800,N,8,1" '4800波特率,无校验,8位数据位,1位停止位

MSComm1.Settings = Combo2.Text &"," &Combo3.Text &"," &Combo4.Text &"," &Combo5.Text '4800波特率,无校验,8位数据位,1位停止位

MSComm1.InputLen = 1 '读取接收缓冲区的所有字符

MSComm1.InputLen = 5

MSComm1.InBufferSize = 5 '设置接收缓冲区为1024字节

MSComm1.OutBufferSize = 5 '设置发送缓冲区为512字节

MSComm1.SThreshold = 0 '不触发发送事件

MSComm1.RThreshold = 1 '每一个字符到接收缓冲区都触发接收事件

MSComm1.PortOpen = True '打开串口

End If

MSComm1.OutBufferCount = 0 '清空发送缓冲区

MSComm1.InBufferCount = 0 '清空接收缓冲区

Dim temp() As Byte

Else

'Option2.Value = False

'Option1.Value = False

'Shape1.Visible = False

Shape1.BackColor = &H808080

Shape1.BorderColor = &H808080

Shape1.FillColor = &H808080

Command2.Enabled = False

'Command3.Enabled = False

MSComm1.PortOpen = False

Command1.Caption = "打开串口"

End If

End Sub

Private Sub Command2_Click()

'Command2.Enabled = False

'Option2.Value = False

'Option1.Value = False

'Shape2.Visible = False

MSComm1.OutBufferCount = 0 '清空发送缓冲区

MSComm1.InBufferCount = 0 '清空接收缓冲区

't(0) = "255"

' MSComm1.Output = t

MSComm1.Output = Text2.Text

End Sub

Private Sub Command3_Click()

Text1.Text = ""

End Sub

Private Sub Command5_Click() '退出

End

End Sub

Private Sub Command6_Click()

Text2.Text = ""

End Sub

Private Sub Command7_Click()

If Command7.Value Then

'Command7.Visible = False

Command1.Visible = True

Command2.Visible = True

Command3.Visible = True

Command6.Visible = True

Label1.Visible = True

Label2.Visible = True

Label3.Visible = True

Label4.Visible = True

Label5.Visible = True

Label6.Visible = True

Label7.Visible = True

Combo1.Visible = True

Combo2.Visible = True

Combo3.Visible = True

Combo4.Visible = True

Combo5.Visible = True

Text1.Visible = True

Text2.Visible = True

Frame1.Visible = True

Frame2.Visible = False

'Command3.Visible = False

'Option1.Value = False

Shape1.Visible = True

Else

Label1.Visible = False

Label2.Visible = False

Label3.Visible = False

Label4.Visible = False

Label5.Visible = False

Label6.Visible = False

Label7.Visible = False

Combo1.Visible = False

Combo2.Visible = False

Combo3.Visible = False

Combo4.Visible = False

Combo5.Visible = False

Command1.Visible = False

Command2.Visible = False

Command3.Visible = False

Text1.Visible = False

Text2.Visible = False

Frame1.Visible = False

Frame2.Visible = False

End If

End Sub

Private Sub Form_Load()

MSComm1.InBufferCount = 0 '清除发送缓冲区数据

MSComm1.OutBufferCount = 0 '清除接收缓冲区数据

ReDim t(0)

t(0) = "0"

Combo1.AddItem "1"

Combo1.AddItem "2"

Combo1.AddItem "3"

Combo1.AddItem "4"

'Combo1.Text = "1"

Combo2.AddItem "300"

Combo2.AddItem "600"

Combo2.AddItem "1200"

Combo2.AddItem "2400"

Combo2.AddItem "4800"

Combo2.AddItem "9600"

Combo2.AddItem "19200"

Combo3.AddItem "N"

Combo3.AddItem "O"

Combo3.AddItem "E"

Combo4.AddItem "8"

Combo4.AddItem "7"

Combo4.AddItem "6"

Combo5.AddItem "1"

Combo5.AddItem "2"

Combo5.AddItem "3"

End Sub

Private Sub Frame2_DragDrop(Source As Control, X As Single, Y As Single)

End Sub

Private Sub MSComm1_OnComm()

Dim s1 As String

Dim s2 As String

Select Case MSComm1.CommEvent

Case comEventOverrun '数据丢失

Text_SEND.Text = "" '清空发送缓冲区

Text_RECV.Text = "" '清空接收缓冲区

Text_SEND.SetFocus

Exit Sub

Case comEventRxOver '接收缓冲区溢出

Text_SEND.Text = "" '清空发送缓冲区

Text_RECV.Text = "" '清空接收缓冲区

Text_SEND.SetFocus

Exit Sub

Case comEventTxFull '发送缓冲区已满

Text_SEND.Text = "" '清空发送缓冲区

Text_RECV.Text = "" '清空接收缓冲区

Text_SEND.SetFocus

Exit Sub

Case comEvReceive '接收字符数据

Dim Buffer As Variant

MSComm1.InputLen = 1

MSComm1.InputMode = comInputModeBinary '二进制传输

Buffer = MSComm1.Input

'Text1.Text = Buffer(0)

's2 = Buffer(1)

Text1.Text = Text1.Text &(Val(MSComm1.Input)) &" "

Case Else

End Select

End Sub

Private Sub Option1_Click()

End Sub

首先,串口调试软件的波特率最好设置成9600,这种波特率也支持显示中文的,

USB转串口看你用的什么芯片,经过实验采用CH340或是341的是没有问题的,其他还有待验证。

串口调试软件也很重要,有些软件会奇怪出现乱码,但并不是程序错误,推荐你用小丁的sscom32,个人感觉很棒! **************************************************************************

程序名称:单片机串口发送中文字符实验,232通信程序

硬件说明:打开《串口调试助手》,波特率设为9600速率,11.0592晶体

注意:《串口调试助手》要设置好COM号(请核对是否和设备管理器的COM编号一致)

*** 作说明:下载程序后打开串口调试助手sscom32.exe即可显示,或按一下复位开关

***************************************************************************/

ORG 0000H

AJMP main

ORG 0030H

main: MOV 20H,#00H

MOV TMOD,#22H 定时器工作方式

MOV SCON,#40H

MOV TH1,#0FAH 波特率9600

MOV PCON,#80H

SETB TR1

SETB REN 允许接收

ajmp start

start: mov dptr,#num 查表

send : clr a

movc a,@a+dptr

cjne a,#'$',send_2

ajmp $ 遇到$停止。

send_2:mov sbuf,a

JNB TI,$ 串行发送一帧结束了吗?

CLR TI

inc dptr 指针加一

Ljmp send

num:

db 0dh,0ah,0dh,0ah,"XY900_USB型单片机学习板USB-232串口中文发送试验 http://www.mcusy.cn"

db 0dh,0ah,0dh,0ah,"-----心情解梦-----"

db 0dh,0ah,0dh,0ah,"我梦见自己和前女友生了一个小孩,自己用婴儿车推着孩子在山间的公路上不停地走..."

db 0dh,0ah,0dh,0ah,"最佳答案:"

db 0dh,0ah,0dh,0ah,"首先,你们之间的感情给你留下了很深刻的东西,我想那些镜头可能是你们在一起时你所感受到的---那也许可能会是真的景物..."

db 0dh,0ah,0dh,0ah,"其次,也可能是代表某种心情,我猜想是不是你们曾经的爱情让你觉得幸福但同时也有隐隐作痛...回忆让你觉得不安全和无法掌握。",0dh,0ah,'$'

end


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/7964946.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存