#include stdlib.h>
#include string.h>
#include unistd.h>
#include fcntl.h>
#include errno.h>
#include termios.h>
int open_port(void)
{
int fd
fd = open("/dev/s3c2410_serial1", O_RDWR|O_NOCTTY|O_NDELAY)
if(fd == -1)
printf("Unable to open uart1\n")
else
fcntl(fd, F_SETFL, 0)
return fd
}
int setup_uart(int fd)
{
struct termios oldtio, newtio
if((tcgetattr(fd, &oldtio)) != 0)
{
printf("Save old error!\n")
return -1
}
bzero(&newtio, sizeof(newtio))
newtio.c_cflag |= (CLOCAL | CREAD)
newtio.c_cflag &= ~CSIZE
newtio.c_cflag &= ~CSTOPB// 1 stop bit
newtio.c_cflag &= ~PARENB// No parity
newtio.c_cflag |= CS8// 8 bits data
cfsetispeed(&newtio, B9600)
cfsetospeed(&newtio, B9600)
//newtio.c_lflag |= (ICANON | ECHO)
//newtio.c_lflag &= ~ECHOE
//newtio.c_iflag &= ~(IXON | IXOFF | IXANY)
newtio.c_cc[VTIME] = 0
newtio.c_cc[VMIN] = 0
tcflush(fd, TCIFLUSH)
if((tcsetattr(fd, TCSANOW, &newtio)) != 0)
{
printf("Set new error!\n")
return -1
}
}
int main(void)
{
int i
int fd
char c,str[200]
fd = open_port()
if(fd == -1)
return 0
i = setup_uart(fd)
if(i == -1)
return 0
while(1)
{
for(i=0i200i++)
str = '\0'
i = 0
printf("GPRS-CMD #")
while((c=getchar()) != 10)
{
str = c
i++
}
str = 13
str[i+1] = '\0'
i = write(fd, str, strlen(str))
if(i 0)
printf("write error!")
for(i=0i100i++)
usleep(100)
i = read(fd, str, 200)
printf("%s\n", str)
}
}
另一篇和主题相似的已发送往您的邮箱,请查收.
你把下面的程序 复制后改名为.frm(加载上comm控件后)
编译一下,这个程序调试通过了
祝你好运
VERSION 5.00
Object = "#1.1#0""MSCOMM32.OCX"
Begin VB.Form frmMain
BorderStyle = 1 'Fixed Single
Caption = "MSComm串口通信示例"
ClientHeight= 3195
ClientLeft = 45
ClientTop = 435
ClientWidth = 4920
Icon= "frmMain.frx":0000
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 3195
ScaleWidth = 4920
StartUpPosition = 2 '屏幕中心
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 1
Left= 600
Style = 1 'Graphical
TabIndex= 6
Top = 1800
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 1
Left= 2400
Style = 1 'Graphical
TabIndex= 15
Top = 2640
Width = 255
End
Begin VB.Frame Frame3
Height = 615
Left= 0
TabIndex= 13
Top = 2400
Width = 4815
Begin VB.CommandButton button
Height = 300
Index = 7
Left= 240
Style = 1 'Graphical
TabIndex= 21
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 6
Left= 600
Style = 1 'Graphical
TabIndex= 20
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 5
Left= 960
Style = 1 'Graphical
TabIndex= 19
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 4
Left= 1320
Style = 1 'Graphical
TabIndex= 18
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 3
Left= 1680
Style = 1 'Graphical
TabIndex= 17
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 2
Left= 2040
Style = 1 'Graphical
TabIndex= 16
Top = 240
Width = 255
End
Begin VB.CommandButton button
Height = 300
Index = 0
Left= 2760
Style = 1 'Graphical
TabIndex= 14
Top = 240
Width = 255
End
Begin VB.Label Label3
Height = 255
Left= 3120
TabIndex= 22
Top = 240
Width = 1575
End
End
Begin VB.CommandButton Command1
Caption = "打开串口"
Height = 495
Left= 3720
TabIndex= 0
Top = 240
Width = 975
End
Begin VB.Frame Frame2
Height = 735
Left= 0
TabIndex= 3
Top = 1560
Width = 4815
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 7
Left= 2760
Style = 1 'Graphical
TabIndex= 12
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 6
Left= 2400
Style = 1 'Graphical
TabIndex= 11
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 5
Left= 2040
Style = 1 'Graphical
TabIndex= 10
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 4
Left= 1680
Style = 1 'Graphical
TabIndex= 9
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 3
Left= 1320
Style = 1 'Graphical
TabIndex= 8
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 2
Left= 960
Style = 1 'Graphical
TabIndex= 7
Top = 240
Width = 255
End
Begin VB.CommandButton LED
Enabled = 0 'False
Height = 255
Index = 0
Left= 240
Style = 1 'Graphical
TabIndex= 5
Top = 240
Width = 255
End
Begin VB.Label Label4
Height = 255
Left= 3120
TabIndex= 4
Top = 240
Width = 1455
End
End
Begin MSCommLib.MSComm MSComm1
Left= 5160
Top = 120
_ExtentX= 1005
_ExtentY= 1005
_Version= 393216
DTREnable = -1 'True
End
Begin VB.Frame Frame1
Height = 1455
Left= 0
TabIndex= 1
Top = 0
Width = 4815
Begin VB.CommandButton exit_com
Caption = "退出程序"
Height = 495
Left= 3720
TabIndex= 24
Top = 720
Width = 975
End
Begin VB.Timer Timer1
Interval= 500
Left= 4920
Top = 1080
End
Begin VB.Label Label2
Alignment = 2 'Center
Caption = "串口等待打开"
BeginProperty Font
Name= "宋体"
Size= 12
Charset = 134
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
Height = 360
Left= 240
TabIndex= 2
Top = 480
Width = 2040
End
End
Begin VB.Label Label5
Caption = "0"
Height = 255
Left= 5400
TabIndex= 23
Top = 1320
Width = 255
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim dat As Integer
'点击后 数据将发送到MCU
'把按钮数据 转换为 十进制数字 进行发送给下位机
Private Sub button_Click(Index As Integer)
If button.Item(Index).Caption = 1 Then
button.Item(Index).Caption = 0
button.Item(Index).BackColor = vbGreen
Else
button.Item(Index).Caption = 1
button.Item(Index).BackColor = vbRed
End If
Dim i, tmp
tmp = 0
For i = 0 To 7
tmp = tmp + (button.Item(i).Caption) * (2 ^ i)
Next i
Label3.Caption = "发送区:" &tmp &" " &Hex(tmp) '监视显示
dat = tmp
'send (tmp)
End Sub
Private Sub Command1_Click()
On Error GoTo ErrExit
MSComm1.PortOpen = True '关闭端口
Command1.Caption = "关闭串口"
Label2.Caption = "串口已打开"
Label2.ForeColor = vbBlue
'初始化 button 8个按钮
Dim i
For i = 0 To 7
button.Item(i).Caption = 0
button.Item(i).BackColor = vbGreen
button.Item(i).Enabled = True
Next i
Exit Sub
ErrExit: '打开端口
Command1.Caption = "打开串口"
Label2.Caption = "串口未打开"
Label2.ForeColor = vbRed
'初始化 button 8个按钮
' Dim i
For i = 0 To 7
button.Item(i).Caption = 0
button.Item(i).BackColor = vbGreen
button.Item(i).Enabled = False
Next i
ClosePort
End Sub
Public Sub ClosePort()
dat = 0
On Error GoTo ErrExit
MSComm1.PortOpen = False '关闭端口
Exit Sub
ErrExit:
End Sub
Private Sub exit_com_Click()
ClosePort '关闭串口
End
End Sub
Private Sub Form_Load()
If App.PrevInstance = True Then MsgBox "程序已经运行", , "创达友情提示": Exit Sub
Com_Init '串口 初始化
'初始化 button 8个按钮 显示0,白色,不可用状态(只用等待开启串口)
Dim i
For i = 0 To 7
button.Item(i).Caption = 0
button.Item(i).BackColor = vbWhite
button.Item(i).Enabled = False
Next i
End Sub
Private Sub Form_Unload(Cancel As Integer)
exit_com_Click '退出子程序
End Sub
Private Sub Timer1_Timer()
MSComm1_OnComm '实时读取下位机传上的数据
send (dat)
On Error Resume Next
chuli (Val(Label5.Caption)) '对数据进行一些处理 并体现在按钮上 必须Label5.Caption,用别的总是失败
End Sub
'//对数据实时读取
Sub MSComm1_OnComm()
Dim aa_input'数据暂时缓存
Dim tr_input
Select Case frmMain.MSComm1.CommEvent
Case comEvReceive
On Error Resume Next
tr_input = frmMain.MSComm1.Input
If tr_input = "" Then GoTo err1
aa_input = Format(Val(tr_input), "0") ' "#0.000")
txtMsg.Text = txtMsg.Text &aa_input &" "
Label4.Caption = "接收区:" &aa_input &" " &Hex(aa_input)
Label5.Caption = aa_input
err1:
Case Else
frmMain.MSComm1.InBufferCount = 0 '对缓存 *** 作
End Select
End Sub
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)