MainActivity.java
package com.example.adc; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.Socket; import java.net.SocketTimeoutException; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { Button bt0, bt1, bt2; EditText et; Socket sock; MyThread mt; String index = "0"; public Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0x11) { et = (EditText) findViewById(R.id.et); Bundle bundle = msg.getData(); et.setText(bundle.getString("msg")); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt0 = (Button) findViewById(R.id.bt0); bt1 = (Button) findViewById(R.id.bt1); bt2 = (Button) findViewById(R.id.bt2); bt0.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub index = "0"; mt = new MyThread(index); mt.start(); } }); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub index = "1"; mt = new MyThread(index); mt.start(); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub index = "2"; mt = new MyThread(index); mt.start(); } }); } class MyThread extends Thread { public String index; Bundle bundle = new Bundle(); public MyThread(String str) { index = str; } public void run() { // 定义消息 try { // 连接服务器 并设置连接超时为5秒 sock = new Socket("192.168.221.140", 2174); OutputStream out = sock.getOutputStream(); BufferedReader bff = new BufferedReader(new InputStreamReader( sock.getInputStream())); out.write(index.getBytes()); out.flush(); while (true) { String line = null; while ((line = bff.readLine()) != null) { Message msg = new Message(); msg.what = 0x11; bundle.clear(); bundle.putString("msg", line); msg.setData(bundle); // 发送消息 修改UI线程中的组件 myHandler.sendMessage(msg); } } } catch (SocketTimeoutException aa) { aa.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/bt0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋钮0" /> <Button android:id="@+id/bt1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋钮1" /> <Button android:id="@+id/bt2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="旋钮2" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="number" > <requestFocus /> </EditText> </LinearLayout> </LinearLayout> s3c2410-adc.h #ifndef _S3C2410_ADC_H_ #define _S3C2410_ADC_H_ #define ADC_WRITE(ch, prescale) ((ch)<<16|(prescale)) #define ADC_WRITE_GETCH(data) (((data)>>16)&0x7) #define ADC_WRITE_GETPRE(data) ((data)&0xff) #endif /* _S3C2410_ADC_H_ */ server.c #include <stdio.h> #include<stdlib.h> #include <unistd.h> #include <sys/types.h> #include <sys/ipc.h> #include <sys/ioctl.h> #include <pthread.h> #include <fcntl.h> #include "s3c2410-adc.h" #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<pthread.h> #define ADC_DEV "/dev/adc/0raw" static int adc_fd = -1; static int init_ADdevice(void) { if((adc_fd=open(ADC_DEV, O_RDWR))<0){ printf("Error opening %s adc device\n", ADC_DEV); return -1; } } static int GetADresult(int channel) { int PRESCALE=0XFF; int data=ADC_WRITE(channel, PRESCALE); write(adc_fd, &data, sizeof(data)); read(adc_fd, &data, sizeof(data)); return data; } static int stop=0; static void* comMonitor(void* data) { getchar(); stop=1; return NULL; } int main(void) { int i; int num=0; float d; char buf[10]; pthread_t th_com; void * retval; int sock; int msgsock; struct sockaddr_in server,tcpaddr; int len=sizeof(tcpaddr); server.sin_family = PF_INET; server.sin_port= htons(2174); server.sin_addr.s_addr=inet_addr("192.168.221.200"); sock =socket(PF_INET,SOCK_STREAM,0); if(sock<0){ printf("error:socket\n"); return -1; } if(bind(sock,(struct sockaddr*)&server,sizeof(server))<0){ printf("error:bind\n"); return -1; } if(listen(sock,5)<0){ printf("error:listen\n"); return -1; } printf("listening......\n"); //set s3c44b0 AD register and start AD if(init_ADdevice()<0) return -1; /* Create the threads */ pthread_create(&th_com, NULL, comMonitor, 0); printf("\nPress Enter key exit!\n"); do { msgsock = accept(sock, (struct sockaddr *)&tcpaddr, (int *)&len); if (msgsock ==-1) { printf("error:accept\n"); return -1; } else { memset(buf, 0, sizeof(buf)); num=recv(msgsock,buf,sizeof(buf),0); i=atoi(buf);printf("%d\n",i); if(num<0){ printf("error:recv\n"); return -1; } while(stop==0){ d=((float)GetADresult(i)*3.3)/1024.0; memset(buf,0,sizeof(buf)); sprintf(buf,"%f",d);printf("%s\n",buf); buf[8]='\r'; num=send(msgsock,buf,sizeof(buf),0); if(num<0){ printf("error:send\n"); return -1; } usleep(1000000); } } }while (stop==0); /* Wait until producer and consumer finish. */ pthread_join(th_com, &retval); printf("\n"); return 0; }