我有unsigned int DataBAR并想发送char到串口!
我的代码是:
unsigned char Printer_buffer[PRN_BUFFER_SIZE];
unsigned int DataBAR, DataD, DataT;
for (i = 0; i < 8; i++) {
SumaN = SumaN + (Printer_buffer[i] & 0x0F);
DataBAR = (Printer_buffer[i] & 0x0F) + 0x30;
nbytes = write(fd,DataBAR ,1); //want to send to the serial port
printf("write error code is %d !!!!!!!!!\n", errno);
if (nbytes != 1) {
printf("error writing on serial port!!!\n");
}
sleep(1);
SumaP = SumaP + ((Printer_buffer[i] >> 4) & 0x0F);
DataBAR = …Run Code Online (Sandbox Code Playgroud) 我有一系列字符,如:
char bytes[8]={2,0,1,3,0,8,1,9}
Run Code Online (Sandbox Code Playgroud)
我想从下面的数组中获取前四个字符,并将它们放入一个新的整数变量中.我怎样才能做到这一点?我试图改变他们,但这种逻辑不起作用.任何的想法?谢谢.
示例:从此数组中获取:年月日
char bytes[8]={2,0,1,3,0,8,1,9}
int year = 2013 ...... month = 8 ............ day = 19
Run Code Online (Sandbox Code Playgroud) 我需要为C语言编写一个用于Linux条形码阅读器的驱动程序.条形码阅读器通过串行总线工作.当我向条形码阅读器发送一组命令时,条形码阅读器应该向我返回状态消息.我设法配置端口并创建信号处理程序.在信号处理程序中,我读取了串行总线接收的数据.
所以问题是:我应该读取缓冲区中的数据然后使用它吗?我是否可以使用以这种方式配置的端口将数据写入缓冲区?当设备回复我时,根据该回复数据,我需要向设备发送另一个命令.另外,我可以write()用来写消息吗?如果我不能使用它,我应该使用什么命令?你可以用write命令帮我一点吗?
我发送给设备的命令总是7个字节,但是应答数据在7-32个字节之间变化.如果读取函数发送了不同数量的字节,那么我怎样才能确定收到所有数据,这样我才能使用它?
这是我写的一些代码.我正朝着正确的方向前进吗?再一次,这个想法非常简单:我正在向设备发送命令,设备会中断并回复.我读了发送的内容,我使用了回复数据,根据该数据,我发送了另一个命令.谢谢你.
#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;
char buffer[14];
int bytes;
struct termios termAttr;
struct sigaction saio;
int main(int argc, char *argv[])
{
fd = open("/dev/ttyUSB1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyO1\n");
exit(1);
} …Run Code Online (Sandbox Code Playgroud) 我有一个 char 数组,但值是字符的十进制表示形式。例子:
char bytes[4]={50,48,49,51}
Run Code Online (Sandbox Code Playgroud)
如何将其转换为获取 char 数组,如下所示:
char bytes1[4]={2,0,1,3}
Run Code Online (Sandbox Code Playgroud)