您的位置:首页 > 编程语言 > C语言/C++

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

2015-01-26 19:41 393 查看

环境:

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;

bit           read_flag=0;

unsigned char com[]="hello\n";

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 gnu.io.CommPort;

import gnu.io.CommPortIdentifier;

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和代码,看是否端口正确。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息