pc和硬件通过界面进行串口通信,硬件上把内容为:
time:1970-1-1 Thu 1:37:56 self_id:1 father_id:0 neighbor_id:16
time:1970-1-1 Thu 1:38:33 self_id:16 father_id:0 neighbor_id:1
的文件一行行读到缓冲去通过串口发给pc,pc串口接收到后写到文件,得到的内容却为:
time:1970-1-1 Thu 1:37:56 self_id:1 father_id:0 neighbor_id:16
time:1970-1-1 Thu 1:37:56 self_id:1 father_id:0 neighbor_id:16
time:1970-1-1 Thu 1:38:33 self_id:16 father_id:0 neighbor_id:1
中间空白的部分都是2进制乱码。
在硬件端程序相关代码:
FILE *fp;
char data[200];
if((fp = fopen(filename,"r"))!= NULL)
{
while (fgets(data,200,fp)!= NULL)
{
int i;
for(i =0;data[i]!= '\0';i++){}
char s_data[i+1];//需要把每一行的‘\0’也发送
memcpy(s_data,data,i+1);
int len;
int send_len=sizeof(s_data);
len= write(fd_serial,s_data,send_len);
if(len == -1)
printf("Write s_data error.\n");
else
{
printf("Write %d bytes of s_data.\n",len); }
}
fclose(fp); }
pc端相关代码:
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[200];
try {
String fileName = null;
while (inputStream.available() > 0) {// InputStream inputStream;
int numBytes = inputStream.read(readBuffer);
fileName = "/root/info.txt";
try {
BufferedWriter bw = new BufferedWriter(
new FileWriter(fileName, true));
String str = new String(readBuffer);
bw.write(str);
bw.close(); } catch (IOException e) { } }
}
catch (IOException e) {
}
break;
}
}
问题出在哪?
time:1970-1-1 Thu 1:37:56 self_id:1 father_id:0 neighbor_id:16
time:1970-1-1 Thu 1:38:33 self_id:16 father_id:0 neighbor_id:1
的文件一行行读到缓冲去通过串口发给pc,pc串口接收到后写到文件,得到的内容却为:
time:1970-1-1 Thu 1:37:56 self_id:1 father_id:0 neighbor_id:16
time:1970-1-1 Thu 1:37:56 self_id:1 father_id:0 neighbor_id:16
time:1970-1-1 Thu 1:38:33 self_id:16 father_id:0 neighbor_id:1
中间空白的部分都是2进制乱码。
在硬件端程序相关代码:
FILE *fp;
char data[200];
if((fp = fopen(filename,"r"))!= NULL)
{
while (fgets(data,200,fp)!= NULL)
{
int i;
for(i =0;data[i]!= '\0';i++){}
char s_data[i+1];//需要把每一行的‘\0’也发送
memcpy(s_data,data,i+1);
int len;
int send_len=sizeof(s_data);
len= write(fd_serial,s_data,send_len);
if(len == -1)
printf("Write s_data error.\n");
else
{
printf("Write %d bytes of s_data.\n",len); }
}
fclose(fp); }
pc端相关代码:
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[200];
try {
String fileName = null;
while (inputStream.available() > 0) {// InputStream inputStream;
int numBytes = inputStream.read(readBuffer);
fileName = "/root/info.txt";
try {
BufferedWriter bw = new BufferedWriter(
new FileWriter(fileName, true));
String str = new String(readBuffer);
bw.write(str);
bw.close(); } catch (IOException e) { } }
}
catch (IOException e) {
}
break;
}
}
问题出在哪?
int send_len=sizeof(s_data);//长度都知道了为什么还用sizeof,如果s_data是指针怎么办建议楼主先试试1024大小,试试一次性会不会出错
pc程序中
{
try {
inputStream = serial_Port.getInputStream();
} catch (IOException a) { }
try {
serial_Port.addEventListener(this);
} catch (TooManyListenersException a) { }serial_Port.notifyOnDataAvailable(true);Thread readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(2000); } catch (Exception ex) { }}每次sleep的时间不同,SerialPortEvent.DATA_AVAILABLE 触发的时间间隔不同,得到的数据中间就会间隔很多空格,而且得到的数据会重复
你说用1024是指?还有楼上我问的关于SerialPortEvent.DATA_AVAILABLE 触发的时间间隔问题