1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > 下位机单片机c语言发送数据到串口 上位机pc机java语言获取端口数据

下位机单片机c语言发送数据到串口 上位机pc机java语言获取端口数据

时间:2019-03-21 22:38:56

相关推荐

下位机单片机c语言发送数据到串口 上位机pc机java语言获取端口数据

环境:

Windows7 64b,jdk64b,myeclipse8.5,rxtx开发包,STC,keil,格西烽火,51单片机,rs232USB转串口线。

下位机c代码

#include <reg51.h>

#include <string.h>

#define INBUF_LEN 7 //数据长度

unsigned char inbuf1[INBUF_LEN];

unsigned char checksum,count3,count=0;

bitread_flag=0;

unsigned char com[]="hellon";

void init_serialcomm(void)

{

TMOD=0x20;//设置定时器1为模式2

TH1=0xfd;//装初值设定波特率

TL1=0xfd;

TR1=1;//启动定时器

SM0=0;//串口通信模式设置

SM1=1;

}

void delay_1s() //1s

{

unsigned int i;

for(i=0;i<45000;i++)

{

}

}

void delay_1us() //1s

{

unsigned int i;

for(i=0;i<45;i++)

{

}

}

//向串口发送一个字符

void send_char_com(char ch)

{

SBUF=ch;

while(TI==0);

TI=0;

delay_1us();

}

//向串口发送一个字符串,strlen为该字符串长度

void send_string_com(char *str,int strlen)

{

int k=0;

do

{

send_char_com(*(str + k));

k++;

} while(k < strlen);

}

//串口接收中断函数

void serial () interrupt 4 using 3

{

if(RI)

{

unsigned char ch1;

RI = 0;

ch1=SBUF;

inbuf1[count++]=ch1;

if(count==INBUF_LEN)

{

read_flag=1; //如果串口接收的数据达到INBUF_LEN个,且校验没错,

count=0; //就置位取数标志

}

}

}

main()

{

init_serialcomm(); //初始化串口

delay_1s();

send_string_com("10.7",4);

return 1;

}

上位机java代码

import mPort;

import mPortIdentifier;

import gnu.io.SerialPort;

import java.io.FileDescriptor;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.sql.*;

public class TwoWaySerialComm

{

public TwoWaySerialComm()

{

super();

}

void connect ( String portName ) throws Exception

{

CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);

if ( portIdentifier.isCurrentlyOwned() )

{

System.out.println("Error: Port is currently in use");

}

else

{

CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);

if ( commPort instanceof SerialPort )

{

SerialPort serialPort = (SerialPort) commPort;

serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

InputStream in = serialPort.getInputStream();

OutputStream out = serialPort.getOutputStream();

(new Thread(new SerialReader(in))).start();

(new Thread(new SerialWriter(out))).start();

}

else

{

System.out.println("Error: Only serial ports are handled by this example.");

}

}

}

/** */

public static class SerialReader implements Runnable

{

InputStream in;

public SerialReader ( InputStream in )

{

this.in = in;

}

public void run ()

{

byte[] buffer = new byte[1024];

int len = -1;

String com_get="";

DBInsert DI=new DBInsert();

try

{

while ( ( len = this.in.read(buffer)) > -1 )

{

//延时10秒

try {

Thread.sleep(10000);

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//com_get保存串口数据

com_get=new String(buffer,0,2);

System.out.print(com_get);

}

}

catch ( IOException e )

{

e.printStackTrace();

}

}

}

/** */

public static class SerialWriter implements Runnable

{

OutputStream out;

public SerialWriter ( OutputStream out )

{

this.out = out;

}

public void run ()

{

try

{

int c = 0;

while ( ( c = System.in.read()) > -1 )

{

this.out.write(c);

}

}

catch ( IOException e )

{

e.printStackTrace();

}

}

}

public static void main ( String[] args )

{

try

{

(new TwoWaySerialComm()).connect("COM3");

}

catch ( Exception e )

{

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

可能出现的问题

获取到的是乱码

可能是没打开单片机的开关按钮

提示open方法错误

可能是没关闭格西烽火或者myeclipse,两者不能同时打开。

提示找不到串口

检查下你的STC和代码,看是否端口正确。

ln

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。