我试图在ubuntu中设置串口的中断(在用C编写的程序中),但它不起作用.我已经检查过串口通讯是否正常运行而没有中断,所以我可能会设置错误.代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <errno.h>
#include <termios.h>
void signal_handler_IO (int status); /* definition of signal handler */
int n;
int fd;
int connected;
struct termios termAttr;
struct sigaction saio;
int main(int argc, char *argv[])
{
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyO1\n");
exit(1);
}
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL; …Run Code Online (Sandbox Code Playgroud) 我通过套接字将信息从用C编写的程序发送到用Java编写的程序。
通过C中的程序,我正在通过char数组发送两个字节(使用Internet套接字),并且Java中收到的信息也存储在char数组中。
我的主要问题是Java数组中收到的信息与C程序发送的信息不正确对应。
我已经读过Java中的char是16位长,而C中的char是8位长。可能是问题所在,但我不知道该如何处理/解决。
发送信息的C代码如下:
char buffer[256];
bzero(buffer,256);
n = read(fd, buffer, 255); // getting info from an uart port, which works properly
n = write(sockfd,buffer,3); // send the information through the socket
Run Code Online (Sandbox Code Playgroud)
以下是部分Java代码(用于Android应用程序):
char[] buffer = new char[256];
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
int readX = in.read(buffer, 0, 3);
if (readX > 0) { // I am using a handler to manipulate the info
Message msg = new Message();
msg.obj = buffer;
mHandler.sendMessage(msg);
}
....
// Part of …Run Code Online (Sandbox Code Playgroud)